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