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