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