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