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