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