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