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