2003-07-04 Kris Warkentin <kewarken@qnx.com>
[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 2003 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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 #include "defs.h"
26
27 #include <fcntl.h>
28 #include <spawn.h>
29 #include <sys/debug.h>
30 #include <sys/procfs.h>
31 #include <sys/neutrino.h>
32 #include <sys/syspage.h>
33 #include <dirent.h>
34 #include <sys/netmgr.h>
35
36 #include "gdb_string.h"
37 #include "gdbcore.h"
38 #include "inferior.h"
39 #include "target.h"
40 #include "objfiles.h"
41 #include "gdbthread.h"
42 #include "nto-tdep.h"
43 #include "command.h"
44 #include "regcache.h"
45
46 #define NULL_PID 0
47 #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
48 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
49
50 static struct target_ops procfs_ops;
51
52 int ctl_fd;
53
54 static void (*ofunc) ();
55
56 static procfs_run run;
57
58 static void procfs_open (char *, int);
59
60 static int procfs_can_run (void);
61
62 static ptid_t procfs_wait (ptid_t, struct target_waitstatus *);
63
64 static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
65 struct mem_attrib *attrib,
66 struct target_ops *);
67
68 static void procfs_fetch_registers (int);
69
70 static void notice_signals (void);
71
72 static void init_procfs_ops (void);
73
74 static ptid_t do_attach (ptid_t ptid);
75
76 static int procfs_can_use_hw_breakpoint (int, int, int);
77
78 static int procfs_insert_hw_breakpoint (CORE_ADDR, char *);
79
80 static int procfs_remove_hw_breakpoint (CORE_ADDR addr, char *);
81
82 static int procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type);
83
84 static int procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type);
85
86 static int procfs_stopped_by_watchpoint (void);
87
88 /* These two globals are only ever set in procfs_open(), but are
89 referenced elsewhere. 'nto_procfs_node' is a flag used to say
90 whether we are local, or we should get the current node descriptor
91 for the remote QNX node. */
92 static char nto_procfs_path[PATH_MAX] = { "/proc" };
93 static unsigned nto_procfs_node = ND_LOCAL_NODE;
94
95 /* Return the current QNX Node, or error out. This is a simple
96 wrapper for the netmgr_strtond() function. The reason this
97 is required is because QNX node descriptors are transient so
98 we have to re-acquire them every time. */
99 static unsigned
100 nto_node()
101 {
102 unsigned node;
103
104 if (ND_NODE_CMP(nto_procfs_node, ND_LOCAL_NODE) == 0)
105 return ND_LOCAL_NODE;
106
107 node = netmgr_strtond(nto_procfs_path,0);
108 if (node == -1)
109 error ("Lost the QNX node. Debug session probably over.");
110
111 return (node);
112 }
113
114 /* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
115 For QNX6 (nto), the only valid arg will be a QNX node string,
116 eg: "/net/some_node". If arg is not a valid QNX node, we will
117 default to local. */
118 static void
119 procfs_open (char *arg, int from_tty)
120 {
121 char *nodestr;
122 char *endstr;
123 char buffer[50];
124 int fd, total_size;
125 procfs_sysinfo *sysinfo;
126
127 /* Set the default node used for spawning to this one,
128 and only override it if there is a valid arg. */
129
130 nto_procfs_node = ND_LOCAL_NODE;
131 nodestr = arg ? xstrdup (arg) : arg;
132
133 init_thread_list ();
134
135 if (nodestr)
136 {
137 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
138 if (nto_procfs_node == -1)
139 {
140 if (errno == ENOTSUP)
141 printf_filtered ("QNX Net Manager not found.\n");
142 printf_filtered ("Invalid QNX node %s: error %d (%s).\n", nodestr,
143 errno, strerror (errno));
144 xfree (nodestr);
145 nodestr = NULL;
146 nto_procfs_node = ND_LOCAL_NODE;
147 }
148 else if (*endstr)
149 {
150 if (*(endstr - 1) == '/')
151 *(endstr - 1) = 0;
152 else
153 *endstr = 0;
154 }
155 }
156 sprintf (nto_procfs_path, "%s%s", nodestr ? nodestr : "", "/proc");
157 if (nodestr)
158 xfree (nodestr);
159
160 fd = open (nto_procfs_path, O_RDONLY);
161 if (fd == -1)
162 {
163 printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
164 strerror (errno));
165 error ("Invalid procfs arg");
166 }
167
168 sysinfo = (void *) buffer;
169 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
170 {
171 printf_filtered ("Error getting size: %d (%s)\n", errno,
172 strerror (errno));
173 close (fd);
174 error ("Devctl failed.");
175 }
176 else
177 {
178 total_size = sysinfo->total_size;
179 sysinfo = alloca (total_size);
180 if (!sysinfo)
181 {
182 printf_filtered ("Memory error: %d (%s)\n", errno,
183 strerror (errno));
184 close (fd);
185 error ("alloca failed.");
186 }
187 else
188 {
189 if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK)
190 {
191 printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
192 strerror (errno));
193 close (fd);
194 error ("Devctl failed.");
195 }
196 else
197 {
198 if (sysinfo->type !=
199 nto_map_arch_to_cputype (TARGET_ARCHITECTURE->arch_name))
200 {
201 close (fd);
202 error ("Invalid target CPU.");
203 }
204 }
205 }
206 }
207 close (fd);
208 printf_filtered ("Debugging using %s\n", nto_procfs_path);
209 }
210
211 static void
212 procfs_set_thread (ptid_t ptid)
213 {
214 pid_t tid;
215
216 tid = ptid_get_tid (ptid);
217 devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0);
218 }
219
220 /* Return nonzero if the thread TH is still alive. */
221 static int
222 procfs_thread_alive (ptid_t ptid)
223 {
224 pid_t tid;
225
226 tid = ptid_get_tid (ptid);
227 if (devctl (ctl_fd, DCMD_PROC_CURTHREAD, &tid, sizeof (tid), 0) == EOK)
228 return 1;
229 return 0;
230 }
231
232 void
233 procfs_find_new_threads (void)
234 {
235 procfs_status status;
236 pid_t pid;
237 ptid_t ptid;
238
239 if (ctl_fd == -1)
240 return;
241
242 pid = ptid_get_pid (inferior_ptid);
243
244 for (status.tid = 1;; ++status.tid)
245 {
246 if (devctl (ctl_fd, DCMD_PROC_TIDSTATUS, &status, sizeof (status), 0)
247 != EOK && status.tid != 0)
248 break;
249 ptid = ptid_build (pid, 0, status.tid);
250 if (!in_thread_list (ptid))
251 add_thread (ptid);
252 }
253 return;
254 }
255
256 void
257 procfs_pidlist (char *args, int from_tty)
258 {
259 DIR *dp = NULL;
260 struct dirent *dirp = NULL;
261 int fd = -1;
262 char buf[512];
263 procfs_info *pidinfo = NULL;
264 procfs_debuginfo *info = NULL;
265 procfs_status *status = NULL;
266 pid_t num_threads = 0;
267 pid_t pid;
268 char name[512];
269
270 dp = opendir (nto_procfs_path);
271 if (dp == NULL)
272 {
273 printf ("failed to opendir \"%s\" - %d (%s)", nto_procfs_path, errno,
274 strerror (errno));
275 return;
276 }
277
278 /* Start scan at first pid. */
279 rewinddir (dp);
280
281 do
282 {
283 /* Get the right pid and procfs path for the pid. */
284 do
285 {
286 dirp = readdir (dp);
287 if (dirp == NULL)
288 {
289 closedir (dp);
290 return;
291 }
292 sprintf (buf, "%s/%s/as", nto_procfs_path, dirp->d_name);
293 pid = atoi (dirp->d_name);
294 }
295 while (pid == 0);
296
297 /* Open the procfs path. */
298 fd = open (buf, O_RDONLY);
299 if (fd == -1)
300 {
301 printf ("failed to open %s - %d (%s)\n", buf, errno,
302 strerror (errno));
303 closedir (dp);
304 return;
305 }
306
307 pidinfo = (procfs_info *) buf;
308 if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
309 {
310 printf ("devctl DCMD_PROC_INFO failed - %d (%s)\n", errno,
311 strerror (errno));
312 break;
313 }
314 num_threads = pidinfo->num_threads;
315
316 info = (procfs_debuginfo *) buf;
317 if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK)
318 strcpy (name, "unavailable");
319 else
320 strcpy (name, info->path);
321
322 /* Collect state info on all the threads. */
323 status = (procfs_status *) buf;
324 for (status->tid = 1; status->tid <= num_threads; status->tid++)
325 {
326 if (devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0) != EOK
327 && status->tid != 0)
328 break;
329 if (status->tid != 0)
330 printf_filtered ("%s - %d/%d\n", name, pid, status->tid);
331 }
332 close (fd);
333 }
334 while (dirp != NULL);
335
336 close (fd);
337 closedir (dp);
338 return;
339 }
340
341 void
342 procfs_meminfo (char *args, int from_tty)
343 {
344 procfs_mapinfo *mapinfos = NULL;
345 static int num_mapinfos = 0;
346 procfs_mapinfo *mapinfo_p, *mapinfo_p2;
347 int flags = ~0, err, num, i, j;
348
349 struct
350 {
351 procfs_debuginfo info;
352 char buff[_POSIX_PATH_MAX];
353 } map;
354
355 struct info
356 {
357 unsigned addr;
358 unsigned size;
359 unsigned flags;
360 unsigned debug_vaddr;
361 unsigned long long offset;
362 };
363
364 struct printinfo
365 {
366 unsigned long long ino;
367 unsigned dev;
368 struct info text;
369 struct info data;
370 char name[256];
371 } printme;
372
373 /* Get the number of map entrys. */
374 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, NULL, 0, &num);
375 if (err != EOK)
376 {
377 printf ("failed devctl num mapinfos - %d (%s)\n", err, strerror (err));
378 return;
379 }
380
381 mapinfos = xmalloc (num * sizeof (procfs_mapinfo));
382
383 num_mapinfos = num;
384 mapinfo_p = mapinfos;
385
386 /* Fill the map entrys. */
387 err = devctl (ctl_fd, DCMD_PROC_MAPINFO, mapinfo_p, num
388 * sizeof (procfs_mapinfo), &num);
389 if (err != EOK)
390 {
391 printf ("failed devctl mapinfos - %d (%s)\n", err, strerror (err));
392 xfree (mapinfos);
393 return;
394 }
395
396 num = min (num, num_mapinfos);
397
398 /* Run through the list of mapinfos, and store the data and text info
399 so we can print it at the bottom of the loop. */
400 for (mapinfo_p = mapinfos, i = 0; i < num; i++, mapinfo_p++)
401 {
402 if (!(mapinfo_p->flags & flags))
403 mapinfo_p->ino = 0;
404
405 if (mapinfo_p->ino == 0) /* Already visited. */
406 continue;
407
408 map.info.vaddr = mapinfo_p->vaddr;
409
410 err = devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
411 if (err != EOK)
412 continue;
413
414 memset (&printme, 0, sizeof printme);
415 printme.dev = mapinfo_p->dev;
416 printme.ino = mapinfo_p->ino;
417 printme.text.addr = mapinfo_p->vaddr;
418 printme.text.size = mapinfo_p->size;
419 printme.text.flags = mapinfo_p->flags;
420 printme.text.offset = mapinfo_p->offset;
421 printme.text.debug_vaddr = map.info.vaddr;
422 strcpy (printme.name, map.info.path);
423
424 /* Check for matching data. */
425 for (mapinfo_p2 = mapinfos, j = 0; j < num; j++, mapinfo_p2++)
426 {
427 if (mapinfo_p2->vaddr != mapinfo_p->vaddr
428 && mapinfo_p2->ino == mapinfo_p->ino
429 && mapinfo_p2->dev == mapinfo_p->dev)
430 {
431 map.info.vaddr = mapinfo_p2->vaddr;
432 err =
433 devctl (ctl_fd, DCMD_PROC_MAPDEBUG, &map, sizeof (map), 0);
434 if (err != EOK)
435 continue;
436
437 if (strcmp (map.info.path, printme.name))
438 continue;
439
440 /* Lower debug_vaddr is always text, if nessessary, swap. */
441 if ((int) map.info.vaddr < (int) printme.text.debug_vaddr)
442 {
443 memcpy (&(printme.data), &(printme.text),
444 sizeof (printme.data));
445 printme.text.addr = mapinfo_p2->vaddr;
446 printme.text.size = mapinfo_p2->size;
447 printme.text.flags = mapinfo_p2->flags;
448 printme.text.offset = mapinfo_p2->offset;
449 printme.text.debug_vaddr = map.info.vaddr;
450 }
451 else
452 {
453 printme.data.addr = mapinfo_p2->vaddr;
454 printme.data.size = mapinfo_p2->size;
455 printme.data.flags = mapinfo_p2->flags;
456 printme.data.offset = mapinfo_p2->offset;
457 printme.data.debug_vaddr = map.info.vaddr;
458 }
459 mapinfo_p2->ino = 0;
460 }
461 }
462 mapinfo_p->ino = 0;
463
464 printf_filtered ("%s\n", printme.name);
465 printf_filtered ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
466 printme.text.addr);
467 printf_filtered ("\t\tflags=%08x\n", printme.text.flags);
468 printf_filtered ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
469 printf_filtered ("\t\toffset=%016llx\n", printme.text.offset);
470 if (printme.data.size)
471 {
472 printf_filtered ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
473 printme.data.addr);
474 printf_filtered ("\t\tflags=%08x\n", printme.data.flags);
475 printf_filtered ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
476 printf_filtered ("\t\toffset=%016llx\n", printme.data.offset);
477 }
478 printf_filtered ("\tdev=0x%x\n", printme.dev);
479 printf_filtered ("\tino=0x%x\n", (unsigned int) printme.ino);
480 }
481 xfree (mapinfos);
482 return;
483 }
484
485 /* Print status information about what we're accessing. */
486 static void
487 procfs_files_info (struct target_ops *ignore)
488 {
489 printf_unfiltered ("\tUsing the running image of %s %s via %s.\n",
490 attach_flag ? "attached" : "child",
491 target_pid_to_str (inferior_ptid), nto_procfs_path);
492 }
493
494 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
495 static int
496 procfs_can_run ()
497 {
498 return 1;
499 }
500
501 /* Attach to process PID, then initialize for debugging it. */
502 static void
503 procfs_attach (char *args, int from_tty)
504 {
505 char *exec_file;
506 int pid;
507
508 if (!args)
509 error_no_arg ("process-id to attach");
510
511 pid = atoi (args);
512
513 if (pid == getpid ())
514 error ("Attaching GDB to itself is not a good idea...");
515
516 if (from_tty)
517 {
518 exec_file = (char *) get_exec_file (0);
519
520 if (exec_file)
521 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file,
522 target_pid_to_str (pid_to_ptid (pid)));
523 else
524 printf_unfiltered ("Attaching to %s\n",
525 target_pid_to_str (pid_to_ptid (pid)));
526
527 gdb_flush (gdb_stdout);
528 }
529 inferior_ptid = do_attach (pid_to_ptid (pid));
530 push_target (&procfs_ops);
531 }
532
533 static void
534 procfs_post_attach (pid_t pid)
535 {
536 #ifdef SOLIB_CREATE_INFERIOR_HOOK
537 if (exec_bfd)
538 SOLIB_CREATE_INFERIOR_HOOK (pid);
539 #endif
540 }
541
542 static ptid_t
543 do_attach (ptid_t ptid)
544 {
545 procfs_status status;
546 struct sigevent event;
547 char path[100];
548
549 sprintf (path, "%s/%d/as", nto_procfs_path, PIDGET (ptid));
550 ctl_fd = open (path, O_RDWR);
551 if (ctl_fd == -1)
552 error ("Couldn't open proc file %s, error %d (%s)", path, errno,
553 strerror (errno));
554 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
555 error ("Couldn't stop process");
556
557 /* Define a sigevent for process stopped notification. */
558 event.sigev_notify = SIGEV_SIGNAL_THREAD;
559 event.sigev_signo = SIGUSR1;
560 event.sigev_code = 0;
561 event.sigev_value.sival_ptr = NULL;
562 event.sigev_priority = -1;
563 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
564
565 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
566 && status.flags & _DEBUG_FLAG_STOPPED)
567 SignalKill (nto_node(), PIDGET (ptid), 0, SIGCONT, 0, 0);
568 attach_flag = 1;
569 nto_init_solib_absolute_prefix ();
570 return ptid;
571 }
572
573 /* Ask the user what to do when an interrupt is received. */
574 static void
575 interrupt_query ()
576 {
577 target_terminal_ours ();
578
579 if (query ("Interrupted while waiting for the program.\n\
580 Give up (and stop debugging it)? "))
581 {
582 target_mourn_inferior ();
583 throw_exception (RETURN_QUIT);
584 }
585
586 target_terminal_inferior ();
587 }
588
589 /* The user typed ^C twice. */
590 static void
591 nto_interrupt_twice (int signo)
592 {
593 signal (signo, ofunc);
594 interrupt_query ();
595 signal (signo, nto_interrupt_twice);
596 }
597
598 static void
599 nto_interrupt (int signo)
600 {
601 /* If this doesn't work, try more severe steps. */
602 signal (signo, nto_interrupt_twice);
603
604 target_stop ();
605 }
606
607 static ptid_t
608 procfs_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
609 {
610 sigset_t set;
611 siginfo_t info;
612 procfs_status status;
613 static int exit_signo = 0; /* To track signals that cause termination. */
614
615 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
616
617 if (ptid_equal (inferior_ptid, null_ptid))
618 {
619 ourstatus->kind = TARGET_WAITKIND_STOPPED;
620 ourstatus->value.sig = TARGET_SIGNAL_0;
621 exit_signo = 0;
622 return null_ptid;
623 }
624
625 sigemptyset (&set);
626 sigaddset (&set, SIGUSR1);
627
628 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
629 while (!(status.flags & _DEBUG_FLAG_ISTOP))
630 {
631 ofunc = (void (*)()) signal (SIGINT, nto_interrupt);
632 sigwaitinfo (&set, &info);
633 signal (SIGINT, ofunc);
634 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
635 }
636
637 if (status.flags & _DEBUG_FLAG_SSTEP)
638 {
639 ourstatus->kind = TARGET_WAITKIND_STOPPED;
640 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
641 }
642 /* Was it a breakpoint? */
643 else if (status.flags & _DEBUG_FLAG_TRACE)
644 {
645 ourstatus->kind = TARGET_WAITKIND_STOPPED;
646 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
647 }
648 else if (status.flags & _DEBUG_FLAG_ISTOP)
649 {
650 switch (status.why)
651 {
652 case _DEBUG_WHY_SIGNALLED:
653 ourstatus->kind = TARGET_WAITKIND_STOPPED;
654 ourstatus->value.sig =
655 target_signal_from_host (status.info.si_signo);
656 exit_signo = 0;
657 break;
658 case _DEBUG_WHY_FAULTED:
659 ourstatus->kind = TARGET_WAITKIND_STOPPED;
660 if (status.info.si_signo == SIGTRAP)
661 {
662 ourstatus->value.sig = 0;
663 exit_signo = 0;
664 }
665 else
666 {
667 ourstatus->value.sig =
668 target_signal_from_host (status.info.si_signo);
669 exit_signo = ourstatus->value.sig;
670 }
671 break;
672
673 case _DEBUG_WHY_TERMINATED:
674 {
675 int waitval = 0;
676
677 waitpid (PIDGET (inferior_ptid), &waitval, WNOHANG);
678 if (exit_signo)
679 {
680 /* Abnormal death. */
681 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
682 ourstatus->value.sig = exit_signo;
683 }
684 else
685 {
686 /* Normal death. */
687 ourstatus->kind = TARGET_WAITKIND_EXITED;
688 ourstatus->value.integer = WEXITSTATUS (waitval);
689 }
690 exit_signo = 0;
691 break;
692 }
693
694 case _DEBUG_WHY_REQUESTED:
695 /* We are assuming a requested stop is due to a SIGINT. */
696 ourstatus->kind = TARGET_WAITKIND_STOPPED;
697 ourstatus->value.sig = TARGET_SIGNAL_INT;
698 exit_signo = 0;
699 break;
700 }
701 }
702
703 return inferior_ptid;
704 }
705
706 /* Read the current values of the inferior's registers, both the
707 general register set and floating point registers (if supported)
708 and update gdb's idea of their current values. */
709 static void
710 procfs_fetch_registers (int regno)
711 {
712 union
713 {
714 procfs_greg greg;
715 procfs_fpreg fpreg;
716 procfs_altreg altreg;
717 }
718 reg;
719 int regsize;
720
721 procfs_set_thread (inferior_ptid);
722 if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
723 nto_supply_gregset ((char *) &reg.greg);
724 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
725 == EOK)
726 nto_supply_fpregset ((char *) &reg.fpreg);
727 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, &reg, sizeof (reg), &regsize)
728 == EOK)
729 nto_supply_altregset ((char *) &reg.altreg);
730 }
731
732 /* Copy LEN bytes to/from inferior's memory starting at MEMADDR
733 from/to debugger memory starting at MYADDR. Copy from inferior
734 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
735
736 Returns the length copied, which is either the LEN argument or
737 zero. This xfer function does not do partial moves, since procfs_ops
738 doesn't allow memory operations to cross below us in the target stack
739 anyway. */
740 static int
741 procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
742 struct mem_attrib *attrib, struct target_ops *target)
743 {
744 int nbytes = 0;
745
746 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
747 {
748 if (dowrite)
749 nbytes = write (ctl_fd, myaddr, len);
750 else
751 nbytes = read (ctl_fd, myaddr, len);
752 if (nbytes < 0)
753 nbytes = 0;
754 }
755 return (nbytes);
756 }
757
758 /* Take a program previously attached to and detaches it.
759 The program resumes execution and will no longer stop
760 on signals, etc. We'd better not have left any breakpoints
761 in the program or it'll die when it hits one. */
762 static void
763 procfs_detach (char *args, int from_tty)
764 {
765 int siggnal = 0;
766
767 if (from_tty)
768 {
769 char *exec_file = get_exec_file (0);
770 if (exec_file == 0)
771 exec_file = "";
772 printf_unfiltered ("Detaching from program: %s %s\n",
773 exec_file, target_pid_to_str (inferior_ptid));
774 gdb_flush (gdb_stdout);
775 }
776 if (args)
777 siggnal = atoi (args);
778
779 if (siggnal)
780 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, siggnal, 0, 0);
781
782 close (ctl_fd);
783 ctl_fd = -1;
784 init_thread_list ();
785 inferior_ptid = null_ptid;
786 attach_flag = 0;
787 unpush_target (&procfs_ops); /* Pop out of handling an inferior. */
788 }
789
790 static int
791 procfs_breakpoint (CORE_ADDR addr, int type, int size)
792 {
793 procfs_break brk;
794
795 brk.type = type;
796 brk.addr = addr;
797 brk.size = size;
798 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
799 if (errno != EOK)
800 return 1;
801 return 0;
802 }
803
804 static int
805 procfs_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
806 {
807 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, 0);
808 }
809
810 static int
811 procfs_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
812 {
813 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC, -1);
814 }
815
816 static int
817 procfs_insert_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
818 {
819 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
820 }
821
822 static int
823 procfs_remove_hw_breakpoint (CORE_ADDR addr, char *contents_cache)
824 {
825 return procfs_breakpoint (addr, _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
826 }
827
828 static void
829 procfs_resume (ptid_t ptid, int step, enum target_signal signo)
830 {
831 int signal_to_pass;
832 procfs_status status;
833
834 if (ptid_equal (inferior_ptid, null_ptid))
835 return;
836
837 procfs_set_thread (ptid_equal (ptid, minus_one_ptid) ? inferior_ptid :
838 ptid);
839
840 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
841 if (step)
842 run.flags |= _DEBUG_RUN_STEP;
843
844 sigemptyset ((sigset_t *) &run.fault);
845 sigaddset ((sigset_t *) &run.fault, FLTBPT);
846 sigaddset ((sigset_t *) &run.fault, FLTTRACE);
847 sigaddset ((sigset_t *) &run.fault, FLTILL);
848 sigaddset ((sigset_t *) &run.fault, FLTPRIV);
849 sigaddset ((sigset_t *) &run.fault, FLTBOUNDS);
850 sigaddset ((sigset_t *) &run.fault, FLTIOVF);
851 sigaddset ((sigset_t *) &run.fault, FLTIZDIV);
852 sigaddset ((sigset_t *) &run.fault, FLTFPE);
853 /* Peter V will be changing this at some point. */
854 sigaddset ((sigset_t *) &run.fault, FLTPAGE);
855
856 run.flags |= _DEBUG_RUN_ARM;
857
858 sigemptyset (&run.trace);
859 notice_signals ();
860 signal_to_pass = target_signal_to_host (signo);
861
862 if (signal_to_pass)
863 {
864 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
865 signal_to_pass = target_signal_to_host (signo);
866 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
867 {
868 if (signal_to_pass != status.info.si_signo)
869 {
870 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, signal_to_pass,
871 0, 0);
872 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
873 }
874 else /* Let it kill the program without telling us. */
875 sigdelset (&run.trace, signal_to_pass);
876 }
877 }
878 else
879 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
880
881 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
882 if (errno != EOK)
883 {
884 perror ("run error!\n");
885 return;
886 }
887 }
888
889 static void
890 procfs_mourn_inferior ()
891 {
892 if (!ptid_equal (inferior_ptid, null_ptid))
893 {
894 SignalKill (nto_node(), PIDGET (inferior_ptid), 0, SIGKILL, 0, 0);
895 close (ctl_fd);
896 }
897 inferior_ptid = null_ptid;
898 init_thread_list ();
899 unpush_target (&procfs_ops);
900 generic_mourn_inferior ();
901 attach_flag = 0;
902 }
903
904 /* This function breaks up an argument string into an argument
905 vector suitable for passing to execvp().
906 E.g., on "run a b c d" this routine would get as input
907 the string "a b c d", and as output it would fill in argv with
908 the four arguments "a", "b", "c", "d". The only additional
909 functionality is simple quoting. The gdb command:
910 run a "b c d" f
911 will fill in argv with the three args "a", "b c d", "e". */
912 static void
913 breakup_args (char *scratch, char **argv)
914 {
915 char *pp, *cp = scratch;
916 char quoting = 0;
917
918 for (;;)
919 {
920 /* Scan past leading separators. */
921 quoting = 0;
922 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
923 cp++;
924
925 /* Break if at end of string. */
926 if (*cp == '\0')
927 break;
928
929 /* Take an arg. */
930 if (*cp == '"')
931 {
932 cp++;
933 quoting = strchr (cp, '"') ? 1 : 0;
934 }
935
936 *argv++ = cp;
937
938 /* Scan for next arg separator. */
939 pp = cp;
940 if (quoting)
941 cp = strchr (pp, '"');
942 if ((cp == NULL) || (!quoting))
943 cp = strchr (pp, ' ');
944 if (cp == NULL)
945 cp = strchr (pp, '\t');
946 if (cp == NULL)
947 cp = strchr (pp, '\n');
948
949 /* No separators => end of string => break. */
950 if (cp == NULL)
951 {
952 pp = cp;
953 break;
954 }
955
956 /* Replace the separator with a terminator. */
957 *cp++ = '\0';
958 }
959
960 /* Execv requires a null-terminated arg vector. */
961 *argv = NULL;
962 }
963
964 static void
965 procfs_create_inferior (char *exec_file, char *allargs, char **env)
966 {
967 struct inheritance inherit;
968 pid_t pid;
969 int flags, errn;
970 char **argv, *args;
971 char *in = "", *out = "", *err = "";
972 int fd, fds[3];
973 sigset_t set;
974
975 argv = xmalloc (((strlen (allargs) + 1) / (unsigned) 2 + 2) *
976 sizeof (*argv));
977 argv[0] = get_exec_file (1);
978 if (!argv[0])
979 {
980 if (exec_file)
981 argv[0] = exec_file;
982 else
983 return;
984 }
985
986 args = xstrdup (allargs);
987 breakup_args (args, exec_file ? &argv[1] : &argv[0]);
988
989 argv = nto_parse_redirection (argv, &in, &out, &err);
990
991 fds[0] = STDIN_FILENO;
992 fds[1] = STDOUT_FILENO;
993 fds[2] = STDERR_FILENO;
994
995 /* If the user specified I/O via gdb's --tty= arg, use it, but only
996 if the i/o is not also being specified via redirection. */
997 if (inferior_io_terminal)
998 {
999 if (!in[0])
1000 in = inferior_io_terminal;
1001 if (!out[0])
1002 out = inferior_io_terminal;
1003 if (!err[0])
1004 err = inferior_io_terminal;
1005 }
1006
1007 if (in[0])
1008 {
1009 fd = open (in, O_RDONLY);
1010 if (fd == -1)
1011 perror (in);
1012 else
1013 fds[0] = fd;
1014 }
1015 if (out[0])
1016 {
1017 fd = open (out, O_WRONLY);
1018 if (fd == -1)
1019 perror (out);
1020 else
1021 fds[1] = fd;
1022 }
1023 if (err[0])
1024 {
1025 fd = open (err, O_WRONLY);
1026 if (fd == -1)
1027 perror (err);
1028 else
1029 fds[2] = fd;
1030 }
1031
1032 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1033 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1034
1035 sigemptyset (&set);
1036 sigaddset (&set, SIGUSR1);
1037 sigprocmask (SIG_UNBLOCK, &set, NULL);
1038
1039 memset (&inherit, 0, sizeof (inherit));
1040
1041 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1042 {
1043 inherit.nd = nto_node();
1044 inherit.flags |= SPAWN_SETND;
1045 inherit.flags &= ~SPAWN_EXEC;
1046 }
1047 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1048 inherit.pgroup = SPAWN_NEWPGROUP;
1049 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1050 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1051 xfree (args);
1052
1053 sigprocmask (SIG_BLOCK, &set, NULL);
1054
1055 if (pid == -1)
1056 error ("Error spawning %s: %d (%s)", argv[0], errno, strerror (errno));
1057
1058 if (fds[0] != STDIN_FILENO)
1059 close (fds[0]);
1060 if (fds[1] != STDOUT_FILENO)
1061 close (fds[1]);
1062 if (fds[2] != STDERR_FILENO)
1063 close (fds[2]);
1064
1065 inferior_ptid = do_attach (pid_to_ptid (pid));
1066
1067 attach_flag = 0;
1068 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1069 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1070 if (errn != EOK)
1071 {
1072 /* FIXME: expected warning? */
1073 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
1074 errn, strerror(errn) ); */
1075 }
1076 push_target (&procfs_ops);
1077 target_terminal_init ();
1078
1079 #ifdef SOLIB_CREATE_INFERIOR_HOOK
1080 if (exec_bfd != NULL
1081 || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
1082 SOLIB_CREATE_INFERIOR_HOOK (pid);
1083 #endif
1084 }
1085
1086 static void
1087 procfs_stop ()
1088 {
1089 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1090 }
1091
1092 static void
1093 procfs_kill_inferior ()
1094 {
1095 target_mourn_inferior ();
1096 }
1097
1098 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1099 of REGISTERS. */
1100 static void
1101 procfs_prepare_to_store ()
1102 {
1103 }
1104
1105 /* Fill buf with regset and return devctl cmd to do the setting. Return
1106 -1 if we fail to get the regset. Store size of regset in regsize. */
1107 static int
1108 get_regset (int regset, char *buf, int bufsize, int *regsize)
1109 {
1110 int dev_get, dev_set;
1111 switch (regset)
1112 {
1113 case NTO_REG_GENERAL:
1114 dev_get = DCMD_PROC_GETGREG;
1115 dev_set = DCMD_PROC_SETGREG;
1116 break;
1117
1118 case NTO_REG_FLOAT:
1119 dev_get = DCMD_PROC_GETFPREG;
1120 dev_set = DCMD_PROC_SETFPREG;
1121 break;
1122
1123 case NTO_REG_ALT:
1124 dev_get = DCMD_PROC_GETALTREG;
1125 dev_set = DCMD_PROC_SETALTREG;
1126 break;
1127
1128 case NTO_REG_SYSTEM:
1129 default:
1130 return -1;
1131 }
1132 if (devctl (ctl_fd, dev_get, &buf, bufsize, regsize) != EOK)
1133 return -1;
1134
1135 return dev_set;
1136 }
1137
1138 void
1139 procfs_store_registers (int regno)
1140 {
1141 union
1142 {
1143 procfs_greg greg;
1144 procfs_fpreg fpreg;
1145 procfs_altreg altreg;
1146 }
1147 reg;
1148 unsigned off;
1149 int len, regset, regsize, dev_set, err;
1150 char *data;
1151
1152 if (ptid_equal (inferior_ptid, null_ptid))
1153 return;
1154 procfs_set_thread (inferior_ptid);
1155
1156 if (regno == -1)
1157 {
1158 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1159 {
1160 dev_set = get_regset (regset, (char *) &reg,
1161 sizeof (reg), &regsize);
1162 if (dev_set == -1)
1163 continue;
1164
1165 if (nto_regset_fill (regset, (char *) &reg) == -1)
1166 continue;
1167
1168 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1169 if (err != EOK)
1170 fprintf_unfiltered (gdb_stderr,
1171 "Warning unable to write regset %d: %s\n",
1172 regno, strerror (err));
1173 }
1174 }
1175 else
1176 {
1177 regset = nto_regset_id (regno);
1178 if (regset == -1)
1179 return;
1180
1181 dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
1182 if (dev_set == -1)
1183 return;
1184
1185 len = nto_register_area (regno, regset, &off);
1186
1187 if (len < 1)
1188 return;
1189
1190 regcache_collect (regno, (char *) &reg + off);
1191
1192 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1193 if (err != EOK)
1194 fprintf_unfiltered (gdb_stderr,
1195 "Warning unable to write regset %d: %s\n", regno,
1196 strerror (err));
1197 }
1198 }
1199
1200 static void
1201 notice_signals (void)
1202 {
1203 int signo;
1204
1205 for (signo = 1; signo < NSIG; signo++)
1206 {
1207 if (signal_stop_state (target_signal_from_host (signo)) == 0
1208 && signal_print_state (target_signal_from_host (signo)) == 0
1209 && signal_pass_state (target_signal_from_host (signo)) == 1)
1210 sigdelset (&run.trace, signo);
1211 else
1212 sigaddset (&run.trace, signo);
1213 }
1214 }
1215
1216 /* When the user changes the state of gdb's signal handling via the
1217 "handle" command, this function gets called to see if any change
1218 in the /proc interface is required. It is also called internally
1219 by other /proc interface functions to initialize the state of
1220 the traced signal set. */
1221 static void
1222 procfs_notice_signals (ptid_t ptid)
1223 {
1224 sigemptyset (&run.trace);
1225 notice_signals ();
1226 }
1227
1228 static struct tidinfo *
1229 procfs_thread_info (pid_t pid, short tid)
1230 {
1231 /* NYI */
1232 return NULL;
1233 }
1234
1235 char *
1236 procfs_pid_to_str (ptid_t ptid)
1237 {
1238 static char buf[1024];
1239 int pid, tid, n;
1240 struct tidinfo *tip;
1241
1242 pid = ptid_get_pid (ptid);
1243 tid = ptid_get_tid (ptid);
1244
1245 n = sprintf (buf, "process %d", pid);
1246
1247 #if 0 /* NYI */
1248 tip = procfs_thread_info (pid, tid);
1249 if (tip != NULL)
1250 sprintf (&buf[n], " (state = 0x%02x)", tip->state);
1251 #endif
1252
1253 return buf;
1254 }
1255
1256 static void
1257 init_procfs_ops ()
1258 {
1259 procfs_ops.to_shortname = "procfs";
1260 procfs_ops.to_longname = "QNX Neutrino procfs child process";
1261 procfs_ops.to_doc =
1262 "QNX Neutrino procfs child process (started by the \"run\" command).\n\
1263 target procfs <node>";
1264 procfs_ops.to_open = procfs_open;
1265 procfs_ops.to_attach = procfs_attach;
1266 procfs_ops.to_post_attach = procfs_post_attach;
1267 procfs_ops.to_detach = procfs_detach;
1268 procfs_ops.to_resume = procfs_resume;
1269 procfs_ops.to_wait = procfs_wait;
1270 procfs_ops.to_fetch_registers = procfs_fetch_registers;
1271 procfs_ops.to_store_registers = procfs_store_registers;
1272 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
1273 procfs_ops.to_xfer_memory = procfs_xfer_memory;
1274 procfs_ops.to_files_info = procfs_files_info;
1275 procfs_ops.to_insert_breakpoint = procfs_insert_breakpoint;
1276 procfs_ops.to_remove_breakpoint = procfs_remove_breakpoint;
1277 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
1278 procfs_ops.to_insert_hw_breakpoint = procfs_insert_hw_breakpoint;
1279 procfs_ops.to_remove_hw_breakpoint = procfs_remove_breakpoint;
1280 procfs_ops.to_insert_watchpoint = procfs_insert_hw_watchpoint;
1281 procfs_ops.to_remove_watchpoint = procfs_remove_hw_watchpoint;
1282 procfs_ops.to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
1283 procfs_ops.to_terminal_init = terminal_init_inferior;
1284 procfs_ops.to_terminal_inferior = terminal_inferior;
1285 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
1286 procfs_ops.to_terminal_ours = terminal_ours;
1287 procfs_ops.to_terminal_info = child_terminal_info;
1288 procfs_ops.to_kill = procfs_kill_inferior;
1289 procfs_ops.to_create_inferior = procfs_create_inferior;
1290 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
1291 procfs_ops.to_can_run = procfs_can_run;
1292 procfs_ops.to_notice_signals = procfs_notice_signals;
1293 procfs_ops.to_thread_alive = procfs_thread_alive;
1294 procfs_ops.to_find_new_threads = procfs_find_new_threads;
1295 procfs_ops.to_pid_to_str = procfs_pid_to_str;
1296 procfs_ops.to_stop = procfs_stop;
1297 procfs_ops.to_stratum = process_stratum;
1298 procfs_ops.to_has_all_memory = 1;
1299 procfs_ops.to_has_memory = 1;
1300 procfs_ops.to_has_stack = 1;
1301 procfs_ops.to_has_registers = 1;
1302 procfs_ops.to_has_execution = 1;
1303 procfs_ops.to_magic = OPS_MAGIC;
1304 procfs_ops.to_have_continuable_watchpoint = 1;
1305 }
1306
1307 #define OSTYPE_NTO 1
1308
1309 void
1310 _initialize_procfs ()
1311 {
1312 sigset_t set;
1313
1314 init_procfs_ops ();
1315 add_target (&procfs_ops);
1316
1317 /* We use SIGUSR1 to gain control after we block waiting for a process.
1318 We use sigwaitevent to wait. */
1319 sigemptyset (&set);
1320 sigaddset (&set, SIGUSR1);
1321 sigprocmask (SIG_BLOCK, &set, NULL);
1322
1323 /* Set up trace and fault sets, as gdb expects them. */
1324 sigemptyset (&run.trace);
1325 notice_signals ();
1326
1327 /* Stuff some information. */
1328 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1329 nto_cpuinfo_valid = 1;
1330
1331 add_info ("pidlist", procfs_pidlist, "pidlist");
1332 add_info ("meminfo", procfs_meminfo, "memory information");
1333 }
1334
1335
1336 static int
1337 procfs_hw_watchpoint (int addr, int len, int type)
1338 {
1339 procfs_break brk;
1340
1341 switch (type)
1342 {
1343 case 1: /* Read. */
1344 brk.type = _DEBUG_BREAK_RD;
1345 break;
1346 case 2: /* Read/Write. */
1347 brk.type = _DEBUG_BREAK_RW;
1348 break;
1349 default: /* Modify. */
1350 /* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1351 brk.type = _DEBUG_BREAK_RW;
1352 }
1353 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1354 brk.addr = addr;
1355 brk.size = len;
1356
1357 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1358 if (errno != EOK)
1359 {
1360 perror ("Failed to set hardware watchpoint");
1361 return -1;
1362 }
1363 return 0;
1364 }
1365
1366 static int
1367 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
1368 {
1369 return 1;
1370 }
1371
1372 static int
1373 procfs_remove_hw_watchpoint (CORE_ADDR addr, int len, int type)
1374 {
1375 return procfs_hw_watchpoint (addr, -1, type);
1376 }
1377
1378 static int
1379 procfs_insert_hw_watchpoint (CORE_ADDR addr, int len, int type)
1380 {
1381 return procfs_hw_watchpoint (addr, len, type);
1382 }
1383
1384 static int
1385 procfs_stopped_by_watchpoint (void)
1386 {
1387 return 0;
1388 }