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