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