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