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