Return std::string from ada_exception_catchpoint_cond_string
[deliverable/binutils-gdb.git] / gdb / nto-procfs.c
CommitLineData
61bb466e 1/* Machine independent support for QNX Neutrino /proc (process file system)
0df8b418 2 for GDB. Written by Colin Burgess at QNX Software Systems Limited.
61bb466e 3
e2882c85 4 Copyright (C) 2003-2018 Free Software Foundation, Inc.
61bb466e
KW
5
6 Contributed by QNX Software Systems Ltd.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
61bb466e
KW
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
61bb466e
KW
22
23#include "defs.h"
24
25#include <fcntl.h>
26#include <spawn.h>
27#include <sys/debug.h>
28#include <sys/procfs.h>
29#include <sys/neutrino.h>
30#include <sys/syspage.h>
2978b111 31#include <dirent.h>
61bb466e 32#include <sys/netmgr.h>
8a6c0ccd
AR
33#include <sys/auxv.h>
34
61bb466e
KW
35#include "gdbcore.h"
36#include "inferior.h"
37#include "target.h"
38#include "objfiles.h"
39#include "gdbthread.h"
40#include "nto-tdep.h"
41#include "command.h"
42#include "regcache.h"
5ea03926 43#include "solib.h"
ee8e9165 44#include "inf-child.h"
774ee6d2 45#include "common/filestuff.h"
5dc1a704 46#include "common/scoped_fd.h"
61bb466e
KW
47
48#define NULL_PID 0
49#define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
50 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
51
61bb466e
KW
52int ctl_fd;
53
a40805d4 54static sighandler_t ofunc;
61bb466e
KW
55
56static procfs_run run;
57
f6ac5f3d
PA
58/* Create the "native" and "procfs" targets. */
59
60struct nto_procfs_target : public inf_child_target
61{
62 void open (const char *arg, int from_tty) override;
63
64 void attach (const char *, int) override = 0;
65
66 void post_attach (int);
67
68 void detach (inferior *, int) override;
69
70 void resume (ptid_t, int, enum gdb_signal) override;
71
72 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
73
74 void fetch_registers (struct regcache *, int) override;
75 void store_registers (struct regcache *, int) override;
76
77 enum target_xfer_status xfer_partial (enum target_object object,
78 const char *annex,
79 gdb_byte *readbuf,
80 const gdb_byte *writebuf,
81 ULONGEST offset, ULONGEST len,
82 ULONGEST *xfered_len) override;
83
84 void files_info () override;
85
86 int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
87
88 int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
89 enum remove_bp_reason) override;
90
91 int can_use_hw_breakpoint (enum bptype, int, int) override;
92
93 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
94
95 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
96
97 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
98 struct expression *) override;
99
100 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
101 struct expression *) override;
102
57810aa7 103 bool stopped_by_watchpoint () override;
f6ac5f3d
PA
104
105 void kill () override;
106
107 void create_inferior (const char *, const std::string &,
108 char **, int) override;
109
110 void mourn_inferior () override;
61bb466e 111
f6ac5f3d 112 void pass_signals (int, unsigned char *) override;
61bb466e 113
57810aa7 114 bool thread_alive (ptid_t ptid) override;
61bb466e 115
f6ac5f3d 116 void update_thread_list () override;
61bb466e 117
f6ac5f3d
PA
118 const char *pid_to_str (ptid_t) override;
119
120 void interrupt () override;
121
122 bool have_continuable_watchpoint ()
123 { return true; }
124
125 const char *extra_thread_info (struct thread_info *) override;
126
127 char *pid_to_exec_file (int pid) override;
128};
129
130/* For "target native". */
f6ac5f3d 131
d9f719f1
PA
132static const target_info nto_native_target_info = {
133 "native",
134 N_("QNX Neutrino local process"),
135 N_("QNX Neutrino local process (started by the \"run\" command).")
136};
f6ac5f3d 137
d9f719f1
PA
138class nto_procfs_target_native final : public nto_procfs_target
139{
140 const target_info &info () const override
141 { return nto_native_target_info; }
f6ac5f3d
PA
142};
143
144/* For "target procfs <node>". */
f6ac5f3d 145
d9f719f1
PA
146static const target_info nto_procfs_target_info = {
147 "procfs",
148 N_("QNX Neutrino local or remote process"),
149 N_("QNX Neutrino process. target procfs <node>")
150};
f6ac5f3d 151
d9f719f1
PA
152struct nto_procfs_target_procfs final : public nto_procfs_target
153{
154 const target_info &info () const override
155 { return nto_procfs_target_info; }
f6ac5f3d
PA
156};
157
158static ptid_t do_attach (ptid_t ptid);
61bb466e 159
6a3cb8e8 160/* These two globals are only ever set in procfs_open_1, but are
61bb466e
KW
161 referenced elsewhere. 'nto_procfs_node' is a flag used to say
162 whether we are local, or we should get the current node descriptor
163 for the remote QNX node. */
609c3040 164static char *nodestr;
61bb466e
KW
165static unsigned nto_procfs_node = ND_LOCAL_NODE;
166
167/* Return the current QNX Node, or error out. This is a simple
168 wrapper for the netmgr_strtond() function. The reason this
169 is required is because QNX node descriptors are transient so
170 we have to re-acquire them every time. */
171static unsigned
d737fd7f 172nto_node (void)
61bb466e
KW
173{
174 unsigned node;
175
609c3040
AR
176 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
177 || nodestr == NULL)
61bb466e
KW
178 return ND_LOCAL_NODE;
179
609c3040 180 node = netmgr_strtond (nodestr, 0);
61bb466e 181 if (node == -1)
8a3fe4f8 182 error (_("Lost the QNX node. Debug session probably over."));
61bb466e
KW
183
184 return (node);
185}
186
d737fd7f
KW
187static enum gdb_osabi
188procfs_is_nto_target (bfd *abfd)
189{
190 return GDB_OSABI_QNXNTO;
191}
192
6a3cb8e8
PA
193/* This is called when we call 'target native' or 'target procfs
194 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
195 will be a QNX node string, eg: "/net/some_node". If arg is not a
196 valid QNX node, we will default to local. */
f6ac5f3d
PA
197void
198nto_procfs_target::open (const char *arg, int from_tty)
61bb466e 199{
61bb466e
KW
200 char *endstr;
201 char buffer[50];
5dc1a704 202 int total_size;
61bb466e 203 procfs_sysinfo *sysinfo;
609c3040 204 char nto_procfs_path[PATH_MAX];
61bb466e 205
6a3cb8e8
PA
206 /* Offer to kill previous inferiors before opening this target. */
207 target_preopen (from_tty);
208
d737fd7f
KW
209 nto_is_nto_target = procfs_is_nto_target;
210
61bb466e
KW
211 /* Set the default node used for spawning to this one,
212 and only override it if there is a valid arg. */
213
609c3040
AR
214 xfree (nodestr);
215 nodestr = NULL;
216
61bb466e 217 nto_procfs_node = ND_LOCAL_NODE;
609c3040 218 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
61bb466e
KW
219
220 init_thread_list ();
221
222 if (nodestr)
223 {
224 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
225 if (nto_procfs_node == -1)
226 {
227 if (errno == ENOTSUP)
228 printf_filtered ("QNX Net Manager not found.\n");
229 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
dc5dd1eb 230 errno, safe_strerror (errno));
61bb466e
KW
231 xfree (nodestr);
232 nodestr = NULL;
233 nto_procfs_node = ND_LOCAL_NODE;
234 }
235 else if (*endstr)
236 {
237 if (*(endstr - 1) == '/')
238 *(endstr - 1) = 0;
239 else
240 *endstr = 0;
241 }
242 }
609c3040
AR
243 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
244 (nodestr != NULL) ? nodestr : "", "/proc");
61bb466e 245
5dc1a704
TT
246 scoped_fd fd (open (nto_procfs_path, O_RDONLY));
247 if (fd.get () == -1)
61bb466e
KW
248 {
249 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
dc5dd1eb 250 safe_strerror (errno));
8a3fe4f8 251 error (_("Invalid procfs arg"));
61bb466e
KW
252 }
253
254 sysinfo = (void *) buffer;
5dc1a704 255 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
61bb466e
KW
256 {
257 printf_filtered ("Error getting size: %d (%s)\n", errno,
dc5dd1eb 258 safe_strerror (errno));
8a3fe4f8 259 error (_("Devctl failed."));
61bb466e
KW
260 }
261 else
262 {
263 total_size = sysinfo->total_size;
264 sysinfo = alloca (total_size);
609c3040 265 if (sysinfo == NULL)
61bb466e
KW
266 {
267 printf_filtered ("Memory error: %d (%s)\n", errno,
dc5dd1eb 268 safe_strerror (errno));
8a3fe4f8 269 error (_("alloca failed."));
61bb466e
KW
270 }
271 else
272 {
5dc1a704
TT
273 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, total_size, 0)
274 != EOK)
61bb466e
KW
275 {
276 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
dc5dd1eb 277 safe_strerror (errno));
8a3fe4f8 278 error (_("Devctl failed."));
61bb466e
KW
279 }
280 else
281 {
282 if (sysinfo->type !=
1143fffb 283 nto_map_arch_to_cputype (gdbarch_bfd_arch_info
f5656ead 284 (target_gdbarch ())->arch_name))
9fe4a216 285 error (_("Invalid target CPU."));
61bb466e
KW
286 }
287 }
288 }
6a3cb8e8 289
f6ac5f3d 290 inf_child_target::open (arg, from_tty);
61bb466e
KW
291 printf_filtered ("Debugging using %s\n", nto_procfs_path);
292}
293
294static void
295procfs_set_thread (ptid_t ptid)
296{
297 pid_t tid;
298
299 tid = ptid_get_tid (ptid);
300 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
301}
302
57810aa7
PA
303/* Return true if the thread TH is still alive. */
304
305bool
f6ac5f3d 306nto_procfs_target::thread_alive (ptid_t ptid)
61bb466e
KW
307{
308 pid_t tid;
745a434e
AR
309 pid_t pid;
310 procfs_status status;
311 int err;
61bb466e
KW
312
313 tid = ptid_get_tid (ptid);
745a434e
AR
314 pid = ptid_get_pid (ptid);
315
316 if (kill (pid, 0) == -1)
57810aa7 317 return false;
745a434e
AR
318
319 status.tid = tid;
320 if ((err = devctl (ctl_fd, DCMD_PROC_TIDSTATUS,
321 &status, sizeof (status), 0)) != EOK)
57810aa7 322 return false;
745a434e
AR
323
324 /* Thread is alive or dead but not yet joined,
325 or dead and there is an alive (or dead unjoined) thread with
326 higher tid.
327
328 If the tid is not the same as requested, requested tid is dead. */
329 return (status.tid == tid) && (status.state != STATE_DEAD);
330}
331
332static void
333update_thread_private_data_name (struct thread_info *new_thread,
334 const char *newname)
335{
7aabaf9d 336 nto_thread_info *pti = get_nto_thread_info (new_thread);
745a434e
AR
337
338 gdb_assert (newname != NULL);
339 gdb_assert (new_thread != NULL);
7aabaf9d
SM
340
341 if (pti)
745a434e 342 {
7aabaf9d
SM
343 pti = new nto_thread_info;
344 new_thread->priv.reset (pti);
745a434e 345 }
7aabaf9d
SM
346
347 pti->name = newname;
745a434e
AR
348}
349
350static void
351update_thread_private_data (struct thread_info *new_thread,
352 pthread_t tid, int state, int flags)
353{
745a434e
AR
354 procfs_info pidinfo;
355 struct _thread_name *tn;
356 procfs_threadctl tctl;
357
358#if _NTO_VERSION > 630
359 gdb_assert (new_thread != NULL);
360
361 if (devctl (ctl_fd, DCMD_PROC_INFO, &pidinfo,
362 sizeof(pidinfo), 0) != EOK)
363 return;
364
365 memset (&tctl, 0, sizeof (tctl));
366 tctl.cmd = _NTO_TCTL_NAME;
367 tn = (struct _thread_name *) (&tctl.data);
368
369 /* Fetch name for the given thread. */
370 tctl.tid = tid;
371 tn->name_buf_len = sizeof (tctl.data) - sizeof (*tn);
372 tn->new_name_len = -1; /* Getting, not setting. */
373 if (devctl (ctl_fd, DCMD_PROC_THREADCTL, &tctl, sizeof (tctl), NULL) != EOK)
374 tn->name_buf[0] = '\0';
375
376 tn->name_buf[_NTO_THREAD_NAME_MAX] = '\0';
377
378 update_thread_private_data_name (new_thread, tn->name_buf);
379
7aabaf9d 380 nto_thread_info *pti = get_nto_thread_info (new_thread);
745a434e
AR
381 pti->tid = tid;
382 pti->state = state;
383 pti->flags = flags;
384#endif /* _NTO_VERSION */
61bb466e
KW
385}
386
f6ac5f3d
PA
387void
388nto_procfs_target::update_thread_list ()
61bb466e
KW
389{
390 procfs_status status;
391 pid_t pid;
392 ptid_t ptid;
745a434e
AR
393 pthread_t tid;
394 struct thread_info *new_thread;
61bb466e
KW
395
396 if (ctl_fd == -1)
397 return;
398
e8032dde
PA
399 prune_threads ();
400
61bb466e
KW
401 pid = ptid_get_pid (inferior_ptid);
402
745a434e
AR
403 status.tid = 1;
404
405 for (tid = 1;; ++tid)
61bb466e 406 {
745a434e
AR
407 if (status.tid == tid
408 && (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
409 != EOK))
61bb466e 410 break;
745a434e
AR
411 if (status.tid != tid)
412 /* The reason why this would not be equal is that devctl might have
413 returned different tid, meaning the requested tid no longer exists
414 (e.g. thread exited). */
415 continue;
416 ptid = ptid_build (pid, 0, tid);
417 new_thread = find_thread_ptid (ptid);
418 if (!new_thread)
419 new_thread = add_thread (ptid);
420 update_thread_private_data (new_thread, tid, status.state, 0);
421 status.tid++;
61bb466e
KW
422 }
423 return;
424}
425
9fe4a216
TT
426static void
427do_closedir_cleanup (void *dir)
428{
429 closedir (dir);
430}
431
774ee6d2 432static void
1d12d88f 433procfs_pidlist (const char *args, int from_tty)
61bb466e
KW
434{
435 DIR *dp = NULL;
436 struct dirent *dirp = NULL;
609c3040 437 char buf[PATH_MAX];
61bb466e
KW
438 procfs_info *pidinfo = NULL;
439 procfs_debuginfo *info = NULL;
440 procfs_status *status = NULL;
441 pid_t num_threads = 0;
442 pid_t pid;
443 char name[512];
9fe4a216 444 struct cleanup *cleanups;
609c3040 445 char procfs_dir[PATH_MAX];
61bb466e 446
609c3040
AR
447 snprintf (procfs_dir, sizeof (procfs_dir), "%s%s",
448 (nodestr != NULL) ? nodestr : "", "/proc");
449
450 dp = opendir (procfs_dir);
61bb466e
KW
451 if (dp == NULL)
452 {
dc5dd1eb 453 fprintf_unfiltered (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
609c3040 454 procfs_dir, errno, safe_strerror (errno));
61bb466e
KW
455 return;
456 }
457
9fe4a216
TT
458 cleanups = make_cleanup (do_closedir_cleanup, dp);
459
61bb466e
KW
460 /* Start scan at first pid. */
461 rewinddir (dp);
462
463 do
464 {
465 /* Get the right pid and procfs path for the pid. */
466 do
467 {
468 dirp = readdir (dp);
469 if (dirp == NULL)
470 {
9fe4a216 471 do_cleanups (cleanups);
61bb466e
KW
472 return;
473 }
609c3040
AR
474 snprintf (buf, sizeof (buf), "%s%s/%s/as",
475 (nodestr != NULL) ? nodestr : "",
476 "/proc", dirp->d_name);
61bb466e
KW
477 pid = atoi (dirp->d_name);
478 }
479 while (pid == 0);
480
0df8b418 481 /* Open the procfs path. */
5dc1a704
TT
482 scoped_fd fd (open (buf, O_RDONLY));
483 if (fd.get () == -1)
61bb466e 484 {
dc5dd1eb 485 fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
d737fd7f 486 buf, errno, safe_strerror (errno));
609c3040 487 continue;
61bb466e
KW
488 }
489
490 pidinfo = (procfs_info *) buf;
5dc1a704 491 if (devctl (fd.get (), DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
61bb466e 492 {
dc5dd1eb 493 fprintf_unfiltered (gdb_stderr,
d737fd7f
KW
494 "devctl DCMD_PROC_INFO failed - %d (%s)\n",
495 errno, safe_strerror (errno));
61bb466e
KW
496 break;
497 }
498 num_threads = pidinfo->num_threads;
499
500 info = (procfs_debuginfo *) buf;
5dc1a704
TT
501 if (devctl (fd.get (), DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0)
502 != EOK)
61bb466e
KW
503 strcpy (name, "unavailable");
504 else
505 strcpy (name, info->path);
506
507 /* Collect state info on all the threads. */
508 status = (procfs_status *) buf;
509 for (status->tid = 1; status->tid <= num_threads; status->tid++)
510 {
609c3040 511 const int err
5dc1a704 512 = devctl (fd.get (), DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
609c3040
AR
513 printf_filtered ("%s - %d", name, pid);
514 if (err == EOK && status->tid != 0)
515 printf_filtered ("/%d\n", status->tid);
516 else
517 {
518 printf_filtered ("\n");
519 break;
520 }
61bb466e 521 }
61bb466e
KW
522 }
523 while (dirp != NULL);
524
9fe4a216 525 do_cleanups (cleanups);
61bb466e
KW
526 return;
527}
528
774ee6d2 529static void
1d12d88f 530procfs_meminfo (const char *args, int from_tty)
61bb466e
KW
531{
532 procfs_mapinfo *mapinfos = NULL;
533 static int num_mapinfos = 0;
534 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
535 int flags = ~0, err, num, i, j;
536
537 struct
538 {
539 procfs_debuginfo info;
540 char buff[_POSIX_PATH_MAX];
541 } map;
542
543 struct info
544 {
545 unsigned addr;
546 unsigned size;
547 unsigned flags;
548 unsigned debug_vaddr;
549 unsigned long long offset;
550 };
551
552 struct printinfo
553 {
554 unsigned long long ino;
555 unsigned dev;
556 struct info text;
557 struct info data;
558 char name[256];
559 } printme;
560
561 /* Get the number of map entrys. */
562 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
563 if (err != EOK)
564 {
d737fd7f
KW
565 printf ("failed devctl num mapinfos - %d (%s)\n", err,
566 safe_strerror (err));
61bb466e
KW
567 return;
568 }
569
774ee6d2 570 mapinfos = XNEWVEC (procfs_mapinfo, num);
61bb466e
KW
571
572 num_mapinfos = num;
573 mapinfo_p = mapinfos;
574
575 /* Fill the map entrys. */
576 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
577 * sizeof (procfs_mapinfo), &num);
578 if (err != EOK)
579 {
5483d879 580 printf ("failed devctl mapinfos - %d (%s)\n", err, safe_strerror (err));
61bb466e
KW
581 xfree (mapinfos);
582 return;
583 }
584
325fac50 585 num = std::min (num, num_mapinfos);
61bb466e
KW
586
587 /* Run through the list of mapinfos, and store the data and text info
588 so we can print it at the bottom of the loop. */
589 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
590 {
591 if (!(mapinfo_p->flags & flags))
592 mapinfo_p->ino = 0;
593
594 if (mapinfo_p->ino == 0) /* Already visited. */
595 continue;
596
597 map.info.vaddr = mapinfo_p->vaddr;
598
599 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
600 if (err != EOK)
601 continue;
602
603 memset (&printme, 0, sizeof printme);
604 printme.dev = mapinfo_p->dev;
605 printme.ino = mapinfo_p->ino;
606 printme.text.addr = mapinfo_p->vaddr;
607 printme.text.size = mapinfo_p->size;
608 printme.text.flags = mapinfo_p->flags;
609 printme.text.offset = mapinfo_p->offset;
610 printme.text.debug_vaddr = map.info.vaddr;
611 strcpy (printme.name, map.info.path);
612
613 /* Check for matching data. */
614 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
615 {
616 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
617 && mapinfo_p2->ino == mapinfo_p->ino
618 && mapinfo_p2->dev == mapinfo_p->dev)
619 {
620 map.info.vaddr = mapinfo_p2->vaddr;
621 err =
622 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
623 if (err != EOK)
624 continue;
625
626 if (strcmp (map.info.path, printme.name))
627 continue;
628
629 /* Lower debug_vaddr is always text, if nessessary, swap. */
630 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
631 {
632 memcpy (&(printme.data), &(printme.text),
633 sizeof (printme.data));
634 printme.text.addr = mapinfo_p2->vaddr;
635 printme.text.size = mapinfo_p2->size;
636 printme.text.flags = mapinfo_p2->flags;
637 printme.text.offset = mapinfo_p2->offset;
638 printme.text.debug_vaddr = map.info.vaddr;
639 }
640 else
641 {
642 printme.data.addr = mapinfo_p2->vaddr;
643 printme.data.size = mapinfo_p2->size;
644 printme.data.flags = mapinfo_p2->flags;
645 printme.data.offset = mapinfo_p2->offset;
646 printme.data.debug_vaddr = map.info.vaddr;
647 }
648 mapinfo_p2->ino = 0;
649 }
650 }
651 mapinfo_p->ino = 0;
652
653 printf_filtered ("%s\n", printme.name);
654 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
655 printme.text.addr);
656 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
657 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
2244ba2e 658 printf_filtered ("\t\toffset=%s\n", phex (printme.text.offset, 8));
61bb466e
KW
659 if (printme.data.size)
660 {
661 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
662 printme.data.addr);
663 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
664 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
2244ba2e 665 printf_filtered ("\t\toffset=%s\n", phex (printme.data.offset, 8));
61bb466e
KW
666 }
667 printf_filtered ("\tdev=0x%x\n", printme.dev);
668 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
669 }
670 xfree (mapinfos);
671 return;
672}
673
674/* Print status information about what we're accessing. */
f6ac5f3d
PA
675void
676nto_procfs_target::files_info ()
61bb466e 677{
181e7f93
PA
678 struct inferior *inf = current_inferior ();
679
61bb466e 680 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
3fdfcbf1 681 inf->attach_flag ? "attached" : "child",
609c3040
AR
682 target_pid_to_str (inferior_ptid),
683 (nodestr != NULL) ? nodestr : "local node");
61bb466e
KW
684}
685
e5343fde
AR
686/* Target to_pid_to_exec_file implementation. */
687
f6ac5f3d
PA
688char *
689nto_procfs_target::pid_to_exec_file (const int pid)
e5343fde
AR
690{
691 int proc_fd;
692 static char proc_path[PATH_MAX];
693 ssize_t rd;
694
695 /* Read exe file name. */
696 snprintf (proc_path, sizeof (proc_path), "%s/proc/%d/exefile",
697 (nodestr != NULL) ? nodestr : "", pid);
698 proc_fd = open (proc_path, O_RDONLY);
699 if (proc_fd == -1)
700 return NULL;
701
702 rd = read (proc_fd, proc_path, sizeof (proc_path) - 1);
703 close (proc_fd);
704 if (rd <= 0)
705 {
706 proc_path[0] = '\0';
707 return NULL;
708 }
709 proc_path[rd] = '\0';
710 return proc_path;
711}
712
61bb466e 713/* Attach to process PID, then initialize for debugging it. */
f6ac5f3d
PA
714void
715nto_procfs_target::attach (const char *args, int from_tty)
61bb466e
KW
716{
717 char *exec_file;
718 int pid;
181e7f93 719 struct inferior *inf;
61bb466e 720
74164c56 721 pid = parse_pid_to_attach (args);
61bb466e
KW
722
723 if (pid == getpid ())
8a3fe4f8 724 error (_("Attaching GDB to itself is not a good idea..."));
61bb466e
KW
725
726 if (from_tty)
727 {
728 exec_file = (char *) get_exec_file (0);
729
730 if (exec_file)
731 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
732 target_pid_to_str (pid_to_ptid (pid)));
733 else
734 printf_unfiltered ("Attaching to %s\n",
735 target_pid_to_str (pid_to_ptid (pid)));
736
737 gdb_flush (gdb_stdout);
738 }
739 inferior_ptid = do_attach (pid_to_ptid (pid));
6c95b8df
PA
740 inf = current_inferior ();
741 inferior_appeared (inf, pid);
181e7f93 742 inf->attach_flag = 1;
7f9f62ba 743
6a3cb8e8
PA
744 if (!target_is_pushed (ops))
745 push_target (ops);
7f9f62ba 746
e8032dde 747 procfs_update_thread_list (ops);
61bb466e
KW
748}
749
f6ac5f3d
PA
750void
751nto_procfs_target::post_attach (pid_t pid)
61bb466e 752{
61bb466e 753 if (exec_bfd)
268a4a75 754 solib_create_inferior_hook (0);
61bb466e
KW
755}
756
757static ptid_t
758do_attach (ptid_t ptid)
759{
760 procfs_status status;
761 struct sigevent event;
dc5dd1eb 762 char path[PATH_MAX];
61bb466e 763
609c3040
AR
764 snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
765 (nodestr != NULL) ? nodestr : "", "/proc", ptid_get_pid (ptid));
61bb466e
KW
766 ctl_fd = open (path, O_RDWR);
767 if (ctl_fd == -1)
8a3fe4f8 768 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
dc5dd1eb 769 safe_strerror (errno));
61bb466e 770 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
8a3fe4f8 771 error (_("Couldn't stop process"));
61bb466e
KW
772
773 /* Define a sigevent for process stopped notification. */
774 event.sigev_notify = SIGEV_SIGNAL_THREAD;
775 event.sigev_signo = SIGUSR1;
776 event.sigev_code = 0;
777 event.sigev_value.sival_ptr = NULL;
778 event.sigev_priority = -1;
779 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
780
781 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
782 && status.flags & _DEBUG_FLAG_STOPPED)
dfd4cc63 783 SignalKill (nto_node (), ptid_get_pid (ptid), 0, SIGCONT, 0, 0);
61bb466e 784 nto_init_solib_absolute_prefix ();
dfd4cc63 785 return ptid_build (ptid_get_pid (ptid), 0, status.tid);
61bb466e
KW
786}
787
788/* Ask the user what to do when an interrupt is received. */
789static void
dc5dd1eb 790interrupt_query (void)
61bb466e 791{
9e2f0ad4
HZ
792 if (query (_("Interrupted while waiting for the program.\n\
793Give up (and stop debugging it)? ")))
61bb466e 794 {
bc1e6c81 795 target_mourn_inferior (inferior_ptid);
039e3c22 796 quit ();
61bb466e 797 }
61bb466e
KW
798}
799
800/* The user typed ^C twice. */
801static void
bfedc46a 802nto_handle_sigint_twice (int signo)
61bb466e
KW
803{
804 signal (signo, ofunc);
805 interrupt_query ();
bfedc46a 806 signal (signo, nto_handle_sigint_twice);
61bb466e
KW
807}
808
809static void
bfedc46a 810nto_handle_sigint (int signo)
61bb466e
KW
811{
812 /* If this doesn't work, try more severe steps. */
bfedc46a 813 signal (signo, nto_handle_sigint_twice);
61bb466e 814
e671cd59 815 target_interrupt ();
61bb466e
KW
816}
817
f6ac5f3d
PA
818sptid_t
819nto_procfs_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
820 int options)
61bb466e
KW
821{
822 sigset_t set;
823 siginfo_t info;
824 procfs_status status;
825 static int exit_signo = 0; /* To track signals that cause termination. */
826
827 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
828
829 if (ptid_equal (inferior_ptid, null_ptid))
830 {
831 ourstatus->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 832 ourstatus->value.sig = GDB_SIGNAL_0;
61bb466e
KW
833 exit_signo = 0;
834 return null_ptid;
835 }
836
837 sigemptyset (&set);
838 sigaddset (&set, SIGUSR1);
839
840 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
841 while (!(status.flags & _DEBUG_FLAG_ISTOP))
842 {
a40805d4 843 ofunc = signal (SIGINT, nto_handle_sigint);
61bb466e
KW
844 sigwaitinfo (&set, &info);
845 signal (SIGINT, ofunc);
846 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
847 }
848
a9889169
AR
849 nto_inferior_data (NULL)->stopped_flags = status.flags;
850 nto_inferior_data (NULL)->stopped_pc = status.ip;
851
61bb466e
KW
852 if (status.flags & _DEBUG_FLAG_SSTEP)
853 {
854 ourstatus->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 855 ourstatus->value.sig = GDB_SIGNAL_TRAP;
61bb466e
KW
856 }
857 /* Was it a breakpoint? */
858 else if (status.flags & _DEBUG_FLAG_TRACE)
859 {
860 ourstatus->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 861 ourstatus->value.sig = GDB_SIGNAL_TRAP;
61bb466e
KW
862 }
863 else if (status.flags & _DEBUG_FLAG_ISTOP)
864 {
865 switch (status.why)
866 {
867 case _DEBUG_WHY_SIGNALLED:
868 ourstatus->kind = TARGET_WAITKIND_STOPPED;
869 ourstatus->value.sig =
2ea28649 870 gdb_signal_from_host (status.info.si_signo);
61bb466e
KW
871 exit_signo = 0;
872 break;
873 case _DEBUG_WHY_FAULTED:
874 ourstatus->kind = TARGET_WAITKIND_STOPPED;
875 if (status.info.si_signo == SIGTRAP)
876 {
877 ourstatus->value.sig = 0;
878 exit_signo = 0;
879 }
880 else
881 {
882 ourstatus->value.sig =
2ea28649 883 gdb_signal_from_host (status.info.si_signo);
61bb466e
KW
884 exit_signo = ourstatus->value.sig;
885 }
886 break;
887
888 case _DEBUG_WHY_TERMINATED:
889 {
890 int waitval = 0;
891
dfd4cc63 892 waitpid (ptid_get_pid (inferior_ptid), &waitval, WNOHANG);
61bb466e
KW
893 if (exit_signo)
894 {
895 /* Abnormal death. */
896 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
897 ourstatus->value.sig = exit_signo;
898 }
899 else
900 {
901 /* Normal death. */
902 ourstatus->kind = TARGET_WAITKIND_EXITED;
903 ourstatus->value.integer = WEXITSTATUS (waitval);
904 }
905 exit_signo = 0;
906 break;
907 }
908
909 case _DEBUG_WHY_REQUESTED:
910 /* We are assuming a requested stop is due to a SIGINT. */
911 ourstatus->kind = TARGET_WAITKIND_STOPPED;
a493e3e2 912 ourstatus->value.sig = GDB_SIGNAL_INT;
61bb466e
KW
913 exit_signo = 0;
914 break;
915 }
916 }
917
a6a7f2a5 918 return ptid_build (status.pid, 0, status.tid);
61bb466e
KW
919}
920
921/* Read the current values of the inferior's registers, both the
922 general register set and floating point registers (if supported)
923 and update gdb's idea of their current values. */
f6ac5f3d
PA
924void
925nto_procfs_target::fetch_registers (struct regcache *regcache, int regno)
61bb466e
KW
926{
927 union
928 {
929 procfs_greg greg;
930 procfs_fpreg fpreg;
931 procfs_altreg altreg;
932 }
933 reg;
934 int regsize;
935
bcc0c096 936 procfs_set_thread (regcache_get_ptid (regcache));
61bb466e 937 if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
56be3814 938 nto_supply_gregset (regcache, (char *) &reg.greg);
61bb466e
KW
939 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
940 == EOK)
56be3814 941 nto_supply_fpregset (regcache, (char *) &reg.fpreg);
61bb466e
KW
942 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, &reg, sizeof (reg), &regsize)
943 == EOK)
56be3814 944 nto_supply_altregset (regcache, (char *) &reg.altreg);
61bb466e
KW
945}
946
9d46c4e5
PA
947/* Helper for procfs_xfer_partial that handles memory transfers.
948 Arguments are like target_xfer_partial. */
61bb466e 949
9d46c4e5
PA
950static enum target_xfer_status
951procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
952 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
61bb466e 953{
9d46c4e5
PA
954 int nbytes;
955
956 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
957 return TARGET_XFER_E_IO;
958
959 if (writebuf != NULL)
960 nbytes = write (ctl_fd, writebuf, len);
961 else
962 nbytes = read (ctl_fd, readbuf, len);
963 if (nbytes <= 0)
964 return TARGET_XFER_E_IO;
965 *xfered_len = nbytes;
966 return TARGET_XFER_OK;
967}
968
969/* Target to_xfer_partial implementation. */
61bb466e 970
f6ac5f3d
PA
971enum target_xfer_status
972nto_procfs_target::xfer_partial (enum target_object object,
973 const char *annex, gdb_byte *readbuf,
974 const gdb_byte *writebuf, ULONGEST offset,
975 ULONGEST len, ULONGEST *xfered_len)
9d46c4e5
PA
976{
977 switch (object)
61bb466e 978 {
9d46c4e5
PA
979 case TARGET_OBJECT_MEMORY:
980 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
8a6c0ccd
AR
981 case TARGET_OBJECT_AUXV:
982 if (readbuf != NULL)
983 {
984 int err;
985 CORE_ADDR initial_stack;
986 debug_process_t procinfo;
987 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
988 const unsigned int sizeof_auxv_t = sizeof (auxv_t);
989 const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
990 int tempread;
991 gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
992
993 if (tempbuf == NULL)
994 return TARGET_XFER_E_IO;
995
996 err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
997 sizeof procinfo, 0);
998 if (err != EOK)
999 return TARGET_XFER_E_IO;
1000
1001 initial_stack = procinfo.initial_stack;
1002
1003 /* procfs is always 'self-hosted', no byte-order manipulation. */
1004 tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
1005 sizeof_tempbuf,
1006 sizeof (auxv_t));
325fac50 1007 tempread = std::min (tempread, len) - offset;
8a6c0ccd
AR
1008 memcpy (readbuf, tempbuf + offset, tempread);
1009 *xfered_len = tempread;
1010 return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
1011 }
1012 /* Fallthru */
9d46c4e5 1013 default:
f6ac5f3d
PA
1014 return this->beneath->xfer_partial (object, annex,
1015 readbuf, writebuf, offset, len,
1016 xfered_len);
61bb466e 1017 }
61bb466e
KW
1018}
1019
1020/* Take a program previously attached to and detaches it.
1021 The program resumes execution and will no longer stop
1022 on signals, etc. We'd better not have left any breakpoints
1023 in the program or it'll die when it hits one. */
f6ac5f3d
PA
1024void
1025nto_procfs_target::detach (inferior *inf, int from_tty)
61bb466e 1026{
7f9f62ba 1027 int pid;
61bb466e 1028
0f48b757
PA
1029 target_announce_detach ();
1030
61bb466e 1031 if (siggnal)
6bd6f3b6 1032 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, 0, 0, 0);
61bb466e
KW
1033
1034 close (ctl_fd);
1035 ctl_fd = -1;
7f9f62ba
PA
1036
1037 pid = ptid_get_pid (inferior_ptid);
61bb466e 1038 inferior_ptid = null_ptid;
7f9f62ba
PA
1039 detach_inferior (pid);
1040 init_thread_list ();
6a3cb8e8 1041 inf_child_maybe_unpush_target (ops);
61bb466e
KW
1042}
1043
1044static int
1045procfs_breakpoint (CORE_ADDR addr, int type, int size)
1046{
1047 procfs_break brk;
1048
1049 brk.type = type;
1050 brk.addr = addr;
1051 brk.size = size;
1052 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1053 if (errno != EOK)
1054 return 1;
1055 return 0;
1056}
1057
f6ac5f3d
PA
1058int
1059nto_procfs_target::insert_breakpoint (struct gdbarch *gdbarch,
1060 struct bp_target_info *bp_tgt)
61bb466e 1061{
0d5ed153 1062 bp_tgt->placed_address = bp_tgt->reqstd_address;
8181d85f 1063 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
61bb466e
KW
1064}
1065
f6ac5f3d
PA
1066int
1067nto_procfs_target::remove_breakpoint (struct gdbarch *gdbarch,
1068 struct bp_target_info *bp_tgt,
1069 enum remove_bp_reason reason)
61bb466e 1070{
8181d85f 1071 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
61bb466e
KW
1072}
1073
f6ac5f3d
PA
1074int
1075nto_procfs_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1076 struct bp_target_info *bp_tgt)
61bb466e 1077{
0d5ed153 1078 bp_tgt->placed_address = bp_tgt->reqstd_address;
8181d85f
DJ
1079 return procfs_breakpoint (bp_tgt->placed_address,
1080 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
61bb466e
KW
1081}
1082
f6ac5f3d
PA
1083int
1084nto_procfs_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1085 struct bp_target_info *bp_tgt)
61bb466e 1086{
8181d85f
DJ
1087 return procfs_breakpoint (bp_tgt->placed_address,
1088 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
61bb466e
KW
1089}
1090
f6ac5f3d
PA
1091void
1092nto_procfs_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
61bb466e
KW
1093{
1094 int signal_to_pass;
1095 procfs_status status;
14ef7606 1096 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
61bb466e
KW
1097
1098 if (ptid_equal (inferior_ptid, null_ptid))
1099 return;
1100
1101 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
1102 ptid);
1103
1104 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
1105 if (step)
1106 run.flags |= _DEBUG_RUN_STEP;
1107
14ef7606
AR
1108 sigemptyset (run_fault);
1109 sigaddset (run_fault, FLTBPT);
1110 sigaddset (run_fault, FLTTRACE);
1111 sigaddset (run_fault, FLTILL);
1112 sigaddset (run_fault, FLTPRIV);
1113 sigaddset (run_fault, FLTBOUNDS);
1114 sigaddset (run_fault, FLTIOVF);
1115 sigaddset (run_fault, FLTIZDIV);
1116 sigaddset (run_fault, FLTFPE);
61bb466e 1117 /* Peter V will be changing this at some point. */
14ef7606 1118 sigaddset (run_fault, FLTPAGE);
61bb466e
KW
1119
1120 run.flags |= _DEBUG_RUN_ARM;
1121
2ea28649 1122 signal_to_pass = gdb_signal_to_host (signo);
61bb466e
KW
1123
1124 if (signal_to_pass)
1125 {
1126 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
2ea28649 1127 signal_to_pass = gdb_signal_to_host (signo);
61bb466e
KW
1128 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1129 {
1130 if (signal_to_pass != status.info.si_signo)
1131 {
dfd4cc63 1132 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0,
d737fd7f 1133 signal_to_pass, 0, 0);
61bb466e
KW
1134 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1135 }
0df8b418 1136 else /* Let it kill the program without telling us. */
61bb466e
KW
1137 sigdelset (&run.trace, signal_to_pass);
1138 }
1139 }
1140 else
1141 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1142
1143 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1144 if (errno != EOK)
1145 {
9b20d036 1146 perror (_("run error!\n"));
61bb466e
KW
1147 return;
1148 }
1149}
1150
f6ac5f3d
PA
1151void
1152nto_procfs_target::mourn_inferior ()
61bb466e
KW
1153{
1154 if (!ptid_equal (inferior_ptid, null_ptid))
1155 {
dfd4cc63 1156 SignalKill (nto_node (), ptid_get_pid (inferior_ptid), 0, SIGKILL, 0, 0);
61bb466e
KW
1157 close (ctl_fd);
1158 }
1159 inferior_ptid = null_ptid;
1160 init_thread_list ();
c1ee2fb3 1161 inf_child_mourn_inferior (ops);
61bb466e
KW
1162}
1163
1164/* This function breaks up an argument string into an argument
1165 vector suitable for passing to execvp().
1166 E.g., on "run a b c d" this routine would get as input
1167 the string "a b c d", and as output it would fill in argv with
1168 the four arguments "a", "b", "c", "d". The only additional
1169 functionality is simple quoting. The gdb command:
1170 run a "b c d" f
1171 will fill in argv with the three args "a", "b c d", "e". */
1172static void
1173breakup_args (char *scratch, char **argv)
1174{
1175 char *pp, *cp = scratch;
1176 char quoting = 0;
1177
1178 for (;;)
1179 {
1180 /* Scan past leading separators. */
1181 quoting = 0;
1182 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1183 cp++;
1184
1185 /* Break if at end of string. */
1186 if (*cp == '\0')
1187 break;
1188
1189 /* Take an arg. */
1190 if (*cp == '"')
1191 {
1192 cp++;
1193 quoting = strchr (cp, '"') ? 1 : 0;
1194 }
1195
1196 *argv++ = cp;
1197
1198 /* Scan for next arg separator. */
1199 pp = cp;
1200 if (quoting)
1201 cp = strchr (pp, '"');
1202 if ((cp == NULL) || (!quoting))
1203 cp = strchr (pp, ' ');
1204 if (cp == NULL)
1205 cp = strchr (pp, '\t');
1206 if (cp == NULL)
1207 cp = strchr (pp, '\n');
1208
1209 /* No separators => end of string => break. */
1210 if (cp == NULL)
1211 {
1212 pp = cp;
1213 break;
1214 }
1215
1216 /* Replace the separator with a terminator. */
1217 *cp++ = '\0';
1218 }
1219
1220 /* Execv requires a null-terminated arg vector. */
1221 *argv = NULL;
1222}
1223
f6ac5f3d
PA
1224void
1225nto_procfs_target::create_inferior (const char *exec_file,
1226 const std::string &allargs,
1227 char **env, int from_tty)
61bb466e
KW
1228{
1229 struct inheritance inherit;
1230 pid_t pid;
1231 int flags, errn;
1232 char **argv, *args;
3cb3b8df 1233 const char *in = "", *out = "", *err = "";
61bb466e
KW
1234 int fd, fds[3];
1235 sigset_t set;
3cb3b8df 1236 const char *inferior_io_terminal = get_inferior_io_terminal ();
3fdfcbf1 1237 struct inferior *inf;
61bb466e 1238
7c5ded6a 1239 argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
61bb466e
KW
1240 sizeof (*argv));
1241 argv[0] = get_exec_file (1);
1242 if (!argv[0])
1243 {
1244 if (exec_file)
1245 argv[0] = exec_file;
1246 else
1247 return;
1248 }
1249
7c5ded6a 1250 args = xstrdup (allargs.c_str ());
609c3040 1251 breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
61bb466e
KW
1252
1253 argv = nto_parse_redirection (argv, &in, &out, &err);
1254
1255 fds[0] = STDIN_FILENO;
1256 fds[1] = STDOUT_FILENO;
1257 fds[2] = STDERR_FILENO;
1258
1259 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1260 if the i/o is not also being specified via redirection. */
1261 if (inferior_io_terminal)
1262 {
1263 if (!in[0])
1264 in = inferior_io_terminal;
1265 if (!out[0])
1266 out = inferior_io_terminal;
1267 if (!err[0])
1268 err = inferior_io_terminal;
1269 }
1270
1271 if (in[0])
1272 {
1273 fd = open (in, O_RDONLY);
1274 if (fd == -1)
1275 perror (in);
1276 else
1277 fds[0] = fd;
1278 }
1279 if (out[0])
1280 {
1281 fd = open (out, O_WRONLY);
1282 if (fd == -1)
1283 perror (out);
1284 else
1285 fds[1] = fd;
1286 }
1287 if (err[0])
1288 {
1289 fd = open (err, O_WRONLY);
1290 if (fd == -1)
1291 perror (err);
1292 else
1293 fds[2] = fd;
1294 }
1295
1296 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1297 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1298
1299 sigemptyset (&set);
1300 sigaddset (&set, SIGUSR1);
1301 sigprocmask (SIG_UNBLOCK, &set, NULL);
1302
1303 memset (&inherit, 0, sizeof (inherit));
1304
1305 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1306 {
d737fd7f 1307 inherit.nd = nto_node ();
61bb466e
KW
1308 inherit.flags |= SPAWN_SETND;
1309 inherit.flags &= ~SPAWN_EXEC;
1310 }
1311 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1312 inherit.pgroup = SPAWN_NEWPGROUP;
1313 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1314 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1315 xfree (args);
1316
1317 sigprocmask (SIG_BLOCK, &set, NULL);
1318
1319 if (pid == -1)
8a3fe4f8 1320 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
d737fd7f 1321 safe_strerror (errno));
61bb466e
KW
1322
1323 if (fds[0] != STDIN_FILENO)
1324 close (fds[0]);
1325 if (fds[1] != STDOUT_FILENO)
1326 close (fds[1]);
1327 if (fds[2] != STDERR_FILENO)
1328 close (fds[2]);
1329
1330 inferior_ptid = do_attach (pid_to_ptid (pid));
e8032dde 1331 procfs_update_thread_list (ops);
61bb466e 1332
6c95b8df
PA
1333 inf = current_inferior ();
1334 inferior_appeared (inf, pid);
3fdfcbf1 1335 inf->attach_flag = 0;
7f9f62ba 1336
61bb466e
KW
1337 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1338 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1339 if (errn != EOK)
1340 {
1341 /* FIXME: expected warning? */
1342 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1343 errn, strerror(errn) ); */
1344 }
6a3cb8e8
PA
1345 if (!target_is_pushed (ops))
1346 push_target (ops);
223ffa71 1347 target_terminal::init ();
61bb466e 1348
61bb466e
KW
1349 if (exec_bfd != NULL
1350 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
268a4a75 1351 solib_create_inferior_hook (0);
61bb466e
KW
1352}
1353
f6ac5f3d
PA
1354void
1355nto_procfs_target::interrupt ()
61bb466e
KW
1356{
1357 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1358}
1359
f6ac5f3d
PA
1360void
1361nto_procfs_target::kill ()
61bb466e 1362{
bc1e6c81 1363 target_mourn_inferior (inferior_ptid);
61bb466e
KW
1364}
1365
61bb466e
KW
1366/* Fill buf with regset and return devctl cmd to do the setting. Return
1367 -1 if we fail to get the regset. Store size of regset in regsize. */
1368static int
1369get_regset (int regset, char *buf, int bufsize, int *regsize)
1370{
1371 int dev_get, dev_set;
1372 switch (regset)
1373 {
1374 case NTO_REG_GENERAL:
1375 dev_get = DCMD_PROC_GETGREG;
1376 dev_set = DCMD_PROC_SETGREG;
1377 break;
1378
1379 case NTO_REG_FLOAT:
1380 dev_get = DCMD_PROC_GETFPREG;
1381 dev_set = DCMD_PROC_SETFPREG;
1382 break;
1383
1384 case NTO_REG_ALT:
1385 dev_get = DCMD_PROC_GETALTREG;
1386 dev_set = DCMD_PROC_SETALTREG;
1387 break;
1388
1389 case NTO_REG_SYSTEM:
1390 default:
1391 return -1;
1392 }
97c44116 1393 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
61bb466e
KW
1394 return -1;
1395
1396 return dev_set;
1397}
1398
f6ac5f3d
PA
1399void
1400nto_procfs_target::store_registers (struct regcache *regcache, int regno)
61bb466e
KW
1401{
1402 union
1403 {
1404 procfs_greg greg;
1405 procfs_fpreg fpreg;
1406 procfs_altreg altreg;
1407 }
1408 reg;
1409 unsigned off;
1410 int len, regset, regsize, dev_set, err;
1411 char *data;
bcc0c096 1412 ptid_t ptid = regcache_get_ptid (regcache);
61bb466e 1413
bcc0c096 1414 if (ptid_equal (ptid, null_ptid))
61bb466e 1415 return;
bcc0c096 1416 procfs_set_thread (ptid);
61bb466e
KW
1417
1418 if (regno == -1)
1419 {
1420 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1421 {
1422 dev_set = get_regset (regset, (char *) &reg,
1423 sizeof (reg), &regsize);
1424 if (dev_set == -1)
1425 continue;
1426
56be3814 1427 if (nto_regset_fill (regcache, regset, (char *) &reg) == -1)
61bb466e
KW
1428 continue;
1429
1430 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1431 if (err != EOK)
1432 fprintf_unfiltered (gdb_stderr,
1433 "Warning unable to write regset %d: %s\n",
dc5dd1eb 1434 regno, safe_strerror (err));
61bb466e
KW
1435 }
1436 }
1437 else
1438 {
1439 regset = nto_regset_id (regno);
1440 if (regset == -1)
1441 return;
1442
1443 dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
1444 if (dev_set == -1)
1445 return;
1446
ac7936df 1447 len = nto_register_area (regcache->arch (),
60441ab9 1448 regno, regset, &off);
61bb466e
KW
1449
1450 if (len < 1)
1451 return;
1452
56be3814 1453 regcache_raw_collect (regcache, regno, (char *) &reg + off);
61bb466e
KW
1454
1455 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1456 if (err != EOK)
1457 fprintf_unfiltered (gdb_stderr,
1458 "Warning unable to write regset %d: %s\n", regno,
dc5dd1eb 1459 safe_strerror (err));
61bb466e
KW
1460 }
1461}
1462
2455069d
UW
1463/* Set list of signals to be handled in the target. */
1464
f6ac5f3d
PA
1465void
1466nto_procfs_target::pass_signals (int numsigs, unsigned char *pass_signals)
61bb466e
KW
1467{
1468 int signo;
1469
2455069d
UW
1470 sigfillset (&run.trace);
1471
61bb466e
KW
1472 for (signo = 1; signo < NSIG; signo++)
1473 {
2ea28649 1474 int target_signo = gdb_signal_from_host (signo);
2455069d
UW
1475 if (target_signo < numsigs && pass_signals[target_signo])
1476 sigdelset (&run.trace, signo);
61bb466e
KW
1477 }
1478}
1479
f6ac5f3d
PA
1480char *
1481nto_procfs_target::pid_to_str (ptid_t ptid)
61bb466e
KW
1482{
1483 static char buf[1024];
1484 int pid, tid, n;
1485 struct tidinfo *tip;
1486
1487 pid = ptid_get_pid (ptid);
1488 tid = ptid_get_tid (ptid);
1489
dc5dd1eb 1490 n = snprintf (buf, 1023, "process %d", pid);
61bb466e
KW
1491
1492#if 0 /* NYI */
1493 tip = procfs_thread_info (pid, tid);
1494 if (tip != NULL)
dc5dd1eb 1495 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
61bb466e
KW
1496#endif
1497
1498 return buf;
1499}
1500
132f8e03
PA
1501/* to_can_run implementation for "target procfs". Note this really
1502 means "can this target be the default run target", which there can
1503 be only one, and we make it be "target native" like other ports.
1504 "target procfs <node>" wouldn't make sense as default run target, as
1505 it needs <node>. */
ee8e9165 1506
f6ac5f3d
PA
1507int
1508nto_procfs_target::can_run ()
132f8e03
PA
1509{
1510 return 0;
1511}
1512
1513/* "target procfs". */
f6ac5f3d 1514static nto_procfs_target_procfs nto_procfs_ops;
132f8e03 1515
6a3cb8e8 1516/* "target native". */
f6ac5f3d 1517static nto_procfs_target_native nto_native_ops;
6a3cb8e8 1518
132f8e03
PA
1519/* Create the "native" and "procfs" targets. */
1520
1521static void
1522init_procfs_targets (void)
61bb466e 1523{
132f8e03 1524 /* Register "target native". This is the default run target. */
d9f719f1
PA
1525 add_target (nto_native_target_info, inf_child_open_target);
1526 set_native_target (&nto_native_ops);
132f8e03
PA
1527
1528 /* Register "target procfs <node>". */
d9f719f1 1529 add_target (nto_procfs_target_info, inf_child_open_target);
61bb466e
KW
1530}
1531
1532#define OSTYPE_NTO 1
1533
1534void
dc5dd1eb 1535_initialize_procfs (void)
61bb466e
KW
1536{
1537 sigset_t set;
1538
132f8e03 1539 init_procfs_targets ();
61bb466e
KW
1540
1541 /* We use SIGUSR1 to gain control after we block waiting for a process.
1542 We use sigwaitevent to wait. */
1543 sigemptyset (&set);
1544 sigaddset (&set, SIGUSR1);
1545 sigprocmask (SIG_BLOCK, &set, NULL);
1546
2455069d
UW
1547 /* Initially, make sure all signals are reported. */
1548 sigfillset (&run.trace);
61bb466e
KW
1549
1550 /* Stuff some information. */
1551 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1552 nto_cpuinfo_valid = 1;
1553
1bedd215
AC
1554 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1555 add_info ("meminfo", procfs_meminfo, _("memory information"));
d737fd7f
KW
1556
1557 nto_is_nto_target = procfs_is_nto_target;
61bb466e
KW
1558}
1559
1560
1561static int
f486487f 1562procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
61bb466e
KW
1563{
1564 procfs_break brk;
1565
1566 switch (type)
1567 {
f486487f 1568 case hw_read:
61bb466e
KW
1569 brk.type = _DEBUG_BREAK_RD;
1570 break;
f486487f 1571 case hw_access:
61bb466e
KW
1572 brk.type = _DEBUG_BREAK_RW;
1573 break;
1574 default: /* Modify. */
1575/* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1576 brk.type = _DEBUG_BREAK_RW;
1577 }
1578 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1579 brk.addr = addr;
1580 brk.size = len;
1581
1582 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1583 if (errno != EOK)
1584 {
9b20d036 1585 perror (_("Failed to set hardware watchpoint"));
61bb466e
KW
1586 return -1;
1587 }
1588 return 0;
1589}
1590
57810aa7 1591bool
f6ac5f3d
PA
1592nto_procfs_target::can_use_hw_breakpoint (enum bptype type,
1593 int cnt, int othertype)
61bb466e
KW
1594{
1595 return 1;
1596}
1597
f6ac5f3d
PA
1598int
1599nto_procfs_target::remove_hw_watchpoint (CORE_ADDR addr, int len,
1600 enum target_hw_bp_type type,
1601 struct expression *cond)
61bb466e
KW
1602{
1603 return procfs_hw_watchpoint (addr, -1, type);
1604}
1605
f6ac5f3d
PA
1606int
1607nto_procfs_target::insert_hw_watchpoint (CORE_ADDR addr, int len,
1608 enum target_hw_bp_type type,
1609 struct expression *cond)
61bb466e
KW
1610{
1611 return procfs_hw_watchpoint (addr, len, type);
1612}
1613
57810aa7 1614bool
f6ac5f3d 1615nto_procfs_target::stopped_by_watchpoint ()
61bb466e 1616{
a9889169
AR
1617 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1618 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1619 future gdb versions will likely run in 'non-stop' mode in which case
1620 we will have to store/examine statuses per thread in question.
1621 Until then, this will work fine. */
1622
1623 struct inferior *inf = current_inferior ();
1624 struct nto_inferior_data *inf_data;
1625
1626 gdb_assert (inf != NULL);
1627
1628 inf_data = nto_inferior_data (inf);
1629
1630 return inf_data->stopped_flags
1631 & (_DEBUG_FLAG_TRACE_RD
1632 | _DEBUG_FLAG_TRACE_WR
1633 | _DEBUG_FLAG_TRACE_MODIFY);
61bb466e 1634}
This page took 1.405204 seconds and 4 git commands to generate.