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