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