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