]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/nto-procfs.c
PR31692, objdump fails .debug_info size check
[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
1d506c26 4 Copyright (C) 2003-2024 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 22
61bb466e
KW
23
24#include <fcntl.h>
25#include <spawn.h>
26#include <sys/debug.h>
27#include <sys/procfs.h>
28#include <sys/neutrino.h>
29#include <sys/syspage.h>
2978b111 30#include <dirent.h>
61bb466e 31#include <sys/netmgr.h>
8a6c0ccd
AR
32#include <sys/auxv.h>
33
61bb466e
KW
34#include "gdbcore.h"
35#include "inferior.h"
36#include "target.h"
37#include "objfiles.h"
38#include "gdbthread.h"
39#include "nto-tdep.h"
40#include "command.h"
41#include "regcache.h"
5ea03926 42#include "solib.h"
ee8e9165 43#include "inf-child.h"
268a13a5
TT
44#include "gdbsupport/filestuff.h"
45#include "gdbsupport/scoped_fd.h"
61bb466e
KW
46
47#define NULL_PID 0
48#define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
49 _DEBUG_FLAG_TRACE_WR|_DEBUG_FLAG_TRACE_MODIFY)
50
61bb466e
KW
51int ctl_fd;
52
a40805d4 53static sighandler_t ofunc;
61bb466e
KW
54
55static procfs_run run;
56
f6ac5f3d
PA
57/* Create the "native" and "procfs" targets. */
58
59struct nto_procfs_target : public inf_child_target
60{
61 void open (const char *arg, int from_tty) override;
62
63 void attach (const char *, int) override = 0;
64
65 void post_attach (int);
66
67 void detach (inferior *, int) override;
68
69 void resume (ptid_t, int, enum gdb_signal) override;
70
b60cea74 71 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
f6ac5f3d
PA
72
73 void fetch_registers (struct regcache *, int) override;
74 void store_registers (struct regcache *, int) override;
75
76 enum target_xfer_status xfer_partial (enum target_object object,
77 const char *annex,
78 gdb_byte *readbuf,
79 const gdb_byte *writebuf,
80 ULONGEST offset, ULONGEST len,
81 ULONGEST *xfered_len) override;
82
83 void files_info () override;
84
85 int insert_breakpoint (struct gdbarch *, struct bp_target_info *) override;
86
87 int remove_breakpoint (struct gdbarch *, struct bp_target_info *,
88 enum remove_bp_reason) override;
89
90 int can_use_hw_breakpoint (enum bptype, int, int) override;
91
92 int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
93
94 int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *) override;
95
96 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
97 struct expression *) override;
98
99 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
100 struct expression *) override;
101
57810aa7 102 bool stopped_by_watchpoint () override;
f6ac5f3d
PA
103
104 void kill () override;
105
106 void create_inferior (const char *, const std::string &,
107 char **, int) override;
108
109 void mourn_inferior () override;
61bb466e 110
adc6a863 111 void pass_signals (gdb::array_view<const unsigned char>) override;
61bb466e 112
57810aa7 113 bool thread_alive (ptid_t ptid) override;
61bb466e 114
f6ac5f3d 115 void update_thread_list () override;
61bb466e 116
a068643d 117 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
118
119 void interrupt () override;
120
f6ac5f3d
PA
121 const char *extra_thread_info (struct thread_info *) override;
122
0e90c441 123 const char *pid_to_exec_file (int pid) override;
f6ac5f3d
PA
124};
125
126/* For "target native". */
f6ac5f3d 127
d9f719f1
PA
128static const target_info nto_native_target_info = {
129 "native",
130 N_("QNX Neutrino local process"),
131 N_("QNX Neutrino local process (started by the \"run\" command).")
132};
f6ac5f3d 133
d9f719f1
PA
134class nto_procfs_target_native final : public nto_procfs_target
135{
136 const target_info &info () const override
137 { return nto_native_target_info; }
f6ac5f3d
PA
138};
139
140/* For "target procfs <node>". */
f6ac5f3d 141
d9f719f1
PA
142static const target_info nto_procfs_target_info = {
143 "procfs",
144 N_("QNX Neutrino local or remote process"),
65e65158 145 N_("QNX Neutrino process. target procfs NODE")
d9f719f1 146};
f6ac5f3d 147
d9f719f1
PA
148struct nto_procfs_target_procfs final : public nto_procfs_target
149{
150 const target_info &info () const override
151 { return nto_procfs_target_info; }
f6ac5f3d
PA
152};
153
154static ptid_t do_attach (ptid_t ptid);
61bb466e 155
6a3cb8e8 156/* These two globals are only ever set in procfs_open_1, but are
61bb466e
KW
157 referenced elsewhere. 'nto_procfs_node' is a flag used to say
158 whether we are local, or we should get the current node descriptor
159 for the remote QNX node. */
609c3040 160static char *nodestr;
61bb466e
KW
161static unsigned nto_procfs_node = ND_LOCAL_NODE;
162
163/* Return the current QNX Node, or error out. This is a simple
164 wrapper for the netmgr_strtond() function. The reason this
165 is required is because QNX node descriptors are transient so
166 we have to re-acquire them every time. */
167static unsigned
d737fd7f 168nto_node (void)
61bb466e
KW
169{
170 unsigned node;
171
609c3040
AR
172 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0
173 || nodestr == NULL)
61bb466e
KW
174 return ND_LOCAL_NODE;
175
609c3040 176 node = netmgr_strtond (nodestr, 0);
61bb466e 177 if (node == -1)
8a3fe4f8 178 error (_("Lost the QNX node. Debug session probably over."));
61bb466e
KW
179
180 return (node);
181}
182
d737fd7f
KW
183static enum gdb_osabi
184procfs_is_nto_target (bfd *abfd)
185{
186 return GDB_OSABI_QNXNTO;
187}
188
6a3cb8e8
PA
189/* This is called when we call 'target native' or 'target procfs
190 <arg>' from the (gdb) prompt. For QNX6 (nto), the only valid arg
191 will be a QNX node string, eg: "/net/some_node". If arg is not a
192 valid QNX node, we will default to local. */
f6ac5f3d
PA
193void
194nto_procfs_target::open (const char *arg, int from_tty)
61bb466e 195{
61bb466e
KW
196 char *endstr;
197 char buffer[50];
5dc1a704 198 int total_size;
61bb466e 199 procfs_sysinfo *sysinfo;
609c3040 200 char nto_procfs_path[PATH_MAX];
61bb466e 201
6a3cb8e8
PA
202 /* Offer to kill previous inferiors before opening this target. */
203 target_preopen (from_tty);
204
d737fd7f
KW
205 nto_is_nto_target = procfs_is_nto_target;
206
61bb466e
KW
207 /* Set the default node used for spawning to this one,
208 and only override it if there is a valid arg. */
209
609c3040
AR
210 xfree (nodestr);
211 nodestr = NULL;
212
61bb466e 213 nto_procfs_node = ND_LOCAL_NODE;
609c3040 214 nodestr = (arg != NULL) ? xstrdup (arg) : NULL;
61bb466e 215
61bb466e
KW
216 if (nodestr)
217 {
218 nto_procfs_node = netmgr_strtond (nodestr, &endstr);
219 if (nto_procfs_node == -1)
220 {
221 if (errno == ENOTSUP)
6cb06a8c
TT
222 gdb_printf ("QNX Net Manager not found.\n");
223 gdb_printf ("Invalid QNX node %s: error %d (%s).\n", nodestr,
224 errno, safe_strerror (errno));
61bb466e
KW
225 xfree (nodestr);
226 nodestr = NULL;
227 nto_procfs_node = ND_LOCAL_NODE;
228 }
229 else if (*endstr)
230 {
231 if (*(endstr - 1) == '/')
232 *(endstr - 1) = 0;
233 else
234 *endstr = 0;
235 }
236 }
609c3040
AR
237 snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
238 (nodestr != NULL) ? nodestr : "", "/proc");
61bb466e 239
5dc1a704
TT
240 scoped_fd fd (open (nto_procfs_path, O_RDONLY));
241 if (fd.get () == -1)
61bb466e 242 {
6cb06a8c
TT
243 gdb_printf ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
244 safe_strerror (errno));
8a3fe4f8 245 error (_("Invalid procfs arg"));
61bb466e
KW
246 }
247
248 sysinfo = (void *) buffer;
5dc1a704 249 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
61bb466e 250 {
6cb06a8c
TT
251 gdb_printf ("Error getting size: %d (%s)\n", errno,
252 safe_strerror (errno));
8a3fe4f8 253 error (_("Devctl failed."));
61bb466e
KW
254 }
255 else
256 {
257 total_size = sysinfo->total_size;
258 sysinfo = alloca (total_size);
609c3040 259 if (sysinfo == NULL)
61bb466e 260 {
6cb06a8c
TT
261 gdb_printf ("Memory error: %d (%s)\n", errno,
262 safe_strerror (errno));
8a3fe4f8 263 error (_("alloca failed."));
61bb466e
KW
264 }
265 else
266 {
5dc1a704
TT
267 if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, total_size, 0)
268 != EOK)
61bb466e 269 {
6cb06a8c
TT
270 gdb_printf ("Error getting sysinfo: %d (%s)\n", errno,
271 safe_strerror (errno));
8a3fe4f8 272 error (_("Devctl failed."));
61bb466e
KW
273 }
274 else
275 {
276 if (sysinfo->type !=
99d9c3b9
SM
277 nto_map_arch_to_cputype
278 (gdbarch_bfd_arch_info
279 (current_inferior ()->arch ())->arch_name))
9fe4a216 280 error (_("Invalid target CPU."));
61bb466e
KW
281 }
282 }
283 }
6a3cb8e8 284
f6ac5f3d 285 inf_child_target::open (arg, from_tty);
6cb06a8c 286 gdb_printf ("Debugging using %s\n", nto_procfs_path);
61bb466e
KW
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
ebe84f23 396 pid = current_inferior ()->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);
9213a6d7 412 new_thread = this->find_thread (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 {
6cb06a8c
TT
440 gdb_printf (gdb_stderr, "failed to opendir \"%s\" - %d (%s)",
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 {
6cb06a8c
TT
467 gdb_printf (gdb_stderr, "failed to open %s - %d (%s)\n",
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 {
6cb06a8c
TT
475 gdb_printf (gdb_stderr,
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);
6cb06a8c 495 gdb_printf ("%s - %d", name, pid);
609c3040 496 if (err == EOK && status->tid != 0)
6cb06a8c 497 gdb_printf ("/%d\n", status->tid);
609c3040
AR
498 else
499 {
6cb06a8c 500 gdb_printf ("\n");
609c3040
AR
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
6cb06a8c
TT
632 gdb_printf ("%s\n", printme.name);
633 gdb_printf ("\ttext=%08x bytes @ 0x%08x\n", printme.text.size,
634 printme.text.addr);
635 gdb_printf ("\t\tflags=%08x\n", printme.text.flags);
636 gdb_printf ("\t\tdebug=%08x\n", printme.text.debug_vaddr);
637 gdb_printf ("\t\toffset=%s\n", phex (printme.text.offset, 8));
61bb466e
KW
638 if (printme.data.size)
639 {
6cb06a8c
TT
640 gdb_printf ("\tdata=%08x bytes @ 0x%08x\n", printme.data.size,
641 printme.data.addr);
642 gdb_printf ("\t\tflags=%08x\n", printme.data.flags);
643 gdb_printf ("\t\tdebug=%08x\n", printme.data.debug_vaddr);
644 gdb_printf ("\t\toffset=%s\n", phex (printme.data.offset, 8));
61bb466e 645 }
6cb06a8c
TT
646 gdb_printf ("\tdev=0x%x\n", printme.dev);
647 gdb_printf ("\tino=0x%x\n", (unsigned int) printme.ino);
61bb466e
KW
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
6cb06a8c
TT
659 gdb_printf ("\tUsing the running image of %s %s via %s.\n",
660 inf->attach_flag ? "attached" : "child",
fb6d30e0 661 target_pid_to_str (ptid_t (inf->pid)).c_str (),
6cb06a8c 662 (nodestr != NULL) ? nodestr : "local node");
61bb466e
KW
663}
664
e5343fde
AR
665/* Target to_pid_to_exec_file implementation. */
666
0e90c441 667const char *
f6ac5f3d 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 703
bc521517 704 target_announce_attach (from_tty, pid);
61bb466e 705
ebe84f23 706 ptid_t ptid = do_attach (ptid_t (pid));
6c95b8df
PA
707 inf = current_inferior ();
708 inferior_appeared (inf, pid);
30220b46 709 inf->attach_flag = true;
7f9f62ba 710
c8fbd44a 711 if (!inf->target_is_pushed (ops))
02980c56 712 inf->push_target (ops);
7f9f62ba 713
ebe84f23
PA
714 update_thread_list ();
715
9213a6d7 716 switch_to_thread (this->find_thread (ptid));
61bb466e
KW
717}
718
f6ac5f3d
PA
719void
720nto_procfs_target::post_attach (pid_t pid)
61bb466e 721{
7e10abd1 722 if (current_program_space->exec_bfd ())
268a4a75 723 solib_create_inferior_hook (0);
61bb466e
KW
724}
725
726static ptid_t
727do_attach (ptid_t ptid)
728{
729 procfs_status status;
730 struct sigevent event;
dc5dd1eb 731 char path[PATH_MAX];
61bb466e 732
609c3040 733 snprintf (path, PATH_MAX - 1, "%s%s/%d/as",
e99b03dc 734 (nodestr != NULL) ? nodestr : "", "/proc", ptid.pid ());
61bb466e
KW
735 ctl_fd = open (path, O_RDWR);
736 if (ctl_fd == -1)
8a3fe4f8 737 error (_("Couldn't open proc file %s, error %d (%s)"), path, errno,
dc5dd1eb 738 safe_strerror (errno));
61bb466e 739 if (devctl (ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0) != EOK)
8a3fe4f8 740 error (_("Couldn't stop process"));
61bb466e
KW
741
742 /* Define a sigevent for process stopped notification. */
743 event.sigev_notify = SIGEV_SIGNAL_THREAD;
744 event.sigev_signo = SIGUSR1;
745 event.sigev_code = 0;
746 event.sigev_value.sival_ptr = NULL;
747 event.sigev_priority = -1;
748 devctl (ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
749
750 if (devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0) == EOK
751 && status.flags & _DEBUG_FLAG_STOPPED)
e99b03dc 752 SignalKill (nto_node (), ptid.pid (), 0, SIGCONT, 0, 0);
61bb466e 753 nto_init_solib_absolute_prefix ();
e99b03dc 754 return ptid_t (ptid.pid (), 0, status.tid);
61bb466e
KW
755}
756
757/* Ask the user what to do when an interrupt is received. */
758static void
dc5dd1eb 759interrupt_query (void)
61bb466e 760{
9e2f0ad4
HZ
761 if (query (_("Interrupted while waiting for the program.\n\
762Give up (and stop debugging it)? ")))
61bb466e 763 {
bc1e6c81 764 target_mourn_inferior (inferior_ptid);
039e3c22 765 quit ();
61bb466e 766 }
61bb466e
KW
767}
768
769/* The user typed ^C twice. */
770static void
bfedc46a 771nto_handle_sigint_twice (int signo)
61bb466e
KW
772{
773 signal (signo, ofunc);
774 interrupt_query ();
bfedc46a 775 signal (signo, nto_handle_sigint_twice);
61bb466e
KW
776}
777
778static void
bfedc46a 779nto_handle_sigint (int signo)
61bb466e
KW
780{
781 /* If this doesn't work, try more severe steps. */
bfedc46a 782 signal (signo, nto_handle_sigint_twice);
61bb466e 783
e671cd59 784 target_interrupt ();
61bb466e
KW
785}
786
f6ac5f3d
PA
787sptid_t
788nto_procfs_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 789 target_wait_flags options)
61bb466e
KW
790{
791 sigset_t set;
792 siginfo_t info;
793 procfs_status status;
794 static int exit_signo = 0; /* To track signals that cause termination. */
795
183be222 796 ourstatus->set_spurious ();
61bb466e 797
d7e15655 798 if (inferior_ptid == null_ptid)
61bb466e 799 {
183be222 800 ourstatus->set_stopped (GDB_SIGNAL_0);
61bb466e
KW
801 exit_signo = 0;
802 return null_ptid;
803 }
804
805 sigemptyset (&set);
806 sigaddset (&set, SIGUSR1);
807
808 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
809 while (!(status.flags & _DEBUG_FLAG_ISTOP))
810 {
a40805d4 811 ofunc = signal (SIGINT, nto_handle_sigint);
61bb466e
KW
812 sigwaitinfo (&set, &info);
813 signal (SIGINT, ofunc);
814 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
815 }
816
a9889169
AR
817 nto_inferior_data (NULL)->stopped_flags = status.flags;
818 nto_inferior_data (NULL)->stopped_pc = status.ip;
819
61bb466e 820 if (status.flags & _DEBUG_FLAG_SSTEP)
183be222 821 ourstatus->set_stopped (GDB_SIGNAL_TRAP);
61bb466e
KW
822 /* Was it a breakpoint? */
823 else if (status.flags & _DEBUG_FLAG_TRACE)
183be222 824 ourstatus->set_stopped (GDB_SIGNAL_TRAP);
61bb466e
KW
825 else if (status.flags & _DEBUG_FLAG_ISTOP)
826 {
827 switch (status.why)
828 {
829 case _DEBUG_WHY_SIGNALLED:
183be222 830 ourstatus->set_stopped (gdb_signal_from_host (status.info.si_signo));
61bb466e
KW
831 exit_signo = 0;
832 break;
833 case _DEBUG_WHY_FAULTED:
61bb466e
KW
834 if (status.info.si_signo == SIGTRAP)
835 {
183be222 836 ourstatus->set_stopped (0);
61bb466e
KW
837 exit_signo = 0;
838 }
839 else
840 {
183be222
SM
841 ourstatus->set_stopped
842 (gdb_signal_from_host (status.info.si_signo));
843 exit_signo = ourstatus->sig ();
61bb466e
KW
844 }
845 break;
846
847 case _DEBUG_WHY_TERMINATED:
848 {
849 int waitval = 0;
850
e99b03dc 851 waitpid (inferior_ptid.pid (), &waitval, WNOHANG);
61bb466e
KW
852 if (exit_signo)
853 {
854 /* Abnormal death. */
183be222 855 ourstatus->set_signalled (exit_signo);
61bb466e
KW
856 }
857 else
858 {
859 /* Normal death. */
183be222 860 ourstatus->set_exited (WEXITSTATUS (waitval));
61bb466e
KW
861 }
862 exit_signo = 0;
863 break;
864 }
865
866 case _DEBUG_WHY_REQUESTED:
867 /* We are assuming a requested stop is due to a SIGINT. */
183be222 868 ourstatus->set_stopped (GDB_SIGNAL_INT);
61bb466e
KW
869 exit_signo = 0;
870 break;
871 }
872 }
873
fd79271b 874 return ptid_t (status.pid, 0, status.tid);
61bb466e
KW
875}
876
877/* Read the current values of the inferior's registers, both the
878 general register set and floating point registers (if supported)
879 and update gdb's idea of their current values. */
f6ac5f3d
PA
880void
881nto_procfs_target::fetch_registers (struct regcache *regcache, int regno)
61bb466e
KW
882{
883 union
884 {
885 procfs_greg greg;
886 procfs_fpreg fpreg;
887 procfs_altreg altreg;
888 }
889 reg;
890 int regsize;
891
222312d3 892 procfs_set_thread (regcache->ptid ());
61bb466e 893 if (devctl (ctl_fd, DCMD_PROC_GETGREG, &reg, sizeof (reg), &regsize) == EOK)
56be3814 894 nto_supply_gregset (regcache, (char *) &reg.greg);
61bb466e
KW
895 if (devctl (ctl_fd, DCMD_PROC_GETFPREG, &reg, sizeof (reg), &regsize)
896 == EOK)
56be3814 897 nto_supply_fpregset (regcache, (char *) &reg.fpreg);
61bb466e
KW
898 if (devctl (ctl_fd, DCMD_PROC_GETALTREG, &reg, sizeof (reg), &regsize)
899 == EOK)
56be3814 900 nto_supply_altregset (regcache, (char *) &reg.altreg);
61bb466e
KW
901}
902
9d46c4e5
PA
903/* Helper for procfs_xfer_partial that handles memory transfers.
904 Arguments are like target_xfer_partial. */
61bb466e 905
9d46c4e5
PA
906static enum target_xfer_status
907procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
908 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
61bb466e 909{
9d46c4e5
PA
910 int nbytes;
911
912 if (lseek (ctl_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
913 return TARGET_XFER_E_IO;
914
915 if (writebuf != NULL)
916 nbytes = write (ctl_fd, writebuf, len);
917 else
918 nbytes = read (ctl_fd, readbuf, len);
919 if (nbytes <= 0)
920 return TARGET_XFER_E_IO;
921 *xfered_len = nbytes;
922 return TARGET_XFER_OK;
923}
924
925/* Target to_xfer_partial implementation. */
61bb466e 926
f6ac5f3d
PA
927enum target_xfer_status
928nto_procfs_target::xfer_partial (enum target_object object,
929 const char *annex, gdb_byte *readbuf,
930 const gdb_byte *writebuf, ULONGEST offset,
931 ULONGEST len, ULONGEST *xfered_len)
9d46c4e5
PA
932{
933 switch (object)
61bb466e 934 {
9d46c4e5
PA
935 case TARGET_OBJECT_MEMORY:
936 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
8a6c0ccd
AR
937 case TARGET_OBJECT_AUXV:
938 if (readbuf != NULL)
939 {
940 int err;
941 CORE_ADDR initial_stack;
942 debug_process_t procinfo;
943 /* For 32-bit architecture, size of auxv_t is 8 bytes. */
944 const unsigned int sizeof_auxv_t = sizeof (auxv_t);
945 const unsigned int sizeof_tempbuf = 20 * sizeof_auxv_t;
946 int tempread;
947 gdb_byte *const tempbuf = alloca (sizeof_tempbuf);
948
949 if (tempbuf == NULL)
950 return TARGET_XFER_E_IO;
951
952 err = devctl (ctl_fd, DCMD_PROC_INFO, &procinfo,
dda83cd7 953 sizeof procinfo, 0);
8a6c0ccd
AR
954 if (err != EOK)
955 return TARGET_XFER_E_IO;
956
957 initial_stack = procinfo.initial_stack;
958
959 /* procfs is always 'self-hosted', no byte-order manipulation. */
960 tempread = nto_read_auxv_from_initial_stack (initial_stack, tempbuf,
961 sizeof_tempbuf,
962 sizeof (auxv_t));
325fac50 963 tempread = std::min (tempread, len) - offset;
8a6c0ccd
AR
964 memcpy (readbuf, tempbuf + offset, tempread);
965 *xfered_len = tempread;
966 return tempread ? TARGET_XFER_OK : TARGET_XFER_EOF;
967 }
968 /* Fallthru */
9d46c4e5 969 default:
4360561f
TT
970 return this->beneath ()->xfer_partial (object, annex,
971 readbuf, writebuf, offset, len,
972 xfered_len);
61bb466e 973 }
61bb466e
KW
974}
975
976/* Take a program previously attached to and detaches it.
977 The program resumes execution and will no longer stop
978 on signals, etc. We'd better not have left any breakpoints
979 in the program or it'll die when it hits one. */
f6ac5f3d
PA
980void
981nto_procfs_target::detach (inferior *inf, int from_tty)
61bb466e 982{
0f48b757
PA
983 target_announce_detach ();
984
61bb466e 985 if (siggnal)
ebe84f23 986 SignalKill (nto_node (), inf->pid, 0, 0, 0, 0);
61bb466e
KW
987
988 close (ctl_fd);
989 ctl_fd = -1;
7f9f62ba 990
ebe84f23
PA
991 switch_to_no_thread ();
992 detach_inferior (inf->pid);
7f9f62ba 993 init_thread_list ();
6a3cb8e8 994 inf_child_maybe_unpush_target (ops);
61bb466e
KW
995}
996
997static int
998procfs_breakpoint (CORE_ADDR addr, int type, int size)
999{
1000 procfs_break brk;
1001
1002 brk.type = type;
1003 brk.addr = addr;
1004 brk.size = size;
1005 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1006 if (errno != EOK)
1007 return 1;
1008 return 0;
1009}
1010
f6ac5f3d
PA
1011int
1012nto_procfs_target::insert_breakpoint (struct gdbarch *gdbarch,
1013 struct bp_target_info *bp_tgt)
61bb466e 1014{
0d5ed153 1015 bp_tgt->placed_address = bp_tgt->reqstd_address;
8181d85f 1016 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, 0);
61bb466e
KW
1017}
1018
f6ac5f3d
PA
1019int
1020nto_procfs_target::remove_breakpoint (struct gdbarch *gdbarch,
1021 struct bp_target_info *bp_tgt,
1022 enum remove_bp_reason reason)
61bb466e 1023{
8181d85f 1024 return procfs_breakpoint (bp_tgt->placed_address, _DEBUG_BREAK_EXEC, -1);
61bb466e
KW
1025}
1026
f6ac5f3d
PA
1027int
1028nto_procfs_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
1029 struct bp_target_info *bp_tgt)
61bb466e 1030{
0d5ed153 1031 bp_tgt->placed_address = bp_tgt->reqstd_address;
8181d85f
DJ
1032 return procfs_breakpoint (bp_tgt->placed_address,
1033 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, 0);
61bb466e
KW
1034}
1035
f6ac5f3d
PA
1036int
1037nto_procfs_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
1038 struct bp_target_info *bp_tgt)
61bb466e 1039{
8181d85f
DJ
1040 return procfs_breakpoint (bp_tgt->placed_address,
1041 _DEBUG_BREAK_EXEC | _DEBUG_BREAK_HW, -1);
61bb466e
KW
1042}
1043
f6ac5f3d
PA
1044void
1045nto_procfs_target::resume (ptid_t ptid, int step, enum gdb_signal signo)
61bb466e
KW
1046{
1047 int signal_to_pass;
1048 procfs_status status;
14ef7606 1049 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
61bb466e 1050
d7e15655 1051 if (inferior_ptid == null_ptid)
61bb466e
KW
1052 return;
1053
d7e15655 1054 procfs_set_thread (ptid == minus_one_ptid ? inferior_ptid :
61bb466e
KW
1055 ptid);
1056
1057 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
1058 if (step)
1059 run.flags |= _DEBUG_RUN_STEP;
1060
14ef7606
AR
1061 sigemptyset (run_fault);
1062 sigaddset (run_fault, FLTBPT);
1063 sigaddset (run_fault, FLTTRACE);
1064 sigaddset (run_fault, FLTILL);
1065 sigaddset (run_fault, FLTPRIV);
1066 sigaddset (run_fault, FLTBOUNDS);
1067 sigaddset (run_fault, FLTIOVF);
1068 sigaddset (run_fault, FLTIZDIV);
1069 sigaddset (run_fault, FLTFPE);
61bb466e 1070 /* Peter V will be changing this at some point. */
14ef7606 1071 sigaddset (run_fault, FLTPAGE);
61bb466e
KW
1072
1073 run.flags |= _DEBUG_RUN_ARM;
1074
2ea28649 1075 signal_to_pass = gdb_signal_to_host (signo);
61bb466e
KW
1076
1077 if (signal_to_pass)
1078 {
1079 devctl (ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
2ea28649 1080 signal_to_pass = gdb_signal_to_host (signo);
61bb466e
KW
1081 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
1082 {
1083 if (signal_to_pass != status.info.si_signo)
1084 {
e99b03dc 1085 SignalKill (nto_node (), inferior_ptid.pid (), 0,
d737fd7f 1086 signal_to_pass, 0, 0);
61bb466e
KW
1087 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
1088 }
0df8b418 1089 else /* Let it kill the program without telling us. */
61bb466e
KW
1090 sigdelset (&run.trace, signal_to_pass);
1091 }
1092 }
1093 else
1094 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
1095
1096 errno = devctl (ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
1097 if (errno != EOK)
1098 {
9b20d036 1099 perror (_("run error!\n"));
61bb466e
KW
1100 return;
1101 }
1102}
1103
f6ac5f3d
PA
1104void
1105nto_procfs_target::mourn_inferior ()
61bb466e 1106{
d7e15655 1107 if (inferior_ptid != null_ptid)
61bb466e 1108 {
e99b03dc 1109 SignalKill (nto_node (), inferior_ptid.pid (), 0, SIGKILL, 0, 0);
61bb466e
KW
1110 close (ctl_fd);
1111 }
ebe84f23 1112 switch_to_no_thread ();
61bb466e 1113 init_thread_list ();
c1ee2fb3 1114 inf_child_mourn_inferior (ops);
61bb466e
KW
1115}
1116
1117/* This function breaks up an argument string into an argument
1118 vector suitable for passing to execvp().
1119 E.g., on "run a b c d" this routine would get as input
1120 the string "a b c d", and as output it would fill in argv with
1121 the four arguments "a", "b", "c", "d". The only additional
1122 functionality is simple quoting. The gdb command:
24b21115 1123 run a "b c d" f
61bb466e
KW
1124 will fill in argv with the three args "a", "b c d", "e". */
1125static void
1126breakup_args (char *scratch, char **argv)
1127{
1128 char *pp, *cp = scratch;
1129 char quoting = 0;
1130
1131 for (;;)
1132 {
1133 /* Scan past leading separators. */
1134 quoting = 0;
1135 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1136 cp++;
1137
1138 /* Break if at end of string. */
1139 if (*cp == '\0')
1140 break;
1141
1142 /* Take an arg. */
1143 if (*cp == '"')
1144 {
1145 cp++;
1146 quoting = strchr (cp, '"') ? 1 : 0;
1147 }
1148
1149 *argv++ = cp;
1150
1151 /* Scan for next arg separator. */
1152 pp = cp;
1153 if (quoting)
1154 cp = strchr (pp, '"');
1155 if ((cp == NULL) || (!quoting))
1156 cp = strchr (pp, ' ');
1157 if (cp == NULL)
1158 cp = strchr (pp, '\t');
1159 if (cp == NULL)
1160 cp = strchr (pp, '\n');
1161
1162 /* No separators => end of string => break. */
1163 if (cp == NULL)
1164 {
1165 pp = cp;
1166 break;
1167 }
1168
1169 /* Replace the separator with a terminator. */
1170 *cp++ = '\0';
1171 }
1172
1173 /* Execv requires a null-terminated arg vector. */
1174 *argv = NULL;
1175}
1176
f6ac5f3d
PA
1177void
1178nto_procfs_target::create_inferior (const char *exec_file,
1179 const std::string &allargs,
1180 char **env, int from_tty)
61bb466e
KW
1181{
1182 struct inheritance inherit;
1183 pid_t pid;
1184 int flags, errn;
1185 char **argv, *args;
3cb3b8df 1186 const char *in = "", *out = "", *err = "";
61bb466e
KW
1187 int fd, fds[3];
1188 sigset_t set;
3fdfcbf1 1189 struct inferior *inf;
61bb466e 1190
7c5ded6a 1191 argv = xmalloc ((allargs.size () / (unsigned) 2 + 2) *
61bb466e 1192 sizeof (*argv));
d9fa87f4 1193 argv[0] = const_cast<char *> (get_exec_file (1));
61bb466e
KW
1194 if (!argv[0])
1195 {
1196 if (exec_file)
1197 argv[0] = exec_file;
1198 else
1199 return;
1200 }
1201
7c5ded6a 1202 args = xstrdup (allargs.c_str ());
609c3040 1203 breakup_args (args, (exec_file != NULL) ? &argv[1] : &argv[0]);
61bb466e
KW
1204
1205 argv = nto_parse_redirection (argv, &in, &out, &err);
1206
1207 fds[0] = STDIN_FILENO;
1208 fds[1] = STDOUT_FILENO;
1209 fds[2] = STDERR_FILENO;
1210
1211 /* If the user specified I/O via gdb's --tty= arg, use it, but only
1212 if the i/o is not also being specified via redirection. */
05779d57
PA
1213 const char *inferior_tty = current_inferior ()->tty ();
1214 if (inferior_tty != nullptr)
61bb466e
KW
1215 {
1216 if (!in[0])
05779d57 1217 in = inferior_tty;
61bb466e 1218 if (!out[0])
05779d57 1219 out = inferior_tty;
61bb466e 1220 if (!err[0])
05779d57 1221 err = inferior_tty;
61bb466e
KW
1222 }
1223
1224 if (in[0])
1225 {
1226 fd = open (in, O_RDONLY);
1227 if (fd == -1)
1228 perror (in);
1229 else
1230 fds[0] = fd;
1231 }
1232 if (out[0])
1233 {
1234 fd = open (out, O_WRONLY);
1235 if (fd == -1)
1236 perror (out);
1237 else
1238 fds[1] = fd;
1239 }
1240 if (err[0])
1241 {
1242 fd = open (err, O_WRONLY);
1243 if (fd == -1)
1244 perror (err);
1245 else
1246 fds[2] = fd;
1247 }
1248
1249 /* Clear any pending SIGUSR1's but keep the behavior the same. */
1250 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
1251
1252 sigemptyset (&set);
1253 sigaddset (&set, SIGUSR1);
1254 sigprocmask (SIG_UNBLOCK, &set, NULL);
1255
1256 memset (&inherit, 0, sizeof (inherit));
1257
1258 if (ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) != 0)
1259 {
d737fd7f 1260 inherit.nd = nto_node ();
61bb466e
KW
1261 inherit.flags |= SPAWN_SETND;
1262 inherit.flags &= ~SPAWN_EXEC;
1263 }
1264 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
1265 inherit.pgroup = SPAWN_NEWPGROUP;
1266 pid = spawnp (argv[0], 3, fds, &inherit, argv,
1267 ND_NODE_CMP (nto_procfs_node, ND_LOCAL_NODE) == 0 ? env : 0);
1268 xfree (args);
1269
1270 sigprocmask (SIG_BLOCK, &set, NULL);
1271
1272 if (pid == -1)
8a3fe4f8 1273 error (_("Error spawning %s: %d (%s)"), argv[0], errno,
d737fd7f 1274 safe_strerror (errno));
61bb466e
KW
1275
1276 if (fds[0] != STDIN_FILENO)
1277 close (fds[0]);
1278 if (fds[1] != STDOUT_FILENO)
1279 close (fds[1]);
1280 if (fds[2] != STDERR_FILENO)
1281 close (fds[2]);
1282
ebe84f23
PA
1283 ptid_t ptid = do_attach (ptid_t (pid));
1284 update_thread_list ();
9213a6d7 1285 switch_to_thread (this->find_thread (ptid));
61bb466e 1286
6c95b8df
PA
1287 inf = current_inferior ();
1288 inferior_appeared (inf, pid);
30220b46 1289 inf->attach_flag = false;
7f9f62ba 1290
61bb466e
KW
1291 flags = _DEBUG_FLAG_KLC; /* Kill-on-Last-Close flag. */
1292 errn = devctl (ctl_fd, DCMD_PROC_SET_FLAG, &flags, sizeof (flags), 0);
1293 if (errn != EOK)
1294 {
1295 /* FIXME: expected warning? */
1296 /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
dda83cd7 1297 errn, safe_strerror(errn) ); */
61bb466e 1298 }
c8fbd44a 1299 if (!inf->target_is_pushed (ops))
02980c56 1300 inf->push_target (ops);
223ffa71 1301 target_terminal::init ();
61bb466e 1302
7e10abd1 1303 if (current_program_space->exec_bfd () != NULL
a42d7dd8
TT
1304 || (current_program_space->symfile_object_file != NULL
1305 && current_program_space->symfile_object_file->obfd != NULL))
268a4a75 1306 solib_create_inferior_hook (0);
61bb466e
KW
1307}
1308
f6ac5f3d
PA
1309void
1310nto_procfs_target::interrupt ()
61bb466e
KW
1311{
1312 devctl (ctl_fd, DCMD_PROC_STOP, NULL, 0, 0);
1313}
1314
f6ac5f3d
PA
1315void
1316nto_procfs_target::kill ()
61bb466e 1317{
bc1e6c81 1318 target_mourn_inferior (inferior_ptid);
61bb466e
KW
1319}
1320
61bb466e
KW
1321/* Fill buf with regset and return devctl cmd to do the setting. Return
1322 -1 if we fail to get the regset. Store size of regset in regsize. */
1323static int
1324get_regset (int regset, char *buf, int bufsize, int *regsize)
1325{
1326 int dev_get, dev_set;
1327 switch (regset)
1328 {
1329 case NTO_REG_GENERAL:
1330 dev_get = DCMD_PROC_GETGREG;
1331 dev_set = DCMD_PROC_SETGREG;
1332 break;
1333
1334 case NTO_REG_FLOAT:
1335 dev_get = DCMD_PROC_GETFPREG;
1336 dev_set = DCMD_PROC_SETFPREG;
1337 break;
1338
1339 case NTO_REG_ALT:
1340 dev_get = DCMD_PROC_GETALTREG;
1341 dev_set = DCMD_PROC_SETALTREG;
1342 break;
1343
1344 case NTO_REG_SYSTEM:
1345 default:
1346 return -1;
1347 }
97c44116 1348 if (devctl (ctl_fd, dev_get, buf, bufsize, regsize) != EOK)
61bb466e
KW
1349 return -1;
1350
1351 return dev_set;
1352}
1353
f6ac5f3d
PA
1354void
1355nto_procfs_target::store_registers (struct regcache *regcache, int regno)
61bb466e
KW
1356{
1357 union
1358 {
1359 procfs_greg greg;
1360 procfs_fpreg fpreg;
1361 procfs_altreg altreg;
1362 }
1363 reg;
1364 unsigned off;
1365 int len, regset, regsize, dev_set, err;
1366 char *data;
222312d3 1367 ptid_t ptid = regcache->ptid ();
61bb466e 1368
d7e15655 1369 if (ptid == null_ptid)
61bb466e 1370 return;
bcc0c096 1371 procfs_set_thread (ptid);
61bb466e
KW
1372
1373 if (regno == -1)
1374 {
1375 for (regset = NTO_REG_GENERAL; regset < NTO_REG_END; regset++)
1376 {
1377 dev_set = get_regset (regset, (char *) &reg,
1378 sizeof (reg), &regsize);
1379 if (dev_set == -1)
1380 continue;
1381
56be3814 1382 if (nto_regset_fill (regcache, regset, (char *) &reg) == -1)
61bb466e
KW
1383 continue;
1384
1385 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1386 if (err != EOK)
6cb06a8c
TT
1387 gdb_printf (gdb_stderr,
1388 "Warning unable to write regset %d: %s\n",
1389 regno, safe_strerror (err));
61bb466e
KW
1390 }
1391 }
1392 else
1393 {
1394 regset = nto_regset_id (regno);
1395 if (regset == -1)
1396 return;
1397
1398 dev_set = get_regset (regset, (char *) &reg, sizeof (reg), &regsize);
1399 if (dev_set == -1)
1400 return;
1401
ac7936df 1402 len = nto_register_area (regcache->arch (),
60441ab9 1403 regno, regset, &off);
61bb466e
KW
1404
1405 if (len < 1)
1406 return;
1407
34a79281 1408 regcache->raw_collect (regno, (char *) &reg + off);
61bb466e
KW
1409
1410 err = devctl (ctl_fd, dev_set, &reg, regsize, 0);
1411 if (err != EOK)
6cb06a8c
TT
1412 gdb_printf (gdb_stderr,
1413 "Warning unable to write regset %d: %s\n", regno,
1414 safe_strerror (err));
61bb466e
KW
1415 }
1416}
1417
2455069d
UW
1418/* Set list of signals to be handled in the target. */
1419
f6ac5f3d 1420void
adc6a863
PA
1421nto_procfs_target::pass_signals
1422 (gdb::array_view<const unsigned char> pass_signals)
61bb466e
KW
1423{
1424 int signo;
1425
2455069d
UW
1426 sigfillset (&run.trace);
1427
61bb466e
KW
1428 for (signo = 1; signo < NSIG; signo++)
1429 {
2ea28649 1430 int target_signo = gdb_signal_from_host (signo);
adc6a863 1431 if (target_signo < pass_signals.size () && pass_signals[target_signo])
dda83cd7 1432 sigdelset (&run.trace, signo);
61bb466e
KW
1433 }
1434}
1435
a068643d 1436std::string
f6ac5f3d 1437nto_procfs_target::pid_to_str (ptid_t ptid)
61bb466e 1438{
a068643d 1439 int pid, tid;
61bb466e
KW
1440 struct tidinfo *tip;
1441
e99b03dc 1442 pid = ptid.pid ();
cc6bcb54 1443 tid = ptid.tid ();
61bb466e 1444
61bb466e
KW
1445#if 0 /* NYI */
1446 tip = procfs_thread_info (pid, tid);
1447 if (tip != NULL)
dc5dd1eb 1448 snprintf (&buf[n], 1023, " (state = 0x%02x)", tip->state);
61bb466e
KW
1449#endif
1450
a068643d 1451 return string_printf ("process %d", pid);
61bb466e
KW
1452}
1453
132f8e03
PA
1454/* to_can_run implementation for "target procfs". Note this really
1455 means "can this target be the default run target", which there can
1456 be only one, and we make it be "target native" like other ports.
1457 "target procfs <node>" wouldn't make sense as default run target, as
1458 it needs <node>. */
ee8e9165 1459
f6ac5f3d
PA
1460int
1461nto_procfs_target::can_run ()
132f8e03
PA
1462{
1463 return 0;
1464}
1465
1466/* "target procfs". */
f6ac5f3d 1467static nto_procfs_target_procfs nto_procfs_ops;
132f8e03 1468
6a3cb8e8 1469/* "target native". */
f6ac5f3d 1470static nto_procfs_target_native nto_native_ops;
6a3cb8e8 1471
132f8e03
PA
1472/* Create the "native" and "procfs" targets. */
1473
1474static void
1475init_procfs_targets (void)
61bb466e 1476{
132f8e03 1477 /* Register "target native". This is the default run target. */
d9f719f1
PA
1478 add_target (nto_native_target_info, inf_child_open_target);
1479 set_native_target (&nto_native_ops);
132f8e03
PA
1480
1481 /* Register "target procfs <node>". */
d9f719f1 1482 add_target (nto_procfs_target_info, inf_child_open_target);
61bb466e
KW
1483}
1484
1485#define OSTYPE_NTO 1
1486
6c265988 1487void _initialize_procfs ();
61bb466e 1488void
6c265988 1489_initialize_procfs ()
61bb466e
KW
1490{
1491 sigset_t set;
1492
132f8e03 1493 init_procfs_targets ();
61bb466e
KW
1494
1495 /* We use SIGUSR1 to gain control after we block waiting for a process.
1496 We use sigwaitevent to wait. */
1497 sigemptyset (&set);
1498 sigaddset (&set, SIGUSR1);
1499 sigprocmask (SIG_BLOCK, &set, NULL);
1500
2455069d
UW
1501 /* Initially, make sure all signals are reported. */
1502 sigfillset (&run.trace);
61bb466e
KW
1503
1504 /* Stuff some information. */
1505 nto_cpuinfo_flags = SYSPAGE_ENTRY (cpuinfo)->flags;
1506 nto_cpuinfo_valid = 1;
1507
1bedd215
AC
1508 add_info ("pidlist", procfs_pidlist, _("pidlist"));
1509 add_info ("meminfo", procfs_meminfo, _("memory information"));
d737fd7f
KW
1510
1511 nto_is_nto_target = procfs_is_nto_target;
61bb466e
KW
1512}
1513
1514
1515static int
f486487f 1516procfs_hw_watchpoint (int addr, int len, enum target_hw_bp_type type)
61bb466e
KW
1517{
1518 procfs_break brk;
1519
1520 switch (type)
1521 {
f486487f 1522 case hw_read:
61bb466e
KW
1523 brk.type = _DEBUG_BREAK_RD;
1524 break;
f486487f 1525 case hw_access:
61bb466e
KW
1526 brk.type = _DEBUG_BREAK_RW;
1527 break;
1528 default: /* Modify. */
1529/* FIXME: brk.type = _DEBUG_BREAK_RWM gives EINVAL for some reason. */
1530 brk.type = _DEBUG_BREAK_RW;
1531 }
1532 brk.type |= _DEBUG_BREAK_HW; /* Always ask for HW. */
1533 brk.addr = addr;
1534 brk.size = len;
1535
1536 errno = devctl (ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0);
1537 if (errno != EOK)
1538 {
9b20d036 1539 perror (_("Failed to set hardware watchpoint"));
61bb466e
KW
1540 return -1;
1541 }
1542 return 0;
1543}
1544
57810aa7 1545bool
f6ac5f3d
PA
1546nto_procfs_target::can_use_hw_breakpoint (enum bptype type,
1547 int cnt, int othertype)
61bb466e
KW
1548{
1549 return 1;
1550}
1551
f6ac5f3d
PA
1552int
1553nto_procfs_target::remove_hw_watchpoint (CORE_ADDR addr, int len,
1554 enum target_hw_bp_type type,
1555 struct expression *cond)
61bb466e
KW
1556{
1557 return procfs_hw_watchpoint (addr, -1, type);
1558}
1559
f6ac5f3d
PA
1560int
1561nto_procfs_target::insert_hw_watchpoint (CORE_ADDR addr, int len,
1562 enum target_hw_bp_type type,
1563 struct expression *cond)
61bb466e
KW
1564{
1565 return procfs_hw_watchpoint (addr, len, type);
1566}
1567
57810aa7 1568bool
f6ac5f3d 1569nto_procfs_target::stopped_by_watchpoint ()
61bb466e 1570{
a9889169
AR
1571 /* NOTE: nto_stopped_by_watchpoint will be called ONLY while we are
1572 stopped due to a SIGTRAP. This assumes gdb works in 'all-stop' mode;
1573 future gdb versions will likely run in 'non-stop' mode in which case
1574 we will have to store/examine statuses per thread in question.
1575 Until then, this will work fine. */
1576
1577 struct inferior *inf = current_inferior ();
1578 struct nto_inferior_data *inf_data;
1579
1580 gdb_assert (inf != NULL);
1581
1582 inf_data = nto_inferior_data (inf);
1583
1584 return inf_data->stopped_flags
1585 & (_DEBUG_FLAG_TRACE_RD
1586 | _DEBUG_FLAG_TRACE_WR
1587 | _DEBUG_FLAG_TRACE_MODIFY);
61bb466e 1588}