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