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