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