| 1 | /* Machine independent support for Solaris /proc (process file system) for GDB. |
| 2 | |
| 3 | Copyright (C) 1999-2018 Free Software Foundation, Inc. |
| 4 | |
| 5 | Written by Michael Snyder at Cygnus Solutions. |
| 6 | Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others. |
| 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 | #include "inferior.h" |
| 25 | #include "infrun.h" |
| 26 | #include "target.h" |
| 27 | #include "gdbcore.h" |
| 28 | #include "elf-bfd.h" /* for elfcore_write_* */ |
| 29 | #include "gdbcmd.h" |
| 30 | #include "gdbthread.h" |
| 31 | #include "regcache.h" |
| 32 | #include "inf-child.h" |
| 33 | #include "nat/fork-inferior.h" |
| 34 | #include "filestuff.h" |
| 35 | |
| 36 | #define _STRUCTURED_PROC 1 /* Should be done by configure script. */ |
| 37 | |
| 38 | #include <sys/procfs.h> |
| 39 | #include <sys/fault.h> |
| 40 | #include <sys/syscall.h> |
| 41 | #include "gdb_wait.h" |
| 42 | #include <signal.h> |
| 43 | #include <ctype.h> |
| 44 | #include "gdb_bfd.h" |
| 45 | #include "inflow.h" |
| 46 | #include "auxv.h" |
| 47 | #include "procfs.h" |
| 48 | #include "observable.h" |
| 49 | #include "common/scoped_fd.h" |
| 50 | |
| 51 | /* This module provides the interface between GDB and the |
| 52 | /proc file system, which is used on many versions of Unix |
| 53 | as a means for debuggers to control other processes. |
| 54 | |
| 55 | /proc works by imitating a file system: you open a simulated file |
| 56 | that represents the process you wish to interact with, and perform |
| 57 | operations on that "file" in order to examine or change the state |
| 58 | of the other process. |
| 59 | |
| 60 | The most important thing to know about /proc and this module is |
| 61 | that there are two very different interfaces to /proc: |
| 62 | |
| 63 | One that uses the ioctl system call, and another that uses read |
| 64 | and write system calls. |
| 65 | |
| 66 | This module supports only the Solaris version of the read/write |
| 67 | interface. */ |
| 68 | |
| 69 | #include <sys/types.h> |
| 70 | #include <dirent.h> /* opendir/readdir, for listing the LWP's */ |
| 71 | |
| 72 | #include <fcntl.h> /* for O_RDONLY */ |
| 73 | #include <unistd.h> /* for "X_OK" */ |
| 74 | #include <sys/stat.h> /* for struct stat */ |
| 75 | |
| 76 | /* Note: procfs-utils.h must be included after the above system header |
| 77 | files, because it redefines various system calls using macros. |
| 78 | This may be incompatible with the prototype declarations. */ |
| 79 | |
| 80 | #include "proc-utils.h" |
| 81 | |
| 82 | /* Prototypes for supply_gregset etc. */ |
| 83 | #include "gregset.h" |
| 84 | |
| 85 | /* =================== TARGET_OPS "MODULE" =================== */ |
| 86 | |
| 87 | /* This module defines the GDB target vector and its methods. */ |
| 88 | |
| 89 | |
| 90 | static enum target_xfer_status procfs_xfer_memory (gdb_byte *, |
| 91 | const gdb_byte *, |
| 92 | ULONGEST, ULONGEST, |
| 93 | ULONGEST *); |
| 94 | |
| 95 | class procfs_target final : public inf_child_target |
| 96 | { |
| 97 | public: |
| 98 | void create_inferior (const char *, const std::string &, |
| 99 | char **, int) override; |
| 100 | |
| 101 | void kill () override; |
| 102 | |
| 103 | void mourn_inferior () override; |
| 104 | |
| 105 | void attach (const char *, int) override; |
| 106 | void detach (inferior *inf, int) override; |
| 107 | |
| 108 | void resume (ptid_t, int, enum gdb_signal) override; |
| 109 | ptid_t wait (ptid_t, struct target_waitstatus *, int) override; |
| 110 | |
| 111 | void fetch_registers (struct regcache *, int) override; |
| 112 | void store_registers (struct regcache *, int) override; |
| 113 | |
| 114 | enum target_xfer_status xfer_partial (enum target_object object, |
| 115 | const char *annex, |
| 116 | gdb_byte *readbuf, |
| 117 | const gdb_byte *writebuf, |
| 118 | ULONGEST offset, ULONGEST len, |
| 119 | ULONGEST *xfered_len) override; |
| 120 | |
| 121 | void pass_signals (int, unsigned char *) override; |
| 122 | |
| 123 | void files_info () override; |
| 124 | |
| 125 | void update_thread_list () override; |
| 126 | |
| 127 | bool thread_alive (ptid_t ptid) override; |
| 128 | |
| 129 | const char *pid_to_str (ptid_t) override; |
| 130 | |
| 131 | thread_control_capabilities get_thread_control_capabilities () override |
| 132 | { return tc_schedlock; } |
| 133 | |
| 134 | /* find_memory_regions support method for gcore */ |
| 135 | int find_memory_regions (find_memory_region_ftype func, void *data) |
| 136 | override; |
| 137 | |
| 138 | char *make_corefile_notes (bfd *, int *) override; |
| 139 | |
| 140 | bool info_proc (const char *, enum info_proc_what) override; |
| 141 | |
| 142 | #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64) |
| 143 | int auxv_parse (gdb_byte **readptr, |
| 144 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) |
| 145 | override; |
| 146 | #endif |
| 147 | |
| 148 | bool stopped_by_watchpoint () override; |
| 149 | |
| 150 | int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type, |
| 151 | struct expression *) override; |
| 152 | |
| 153 | int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type, |
| 154 | struct expression *) override; |
| 155 | |
| 156 | int region_ok_for_hw_watchpoint (CORE_ADDR, int) override; |
| 157 | |
| 158 | int can_use_hw_breakpoint (enum bptype, int, int) override; |
| 159 | bool stopped_data_address (CORE_ADDR *) override; |
| 160 | }; |
| 161 | |
| 162 | static procfs_target the_procfs_target; |
| 163 | |
| 164 | #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64) |
| 165 | /* When GDB is built as 64-bit application on Solaris, the auxv data |
| 166 | is presented in 64-bit format. We need to provide a custom parser |
| 167 | to handle that. */ |
| 168 | int |
| 169 | procfs_target::auxv_parse (gdb_byte **readptr, |
| 170 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) |
| 171 | { |
| 172 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
| 173 | gdb_byte *ptr = *readptr; |
| 174 | |
| 175 | if (endptr == ptr) |
| 176 | return 0; |
| 177 | |
| 178 | if (endptr - ptr < 8 * 2) |
| 179 | return -1; |
| 180 | |
| 181 | *typep = extract_unsigned_integer (ptr, 4, byte_order); |
| 182 | ptr += 8; |
| 183 | /* The size of data is always 64-bit. If the application is 32-bit, |
| 184 | it will be zero extended, as expected. */ |
| 185 | *valp = extract_unsigned_integer (ptr, 8, byte_order); |
| 186 | ptr += 8; |
| 187 | |
| 188 | *readptr = ptr; |
| 189 | return 1; |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | /* =================== END, TARGET_OPS "MODULE" =================== */ |
| 194 | |
| 195 | /* World Unification: |
| 196 | |
| 197 | Put any typedefs, defines etc. here that are required for the |
| 198 | unification of code that handles different versions of /proc. */ |
| 199 | |
| 200 | enum { READ_WATCHFLAG = WA_READ, |
| 201 | WRITE_WATCHFLAG = WA_WRITE, |
| 202 | EXEC_WATCHFLAG = WA_EXEC, |
| 203 | AFTER_WATCHFLAG = WA_TRAPAFTER |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | /* =================== STRUCT PROCINFO "MODULE" =================== */ |
| 208 | |
| 209 | /* FIXME: this comment will soon be out of date W.R.T. threads. */ |
| 210 | |
| 211 | /* The procinfo struct is a wrapper to hold all the state information |
| 212 | concerning a /proc process. There should be exactly one procinfo |
| 213 | for each process, and since GDB currently can debug only one |
| 214 | process at a time, that means there should be only one procinfo. |
| 215 | All of the LWP's of a process can be accessed indirectly thru the |
| 216 | single process procinfo. |
| 217 | |
| 218 | However, against the day when GDB may debug more than one process, |
| 219 | this data structure is kept in a list (which for now will hold no |
| 220 | more than one member), and many functions will have a pointer to a |
| 221 | procinfo as an argument. |
| 222 | |
| 223 | There will be a separate procinfo structure for use by the (not yet |
| 224 | implemented) "info proc" command, so that we can print useful |
| 225 | information about any random process without interfering with the |
| 226 | inferior's procinfo information. */ |
| 227 | |
| 228 | /* format strings for /proc paths */ |
| 229 | #define MAIN_PROC_NAME_FMT "/proc/%d" |
| 230 | #define CTL_PROC_NAME_FMT "/proc/%d/ctl" |
| 231 | #define AS_PROC_NAME_FMT "/proc/%d/as" |
| 232 | #define MAP_PROC_NAME_FMT "/proc/%d/map" |
| 233 | #define STATUS_PROC_NAME_FMT "/proc/%d/status" |
| 234 | #define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus") |
| 235 | |
| 236 | typedef struct procinfo { |
| 237 | struct procinfo *next; |
| 238 | int pid; /* Process ID */ |
| 239 | int tid; /* Thread/LWP id */ |
| 240 | |
| 241 | /* process state */ |
| 242 | int was_stopped; |
| 243 | int ignore_next_sigstop; |
| 244 | |
| 245 | int ctl_fd; /* File descriptor for /proc control file */ |
| 246 | int status_fd; /* File descriptor for /proc status file */ |
| 247 | int as_fd; /* File descriptor for /proc as file */ |
| 248 | |
| 249 | char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */ |
| 250 | |
| 251 | fltset_t saved_fltset; /* Saved traced hardware fault set */ |
| 252 | sigset_t saved_sigset; /* Saved traced signal set */ |
| 253 | sigset_t saved_sighold; /* Saved held signal set */ |
| 254 | sysset_t *saved_exitset; /* Saved traced system call exit set */ |
| 255 | sysset_t *saved_entryset; /* Saved traced system call entry set */ |
| 256 | |
| 257 | pstatus_t prstatus; /* Current process status info */ |
| 258 | |
| 259 | struct procinfo *thread_list; |
| 260 | |
| 261 | int status_valid : 1; |
| 262 | int gregs_valid : 1; |
| 263 | int fpregs_valid : 1; |
| 264 | int threads_valid: 1; |
| 265 | } procinfo; |
| 266 | |
| 267 | static char errmsg[128]; /* shared error msg buffer */ |
| 268 | |
| 269 | /* Function prototypes for procinfo module: */ |
| 270 | |
| 271 | static procinfo *find_procinfo_or_die (int pid, int tid); |
| 272 | static procinfo *find_procinfo (int pid, int tid); |
| 273 | static procinfo *create_procinfo (int pid, int tid); |
| 274 | static void destroy_procinfo (procinfo *p); |
| 275 | static void do_destroy_procinfo_cleanup (void *); |
| 276 | static void dead_procinfo (procinfo *p, const char *msg, int killp); |
| 277 | static int open_procinfo_files (procinfo *p, int which); |
| 278 | static void close_procinfo_files (procinfo *p); |
| 279 | static sysset_t *sysset_t_alloc (procinfo *pi); |
| 280 | |
| 281 | static int iterate_over_mappings |
| 282 | (procinfo *pi, find_memory_region_ftype child_func, void *data, |
| 283 | int (*func) (struct prmap *map, find_memory_region_ftype child_func, |
| 284 | void *data)); |
| 285 | |
| 286 | /* The head of the procinfo list: */ |
| 287 | static procinfo *procinfo_list; |
| 288 | |
| 289 | /* Search the procinfo list. Return a pointer to procinfo, or NULL if |
| 290 | not found. */ |
| 291 | |
| 292 | static procinfo * |
| 293 | find_procinfo (int pid, int tid) |
| 294 | { |
| 295 | procinfo *pi; |
| 296 | |
| 297 | for (pi = procinfo_list; pi; pi = pi->next) |
| 298 | if (pi->pid == pid) |
| 299 | break; |
| 300 | |
| 301 | if (pi) |
| 302 | if (tid) |
| 303 | { |
| 304 | /* Don't check threads_valid. If we're updating the |
| 305 | thread_list, we want to find whatever threads are already |
| 306 | here. This means that in general it is the caller's |
| 307 | responsibility to check threads_valid and update before |
| 308 | calling find_procinfo, if the caller wants to find a new |
| 309 | thread. */ |
| 310 | |
| 311 | for (pi = pi->thread_list; pi; pi = pi->next) |
| 312 | if (pi->tid == tid) |
| 313 | break; |
| 314 | } |
| 315 | |
| 316 | return pi; |
| 317 | } |
| 318 | |
| 319 | /* Calls find_procinfo, but errors on failure. */ |
| 320 | |
| 321 | static procinfo * |
| 322 | find_procinfo_or_die (int pid, int tid) |
| 323 | { |
| 324 | procinfo *pi = find_procinfo (pid, tid); |
| 325 | |
| 326 | if (pi == NULL) |
| 327 | { |
| 328 | if (tid) |
| 329 | error (_("procfs: couldn't find pid %d " |
| 330 | "(kernel thread %d) in procinfo list."), |
| 331 | pid, tid); |
| 332 | else |
| 333 | error (_("procfs: couldn't find pid %d in procinfo list."), pid); |
| 334 | } |
| 335 | return pi; |
| 336 | } |
| 337 | |
| 338 | /* Wrapper for `open'. The appropriate open call is attempted; if |
| 339 | unsuccessful, it will be retried as many times as needed for the |
| 340 | EAGAIN and EINTR conditions. |
| 341 | |
| 342 | For other conditions, retry the open a limited number of times. In |
| 343 | addition, a short sleep is imposed prior to retrying the open. The |
| 344 | reason for this sleep is to give the kernel a chance to catch up |
| 345 | and create the file in question in the event that GDB "wins" the |
| 346 | race to open a file before the kernel has created it. */ |
| 347 | |
| 348 | static int |
| 349 | open_with_retry (const char *pathname, int flags) |
| 350 | { |
| 351 | int retries_remaining, status; |
| 352 | |
| 353 | retries_remaining = 2; |
| 354 | |
| 355 | while (1) |
| 356 | { |
| 357 | status = open (pathname, flags); |
| 358 | |
| 359 | if (status >= 0 || retries_remaining == 0) |
| 360 | break; |
| 361 | else if (errno != EINTR && errno != EAGAIN) |
| 362 | { |
| 363 | retries_remaining--; |
| 364 | sleep (1); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return status; |
| 369 | } |
| 370 | |
| 371 | /* Open the file descriptor for the process or LWP. We only open the |
| 372 | control file descriptor; the others are opened lazily as needed. |
| 373 | Returns the file descriptor, or zero for failure. */ |
| 374 | |
| 375 | enum { FD_CTL, FD_STATUS, FD_AS }; |
| 376 | |
| 377 | static int |
| 378 | open_procinfo_files (procinfo *pi, int which) |
| 379 | { |
| 380 | char tmp[MAX_PROC_NAME_SIZE]; |
| 381 | int fd; |
| 382 | |
| 383 | /* This function is getting ALMOST long enough to break up into |
| 384 | several. Here is some rationale: |
| 385 | |
| 386 | There are several file descriptors that may need to be open |
| 387 | for any given process or LWP. The ones we're intereted in are: |
| 388 | - control (ctl) write-only change the state |
| 389 | - status (status) read-only query the state |
| 390 | - address space (as) read/write access memory |
| 391 | - map (map) read-only virtual addr map |
| 392 | Most of these are opened lazily as they are needed. |
| 393 | The pathnames for the 'files' for an LWP look slightly |
| 394 | different from those of a first-class process: |
| 395 | Pathnames for a process (<proc-id>): |
| 396 | /proc/<proc-id>/ctl |
| 397 | /proc/<proc-id>/status |
| 398 | /proc/<proc-id>/as |
| 399 | /proc/<proc-id>/map |
| 400 | Pathnames for an LWP (lwp-id): |
| 401 | /proc/<proc-id>/lwp/<lwp-id>/lwpctl |
| 402 | /proc/<proc-id>/lwp/<lwp-id>/lwpstatus |
| 403 | An LWP has no map or address space file descriptor, since |
| 404 | the memory map and address space are shared by all LWPs. */ |
| 405 | |
| 406 | /* In this case, there are several different file descriptors that |
| 407 | we might be asked to open. The control file descriptor will be |
| 408 | opened early, but the others will be opened lazily as they are |
| 409 | needed. */ |
| 410 | |
| 411 | strcpy (tmp, pi->pathname); |
| 412 | switch (which) { /* Which file descriptor to open? */ |
| 413 | case FD_CTL: |
| 414 | if (pi->tid) |
| 415 | strcat (tmp, "/lwpctl"); |
| 416 | else |
| 417 | strcat (tmp, "/ctl"); |
| 418 | fd = open_with_retry (tmp, O_WRONLY); |
| 419 | if (fd < 0) |
| 420 | return 0; /* fail */ |
| 421 | pi->ctl_fd = fd; |
| 422 | break; |
| 423 | case FD_AS: |
| 424 | if (pi->tid) |
| 425 | return 0; /* There is no 'as' file descriptor for an lwp. */ |
| 426 | strcat (tmp, "/as"); |
| 427 | fd = open_with_retry (tmp, O_RDWR); |
| 428 | if (fd < 0) |
| 429 | return 0; /* fail */ |
| 430 | pi->as_fd = fd; |
| 431 | break; |
| 432 | case FD_STATUS: |
| 433 | if (pi->tid) |
| 434 | strcat (tmp, "/lwpstatus"); |
| 435 | else |
| 436 | strcat (tmp, "/status"); |
| 437 | fd = open_with_retry (tmp, O_RDONLY); |
| 438 | if (fd < 0) |
| 439 | return 0; /* fail */ |
| 440 | pi->status_fd = fd; |
| 441 | break; |
| 442 | default: |
| 443 | return 0; /* unknown file descriptor */ |
| 444 | } |
| 445 | |
| 446 | return 1; /* success */ |
| 447 | } |
| 448 | |
| 449 | /* Allocate a data structure and link it into the procinfo list. |
| 450 | First tries to find a pre-existing one (FIXME: why?). Returns the |
| 451 | pointer to new procinfo struct. */ |
| 452 | |
| 453 | static procinfo * |
| 454 | create_procinfo (int pid, int tid) |
| 455 | { |
| 456 | procinfo *pi, *parent = NULL; |
| 457 | |
| 458 | if ((pi = find_procinfo (pid, tid))) |
| 459 | return pi; /* Already exists, nothing to do. */ |
| 460 | |
| 461 | /* Find parent before doing malloc, to save having to cleanup. */ |
| 462 | if (tid != 0) |
| 463 | parent = find_procinfo_or_die (pid, 0); /* FIXME: should I |
| 464 | create it if it |
| 465 | doesn't exist yet? */ |
| 466 | |
| 467 | pi = XNEW (procinfo); |
| 468 | memset (pi, 0, sizeof (procinfo)); |
| 469 | pi->pid = pid; |
| 470 | pi->tid = tid; |
| 471 | |
| 472 | pi->saved_entryset = sysset_t_alloc (pi); |
| 473 | pi->saved_exitset = sysset_t_alloc (pi); |
| 474 | |
| 475 | /* Chain into list. */ |
| 476 | if (tid == 0) |
| 477 | { |
| 478 | sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid); |
| 479 | pi->next = procinfo_list; |
| 480 | procinfo_list = pi; |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid); |
| 485 | pi->next = parent->thread_list; |
| 486 | parent->thread_list = pi; |
| 487 | } |
| 488 | return pi; |
| 489 | } |
| 490 | |
| 491 | /* Close all file descriptors associated with the procinfo. */ |
| 492 | |
| 493 | static void |
| 494 | close_procinfo_files (procinfo *pi) |
| 495 | { |
| 496 | if (pi->ctl_fd > 0) |
| 497 | close (pi->ctl_fd); |
| 498 | if (pi->as_fd > 0) |
| 499 | close (pi->as_fd); |
| 500 | if (pi->status_fd > 0) |
| 501 | close (pi->status_fd); |
| 502 | pi->ctl_fd = pi->as_fd = pi->status_fd = 0; |
| 503 | } |
| 504 | |
| 505 | /* Destructor function. Close, unlink and deallocate the object. */ |
| 506 | |
| 507 | static void |
| 508 | destroy_one_procinfo (procinfo **list, procinfo *pi) |
| 509 | { |
| 510 | procinfo *ptr; |
| 511 | |
| 512 | /* Step one: unlink the procinfo from its list. */ |
| 513 | if (pi == *list) |
| 514 | *list = pi->next; |
| 515 | else |
| 516 | for (ptr = *list; ptr; ptr = ptr->next) |
| 517 | if (ptr->next == pi) |
| 518 | { |
| 519 | ptr->next = pi->next; |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | /* Step two: close any open file descriptors. */ |
| 524 | close_procinfo_files (pi); |
| 525 | |
| 526 | /* Step three: free the memory. */ |
| 527 | xfree (pi->saved_entryset); |
| 528 | xfree (pi->saved_exitset); |
| 529 | xfree (pi); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | destroy_procinfo (procinfo *pi) |
| 534 | { |
| 535 | procinfo *tmp; |
| 536 | |
| 537 | if (pi->tid != 0) /* Destroy a thread procinfo. */ |
| 538 | { |
| 539 | tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */ |
| 540 | destroy_one_procinfo (&tmp->thread_list, pi); |
| 541 | } |
| 542 | else /* Destroy a process procinfo and all its threads. */ |
| 543 | { |
| 544 | /* First destroy the children, if any; */ |
| 545 | while (pi->thread_list != NULL) |
| 546 | destroy_one_procinfo (&pi->thread_list, pi->thread_list); |
| 547 | /* Then destroy the parent. Genocide!!! */ |
| 548 | destroy_one_procinfo (&procinfo_list, pi); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | do_destroy_procinfo_cleanup (void *pi) |
| 554 | { |
| 555 | destroy_procinfo ((procinfo *) pi); |
| 556 | } |
| 557 | |
| 558 | enum { NOKILL, KILL }; |
| 559 | |
| 560 | /* To be called on a non_recoverable error for a procinfo. Prints |
| 561 | error messages, optionally sends a SIGKILL to the process, then |
| 562 | destroys the data structure. */ |
| 563 | |
| 564 | static void |
| 565 | dead_procinfo (procinfo *pi, const char *msg, int kill_p) |
| 566 | { |
| 567 | char procfile[80]; |
| 568 | |
| 569 | if (pi->pathname) |
| 570 | { |
| 571 | print_sys_errmsg (pi->pathname, errno); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | sprintf (procfile, "process %d", pi->pid); |
| 576 | print_sys_errmsg (procfile, errno); |
| 577 | } |
| 578 | if (kill_p == KILL) |
| 579 | kill (pi->pid, SIGKILL); |
| 580 | |
| 581 | destroy_procinfo (pi); |
| 582 | error ("%s", msg); |
| 583 | } |
| 584 | |
| 585 | /* Allocate and (partially) initialize a sysset_t struct. */ |
| 586 | |
| 587 | static sysset_t * |
| 588 | sysset_t_alloc (procinfo *pi) |
| 589 | { |
| 590 | return (sysset_t *) xmalloc (sizeof (sysset_t)); |
| 591 | } |
| 592 | |
| 593 | /* =================== END, STRUCT PROCINFO "MODULE" =================== */ |
| 594 | |
| 595 | /* =================== /proc "MODULE" =================== */ |
| 596 | |
| 597 | /* This "module" is the interface layer between the /proc system API |
| 598 | and the gdb target vector functions. This layer consists of access |
| 599 | functions that encapsulate each of the basic operations that we |
| 600 | need to use from the /proc API. |
| 601 | |
| 602 | The main motivation for this layer is to hide the fact that there |
| 603 | are two very different implementations of the /proc API. Rather |
| 604 | than have a bunch of #ifdefs all thru the gdb target vector |
| 605 | functions, we do our best to hide them all in here. */ |
| 606 | |
| 607 | static long proc_flags (procinfo *pi); |
| 608 | static int proc_why (procinfo *pi); |
| 609 | static int proc_what (procinfo *pi); |
| 610 | static int proc_set_current_signal (procinfo *pi, int signo); |
| 611 | static int proc_get_current_thread (procinfo *pi); |
| 612 | static int proc_iterate_over_threads |
| 613 | (procinfo *pi, |
| 614 | int (*func) (procinfo *, procinfo *, void *), |
| 615 | void *ptr); |
| 616 | |
| 617 | static void |
| 618 | proc_warn (procinfo *pi, const char *func, int line) |
| 619 | { |
| 620 | sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); |
| 621 | print_sys_errmsg (errmsg, errno); |
| 622 | } |
| 623 | |
| 624 | static void |
| 625 | proc_error (procinfo *pi, const char *func, int line) |
| 626 | { |
| 627 | sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); |
| 628 | perror_with_name (errmsg); |
| 629 | } |
| 630 | |
| 631 | /* Updates the status struct in the procinfo. There is a 'valid' |
| 632 | flag, to let other functions know when this function needs to be |
| 633 | called (so the status is only read when it is needed). The status |
| 634 | file descriptor is also only opened when it is needed. Returns |
| 635 | non-zero for success, zero for failure. */ |
| 636 | |
| 637 | static int |
| 638 | proc_get_status (procinfo *pi) |
| 639 | { |
| 640 | /* Status file descriptor is opened "lazily". */ |
| 641 | if (pi->status_fd == 0 && |
| 642 | open_procinfo_files (pi, FD_STATUS) == 0) |
| 643 | { |
| 644 | pi->status_valid = 0; |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | if (lseek (pi->status_fd, 0, SEEK_SET) < 0) |
| 649 | pi->status_valid = 0; /* fail */ |
| 650 | else |
| 651 | { |
| 652 | /* Sigh... I have to read a different data structure, |
| 653 | depending on whether this is a main process or an LWP. */ |
| 654 | if (pi->tid) |
| 655 | pi->status_valid = (read (pi->status_fd, |
| 656 | (char *) &pi->prstatus.pr_lwp, |
| 657 | sizeof (lwpstatus_t)) |
| 658 | == sizeof (lwpstatus_t)); |
| 659 | else |
| 660 | { |
| 661 | pi->status_valid = (read (pi->status_fd, |
| 662 | (char *) &pi->prstatus, |
| 663 | sizeof (pstatus_t)) |
| 664 | == sizeof (pstatus_t)); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (pi->status_valid) |
| 669 | { |
| 670 | PROC_PRETTYFPRINT_STATUS (proc_flags (pi), |
| 671 | proc_why (pi), |
| 672 | proc_what (pi), |
| 673 | proc_get_current_thread (pi)); |
| 674 | } |
| 675 | |
| 676 | /* The status struct includes general regs, so mark them valid too. */ |
| 677 | pi->gregs_valid = pi->status_valid; |
| 678 | /* In the read/write multiple-fd model, the status struct includes |
| 679 | the fp regs too, so mark them valid too. */ |
| 680 | pi->fpregs_valid = pi->status_valid; |
| 681 | return pi->status_valid; /* True if success, false if failure. */ |
| 682 | } |
| 683 | |
| 684 | /* Returns the process flags (pr_flags field). */ |
| 685 | |
| 686 | static long |
| 687 | proc_flags (procinfo *pi) |
| 688 | { |
| 689 | if (!pi->status_valid) |
| 690 | if (!proc_get_status (pi)) |
| 691 | return 0; /* FIXME: not a good failure value (but what is?) */ |
| 692 | |
| 693 | return pi->prstatus.pr_lwp.pr_flags; |
| 694 | } |
| 695 | |
| 696 | /* Returns the pr_why field (why the process stopped). */ |
| 697 | |
| 698 | static int |
| 699 | proc_why (procinfo *pi) |
| 700 | { |
| 701 | if (!pi->status_valid) |
| 702 | if (!proc_get_status (pi)) |
| 703 | return 0; /* FIXME: not a good failure value (but what is?) */ |
| 704 | |
| 705 | return pi->prstatus.pr_lwp.pr_why; |
| 706 | } |
| 707 | |
| 708 | /* Returns the pr_what field (details of why the process stopped). */ |
| 709 | |
| 710 | static int |
| 711 | proc_what (procinfo *pi) |
| 712 | { |
| 713 | if (!pi->status_valid) |
| 714 | if (!proc_get_status (pi)) |
| 715 | return 0; /* FIXME: not a good failure value (but what is?) */ |
| 716 | |
| 717 | return pi->prstatus.pr_lwp.pr_what; |
| 718 | } |
| 719 | |
| 720 | /* This function is only called when PI is stopped by a watchpoint. |
| 721 | Assuming the OS supports it, write to *ADDR the data address which |
| 722 | triggered it and return 1. Return 0 if it is not possible to know |
| 723 | the address. */ |
| 724 | |
| 725 | static int |
| 726 | proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr) |
| 727 | { |
| 728 | if (!pi->status_valid) |
| 729 | if (!proc_get_status (pi)) |
| 730 | return 0; |
| 731 | |
| 732 | *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch (), |
| 733 | builtin_type (target_gdbarch ())->builtin_data_ptr, |
| 734 | (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr); |
| 735 | return 1; |
| 736 | } |
| 737 | |
| 738 | /* Returns the pr_nsysarg field (number of args to the current |
| 739 | syscall). */ |
| 740 | |
| 741 | static int |
| 742 | proc_nsysarg (procinfo *pi) |
| 743 | { |
| 744 | if (!pi->status_valid) |
| 745 | if (!proc_get_status (pi)) |
| 746 | return 0; |
| 747 | |
| 748 | return pi->prstatus.pr_lwp.pr_nsysarg; |
| 749 | } |
| 750 | |
| 751 | /* Returns the pr_sysarg field (pointer to the arguments of current |
| 752 | syscall). */ |
| 753 | |
| 754 | static long * |
| 755 | proc_sysargs (procinfo *pi) |
| 756 | { |
| 757 | if (!pi->status_valid) |
| 758 | if (!proc_get_status (pi)) |
| 759 | return NULL; |
| 760 | |
| 761 | return (long *) &pi->prstatus.pr_lwp.pr_sysarg; |
| 762 | } |
| 763 | |
| 764 | /* Set or reset any of the following process flags: |
| 765 | PR_FORK -- forked child will inherit trace flags |
| 766 | PR_RLC -- traced process runs when last /proc file closed. |
| 767 | PR_KLC -- traced process is killed when last /proc file closed. |
| 768 | PR_ASYNC -- LWP's get to run/stop independently. |
| 769 | |
| 770 | This function is done using read/write [PCSET/PCRESET/PCUNSET]. |
| 771 | |
| 772 | Arguments: |
| 773 | pi -- the procinfo |
| 774 | flag -- one of PR_FORK, PR_RLC, or PR_ASYNC |
| 775 | mode -- 1 for set, 0 for reset. |
| 776 | |
| 777 | Returns non-zero for success, zero for failure. */ |
| 778 | |
| 779 | enum { FLAG_RESET, FLAG_SET }; |
| 780 | |
| 781 | static int |
| 782 | proc_modify_flag (procinfo *pi, long flag, long mode) |
| 783 | { |
| 784 | long win = 0; /* default to fail */ |
| 785 | |
| 786 | /* These operations affect the process as a whole, and applying them |
| 787 | to an individual LWP has the same meaning as applying them to the |
| 788 | main process. Therefore, if we're ever called with a pointer to |
| 789 | an LWP's procinfo, let's substitute the process's procinfo and |
| 790 | avoid opening the LWP's file descriptor unnecessarily. */ |
| 791 | |
| 792 | if (pi->pid != 0) |
| 793 | pi = find_procinfo_or_die (pi->pid, 0); |
| 794 | |
| 795 | procfs_ctl_t arg[2]; |
| 796 | |
| 797 | if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */ |
| 798 | arg[0] = PCSET; |
| 799 | else /* Reset the flag. */ |
| 800 | arg[0] = PCUNSET; |
| 801 | |
| 802 | arg[1] = flag; |
| 803 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 804 | |
| 805 | /* The above operation renders the procinfo's cached pstatus |
| 806 | obsolete. */ |
| 807 | pi->status_valid = 0; |
| 808 | |
| 809 | if (!win) |
| 810 | warning (_("procfs: modify_flag failed to turn %s %s"), |
| 811 | flag == PR_FORK ? "PR_FORK" : |
| 812 | flag == PR_RLC ? "PR_RLC" : |
| 813 | flag == PR_ASYNC ? "PR_ASYNC" : |
| 814 | flag == PR_KLC ? "PR_KLC" : |
| 815 | "<unknown flag>", |
| 816 | mode == FLAG_RESET ? "off" : "on"); |
| 817 | |
| 818 | return win; |
| 819 | } |
| 820 | |
| 821 | /* Set the run_on_last_close flag. Process with all threads will |
| 822 | become runnable when debugger closes all /proc fds. Returns |
| 823 | non-zero for success, zero for failure. */ |
| 824 | |
| 825 | static int |
| 826 | proc_set_run_on_last_close (procinfo *pi) |
| 827 | { |
| 828 | return proc_modify_flag (pi, PR_RLC, FLAG_SET); |
| 829 | } |
| 830 | |
| 831 | /* Reset the run_on_last_close flag. The process will NOT become |
| 832 | runnable when debugger closes its file handles. Returns non-zero |
| 833 | for success, zero for failure. */ |
| 834 | |
| 835 | static int |
| 836 | proc_unset_run_on_last_close (procinfo *pi) |
| 837 | { |
| 838 | return proc_modify_flag (pi, PR_RLC, FLAG_RESET); |
| 839 | } |
| 840 | |
| 841 | /* Reset inherit_on_fork flag. If the process forks a child while we |
| 842 | are registered for events in the parent, then we will NOT recieve |
| 843 | events from the child. Returns non-zero for success, zero for |
| 844 | failure. */ |
| 845 | |
| 846 | static int |
| 847 | proc_unset_inherit_on_fork (procinfo *pi) |
| 848 | { |
| 849 | return proc_modify_flag (pi, PR_FORK, FLAG_RESET); |
| 850 | } |
| 851 | |
| 852 | /* Set PR_ASYNC flag. If one LWP stops because of a debug event |
| 853 | (signal etc.), the remaining LWPs will continue to run. Returns |
| 854 | non-zero for success, zero for failure. */ |
| 855 | |
| 856 | static int |
| 857 | proc_set_async (procinfo *pi) |
| 858 | { |
| 859 | return proc_modify_flag (pi, PR_ASYNC, FLAG_SET); |
| 860 | } |
| 861 | |
| 862 | /* Reset PR_ASYNC flag. If one LWP stops because of a debug event |
| 863 | (signal etc.), then all other LWPs will stop as well. Returns |
| 864 | non-zero for success, zero for failure. */ |
| 865 | |
| 866 | static int |
| 867 | proc_unset_async (procinfo *pi) |
| 868 | { |
| 869 | return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET); |
| 870 | } |
| 871 | |
| 872 | /* Request the process/LWP to stop. Does not wait. Returns non-zero |
| 873 | for success, zero for failure. */ |
| 874 | |
| 875 | static int |
| 876 | proc_stop_process (procinfo *pi) |
| 877 | { |
| 878 | int win; |
| 879 | |
| 880 | /* We might conceivably apply this operation to an LWP, and the |
| 881 | LWP's ctl file descriptor might not be open. */ |
| 882 | |
| 883 | if (pi->ctl_fd == 0 && |
| 884 | open_procinfo_files (pi, FD_CTL) == 0) |
| 885 | return 0; |
| 886 | else |
| 887 | { |
| 888 | procfs_ctl_t cmd = PCSTOP; |
| 889 | |
| 890 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
| 891 | } |
| 892 | |
| 893 | return win; |
| 894 | } |
| 895 | |
| 896 | /* Wait for the process or LWP to stop (block until it does). Returns |
| 897 | non-zero for success, zero for failure. */ |
| 898 | |
| 899 | static int |
| 900 | proc_wait_for_stop (procinfo *pi) |
| 901 | { |
| 902 | int win; |
| 903 | |
| 904 | /* We should never have to apply this operation to any procinfo |
| 905 | except the one for the main process. If that ever changes for |
| 906 | any reason, then take out the following clause and replace it |
| 907 | with one that makes sure the ctl_fd is open. */ |
| 908 | |
| 909 | if (pi->tid != 0) |
| 910 | pi = find_procinfo_or_die (pi->pid, 0); |
| 911 | |
| 912 | procfs_ctl_t cmd = PCWSTOP; |
| 913 | |
| 914 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
| 915 | /* We been runnin' and we stopped -- need to update status. */ |
| 916 | pi->status_valid = 0; |
| 917 | |
| 918 | return win; |
| 919 | } |
| 920 | |
| 921 | /* Make the process or LWP runnable. |
| 922 | |
| 923 | Options (not all are implemented): |
| 924 | - single-step |
| 925 | - clear current fault |
| 926 | - clear current signal |
| 927 | - abort the current system call |
| 928 | - stop as soon as finished with system call |
| 929 | - (ioctl): set traced signal set |
| 930 | - (ioctl): set held signal set |
| 931 | - (ioctl): set traced fault set |
| 932 | - (ioctl): set start pc (vaddr) |
| 933 | |
| 934 | Always clears the current fault. PI is the process or LWP to |
| 935 | operate on. If STEP is true, set the process or LWP to trap after |
| 936 | one instruction. If SIGNO is zero, clear the current signal if |
| 937 | any; if non-zero, set the current signal to this one. Returns |
| 938 | non-zero for success, zero for failure. */ |
| 939 | |
| 940 | static int |
| 941 | proc_run_process (procinfo *pi, int step, int signo) |
| 942 | { |
| 943 | int win; |
| 944 | int runflags; |
| 945 | |
| 946 | /* We will probably have to apply this operation to individual |
| 947 | threads, so make sure the control file descriptor is open. */ |
| 948 | |
| 949 | if (pi->ctl_fd == 0 && |
| 950 | open_procinfo_files (pi, FD_CTL) == 0) |
| 951 | { |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | runflags = PRCFAULT; /* Always clear current fault. */ |
| 956 | if (step) |
| 957 | runflags |= PRSTEP; |
| 958 | if (signo == 0) |
| 959 | runflags |= PRCSIG; |
| 960 | else if (signo != -1) /* -1 means do nothing W.R.T. signals. */ |
| 961 | proc_set_current_signal (pi, signo); |
| 962 | |
| 963 | procfs_ctl_t cmd[2]; |
| 964 | |
| 965 | cmd[0] = PCRUN; |
| 966 | cmd[1] = runflags; |
| 967 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
| 968 | |
| 969 | return win; |
| 970 | } |
| 971 | |
| 972 | /* Register to trace signals in the process or LWP. Returns non-zero |
| 973 | for success, zero for failure. */ |
| 974 | |
| 975 | static int |
| 976 | proc_set_traced_signals (procinfo *pi, sigset_t *sigset) |
| 977 | { |
| 978 | int win; |
| 979 | |
| 980 | /* We should never have to apply this operation to any procinfo |
| 981 | except the one for the main process. If that ever changes for |
| 982 | any reason, then take out the following clause and replace it |
| 983 | with one that makes sure the ctl_fd is open. */ |
| 984 | |
| 985 | if (pi->tid != 0) |
| 986 | pi = find_procinfo_or_die (pi->pid, 0); |
| 987 | |
| 988 | struct { |
| 989 | procfs_ctl_t cmd; |
| 990 | /* Use char array to avoid alignment issues. */ |
| 991 | char sigset[sizeof (sigset_t)]; |
| 992 | } arg; |
| 993 | |
| 994 | arg.cmd = PCSTRACE; |
| 995 | memcpy (&arg.sigset, sigset, sizeof (sigset_t)); |
| 996 | |
| 997 | win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); |
| 998 | |
| 999 | /* The above operation renders the procinfo's cached pstatus obsolete. */ |
| 1000 | pi->status_valid = 0; |
| 1001 | |
| 1002 | if (!win) |
| 1003 | warning (_("procfs: set_traced_signals failed")); |
| 1004 | return win; |
| 1005 | } |
| 1006 | |
| 1007 | /* Register to trace hardware faults in the process or LWP. Returns |
| 1008 | non-zero for success, zero for failure. */ |
| 1009 | |
| 1010 | static int |
| 1011 | proc_set_traced_faults (procinfo *pi, fltset_t *fltset) |
| 1012 | { |
| 1013 | int win; |
| 1014 | |
| 1015 | /* We should never have to apply this operation to any procinfo |
| 1016 | except the one for the main process. If that ever changes for |
| 1017 | any reason, then take out the following clause and replace it |
| 1018 | with one that makes sure the ctl_fd is open. */ |
| 1019 | |
| 1020 | if (pi->tid != 0) |
| 1021 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1022 | |
| 1023 | struct { |
| 1024 | procfs_ctl_t cmd; |
| 1025 | /* Use char array to avoid alignment issues. */ |
| 1026 | char fltset[sizeof (fltset_t)]; |
| 1027 | } arg; |
| 1028 | |
| 1029 | arg.cmd = PCSFAULT; |
| 1030 | memcpy (&arg.fltset, fltset, sizeof (fltset_t)); |
| 1031 | |
| 1032 | win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1033 | |
| 1034 | /* The above operation renders the procinfo's cached pstatus obsolete. */ |
| 1035 | pi->status_valid = 0; |
| 1036 | |
| 1037 | return win; |
| 1038 | } |
| 1039 | |
| 1040 | /* Register to trace entry to system calls in the process or LWP. |
| 1041 | Returns non-zero for success, zero for failure. */ |
| 1042 | |
| 1043 | static int |
| 1044 | proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset) |
| 1045 | { |
| 1046 | int win; |
| 1047 | |
| 1048 | /* We should never have to apply this operation to any procinfo |
| 1049 | except the one for the main process. If that ever changes for |
| 1050 | any reason, then take out the following clause and replace it |
| 1051 | with one that makes sure the ctl_fd is open. */ |
| 1052 | |
| 1053 | if (pi->tid != 0) |
| 1054 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1055 | |
| 1056 | struct gdb_proc_ctl_pcsentry { |
| 1057 | procfs_ctl_t cmd; |
| 1058 | /* Use char array to avoid alignment issues. */ |
| 1059 | char sysset[sizeof (sysset_t)]; |
| 1060 | } *argp; |
| 1061 | int argp_size = sizeof (struct gdb_proc_ctl_pcsentry); |
| 1062 | |
| 1063 | argp = (struct gdb_proc_ctl_pcsentry *) xmalloc (argp_size); |
| 1064 | |
| 1065 | argp->cmd = PCSENTRY; |
| 1066 | memcpy (&argp->sysset, sysset, sizeof (sysset_t)); |
| 1067 | |
| 1068 | win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); |
| 1069 | xfree (argp); |
| 1070 | |
| 1071 | /* The above operation renders the procinfo's cached pstatus |
| 1072 | obsolete. */ |
| 1073 | pi->status_valid = 0; |
| 1074 | |
| 1075 | return win; |
| 1076 | } |
| 1077 | |
| 1078 | /* Register to trace exit from system calls in the process or LWP. |
| 1079 | Returns non-zero for success, zero for failure. */ |
| 1080 | |
| 1081 | static int |
| 1082 | proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset) |
| 1083 | { |
| 1084 | int win; |
| 1085 | |
| 1086 | /* We should never have to apply this operation to any procinfo |
| 1087 | except the one for the main process. If that ever changes for |
| 1088 | any reason, then take out the following clause and replace it |
| 1089 | with one that makes sure the ctl_fd is open. */ |
| 1090 | |
| 1091 | if (pi->tid != 0) |
| 1092 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1093 | |
| 1094 | struct gdb_proc_ctl_pcsexit { |
| 1095 | procfs_ctl_t cmd; |
| 1096 | /* Use char array to avoid alignment issues. */ |
| 1097 | char sysset[sizeof (sysset_t)]; |
| 1098 | } *argp; |
| 1099 | int argp_size = sizeof (struct gdb_proc_ctl_pcsexit); |
| 1100 | |
| 1101 | argp = (struct gdb_proc_ctl_pcsexit *) xmalloc (argp_size); |
| 1102 | |
| 1103 | argp->cmd = PCSEXIT; |
| 1104 | memcpy (&argp->sysset, sysset, sizeof (sysset_t)); |
| 1105 | |
| 1106 | win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); |
| 1107 | xfree (argp); |
| 1108 | |
| 1109 | /* The above operation renders the procinfo's cached pstatus |
| 1110 | obsolete. */ |
| 1111 | pi->status_valid = 0; |
| 1112 | |
| 1113 | return win; |
| 1114 | } |
| 1115 | |
| 1116 | /* Specify the set of blocked / held signals in the process or LWP. |
| 1117 | Returns non-zero for success, zero for failure. */ |
| 1118 | |
| 1119 | static int |
| 1120 | proc_set_held_signals (procinfo *pi, sigset_t *sighold) |
| 1121 | { |
| 1122 | int win; |
| 1123 | |
| 1124 | /* We should never have to apply this operation to any procinfo |
| 1125 | except the one for the main process. If that ever changes for |
| 1126 | any reason, then take out the following clause and replace it |
| 1127 | with one that makes sure the ctl_fd is open. */ |
| 1128 | |
| 1129 | if (pi->tid != 0) |
| 1130 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1131 | |
| 1132 | struct { |
| 1133 | procfs_ctl_t cmd; |
| 1134 | /* Use char array to avoid alignment issues. */ |
| 1135 | char hold[sizeof (sigset_t)]; |
| 1136 | } arg; |
| 1137 | |
| 1138 | arg.cmd = PCSHOLD; |
| 1139 | memcpy (&arg.hold, sighold, sizeof (sigset_t)); |
| 1140 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1141 | |
| 1142 | /* The above operation renders the procinfo's cached pstatus |
| 1143 | obsolete. */ |
| 1144 | pi->status_valid = 0; |
| 1145 | |
| 1146 | return win; |
| 1147 | } |
| 1148 | |
| 1149 | /* Returns the set of signals that are held / blocked. Will also copy |
| 1150 | the sigset if SAVE is non-zero. */ |
| 1151 | |
| 1152 | static sigset_t * |
| 1153 | proc_get_held_signals (procinfo *pi, sigset_t *save) |
| 1154 | { |
| 1155 | sigset_t *ret = NULL; |
| 1156 | |
| 1157 | /* We should never have to apply this operation to any procinfo |
| 1158 | except the one for the main process. If that ever changes for |
| 1159 | any reason, then take out the following clause and replace it |
| 1160 | with one that makes sure the ctl_fd is open. */ |
| 1161 | |
| 1162 | if (pi->tid != 0) |
| 1163 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1164 | |
| 1165 | if (!pi->status_valid) |
| 1166 | if (!proc_get_status (pi)) |
| 1167 | return NULL; |
| 1168 | |
| 1169 | ret = &pi->prstatus.pr_lwp.pr_lwphold; |
| 1170 | if (save && ret) |
| 1171 | memcpy (save, ret, sizeof (sigset_t)); |
| 1172 | |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | /* Returns the set of signals that are traced / debugged. Will also |
| 1177 | copy the sigset if SAVE is non-zero. */ |
| 1178 | |
| 1179 | static sigset_t * |
| 1180 | proc_get_traced_signals (procinfo *pi, sigset_t *save) |
| 1181 | { |
| 1182 | sigset_t *ret = NULL; |
| 1183 | |
| 1184 | /* We should never have to apply this operation to any procinfo |
| 1185 | except the one for the main process. If that ever changes for |
| 1186 | any reason, then take out the following clause and replace it |
| 1187 | with one that makes sure the ctl_fd is open. */ |
| 1188 | |
| 1189 | if (pi->tid != 0) |
| 1190 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1191 | |
| 1192 | if (!pi->status_valid) |
| 1193 | if (!proc_get_status (pi)) |
| 1194 | return NULL; |
| 1195 | |
| 1196 | ret = &pi->prstatus.pr_sigtrace; |
| 1197 | if (save && ret) |
| 1198 | memcpy (save, ret, sizeof (sigset_t)); |
| 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | /* Returns the set of hardware faults that are traced /debugged. Will |
| 1204 | also copy the faultset if SAVE is non-zero. */ |
| 1205 | |
| 1206 | static fltset_t * |
| 1207 | proc_get_traced_faults (procinfo *pi, fltset_t *save) |
| 1208 | { |
| 1209 | fltset_t *ret = NULL; |
| 1210 | |
| 1211 | /* We should never have to apply this operation to any procinfo |
| 1212 | except the one for the main process. If that ever changes for |
| 1213 | any reason, then take out the following clause and replace it |
| 1214 | with one that makes sure the ctl_fd is open. */ |
| 1215 | |
| 1216 | if (pi->tid != 0) |
| 1217 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1218 | |
| 1219 | if (!pi->status_valid) |
| 1220 | if (!proc_get_status (pi)) |
| 1221 | return NULL; |
| 1222 | |
| 1223 | ret = &pi->prstatus.pr_flttrace; |
| 1224 | if (save && ret) |
| 1225 | memcpy (save, ret, sizeof (fltset_t)); |
| 1226 | |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | /* Returns the set of syscalls that are traced /debugged on entry. |
| 1231 | Will also copy the syscall set if SAVE is non-zero. */ |
| 1232 | |
| 1233 | static sysset_t * |
| 1234 | proc_get_traced_sysentry (procinfo *pi, sysset_t *save) |
| 1235 | { |
| 1236 | sysset_t *ret = NULL; |
| 1237 | |
| 1238 | /* We should never have to apply this operation to any procinfo |
| 1239 | except the one for the main process. If that ever changes for |
| 1240 | any reason, then take out the following clause and replace it |
| 1241 | with one that makes sure the ctl_fd is open. */ |
| 1242 | |
| 1243 | if (pi->tid != 0) |
| 1244 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1245 | |
| 1246 | if (!pi->status_valid) |
| 1247 | if (!proc_get_status (pi)) |
| 1248 | return NULL; |
| 1249 | |
| 1250 | ret = &pi->prstatus.pr_sysentry; |
| 1251 | if (save && ret) |
| 1252 | memcpy (save, ret, sizeof (sysset_t)); |
| 1253 | |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | /* Returns the set of syscalls that are traced /debugged on exit. |
| 1258 | Will also copy the syscall set if SAVE is non-zero. */ |
| 1259 | |
| 1260 | static sysset_t * |
| 1261 | proc_get_traced_sysexit (procinfo *pi, sysset_t *save) |
| 1262 | { |
| 1263 | sysset_t *ret = NULL; |
| 1264 | |
| 1265 | /* We should never have to apply this operation to any procinfo |
| 1266 | except the one for the main process. If that ever changes for |
| 1267 | any reason, then take out the following clause and replace it |
| 1268 | with one that makes sure the ctl_fd is open. */ |
| 1269 | |
| 1270 | if (pi->tid != 0) |
| 1271 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1272 | |
| 1273 | if (!pi->status_valid) |
| 1274 | if (!proc_get_status (pi)) |
| 1275 | return NULL; |
| 1276 | |
| 1277 | ret = &pi->prstatus.pr_sysexit; |
| 1278 | if (save && ret) |
| 1279 | memcpy (save, ret, sizeof (sysset_t)); |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | /* The current fault (if any) is cleared; the associated signal will |
| 1285 | not be sent to the process or LWP when it resumes. Returns |
| 1286 | non-zero for success, zero for failure. */ |
| 1287 | |
| 1288 | static int |
| 1289 | proc_clear_current_fault (procinfo *pi) |
| 1290 | { |
| 1291 | int win; |
| 1292 | |
| 1293 | /* We should never have to apply this operation to any procinfo |
| 1294 | except the one for the main process. If that ever changes for |
| 1295 | any reason, then take out the following clause and replace it |
| 1296 | with one that makes sure the ctl_fd is open. */ |
| 1297 | |
| 1298 | if (pi->tid != 0) |
| 1299 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1300 | |
| 1301 | procfs_ctl_t cmd = PCCFAULT; |
| 1302 | |
| 1303 | win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
| 1304 | |
| 1305 | return win; |
| 1306 | } |
| 1307 | |
| 1308 | /* Set the "current signal" that will be delivered next to the |
| 1309 | process. NOTE: semantics are different from those of KILL. This |
| 1310 | signal will be delivered to the process or LWP immediately when it |
| 1311 | is resumed (even if the signal is held/blocked); it will NOT |
| 1312 | immediately cause another event of interest, and will NOT first |
| 1313 | trap back to the debugger. Returns non-zero for success, zero for |
| 1314 | failure. */ |
| 1315 | |
| 1316 | static int |
| 1317 | proc_set_current_signal (procinfo *pi, int signo) |
| 1318 | { |
| 1319 | int win; |
| 1320 | struct { |
| 1321 | procfs_ctl_t cmd; |
| 1322 | /* Use char array to avoid alignment issues. */ |
| 1323 | char sinfo[sizeof (siginfo_t)]; |
| 1324 | } arg; |
| 1325 | siginfo_t mysinfo; |
| 1326 | ptid_t wait_ptid; |
| 1327 | struct target_waitstatus wait_status; |
| 1328 | |
| 1329 | /* We should never have to apply this operation to any procinfo |
| 1330 | except the one for the main process. If that ever changes for |
| 1331 | any reason, then take out the following clause and replace it |
| 1332 | with one that makes sure the ctl_fd is open. */ |
| 1333 | |
| 1334 | if (pi->tid != 0) |
| 1335 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1336 | |
| 1337 | /* The pointer is just a type alias. */ |
| 1338 | get_last_target_status (&wait_ptid, &wait_status); |
| 1339 | if (ptid_equal (wait_ptid, inferior_ptid) |
| 1340 | && wait_status.kind == TARGET_WAITKIND_STOPPED |
| 1341 | && wait_status.value.sig == gdb_signal_from_host (signo) |
| 1342 | && proc_get_status (pi) |
| 1343 | && pi->prstatus.pr_lwp.pr_info.si_signo == signo |
| 1344 | ) |
| 1345 | /* Use the siginfo associated with the signal being |
| 1346 | redelivered. */ |
| 1347 | memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (siginfo_t)); |
| 1348 | else |
| 1349 | { |
| 1350 | mysinfo.si_signo = signo; |
| 1351 | mysinfo.si_code = 0; |
| 1352 | mysinfo.si_pid = getpid (); /* ?why? */ |
| 1353 | mysinfo.si_uid = getuid (); /* ?why? */ |
| 1354 | memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t)); |
| 1355 | } |
| 1356 | |
| 1357 | arg.cmd = PCSSIG; |
| 1358 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1359 | |
| 1360 | return win; |
| 1361 | } |
| 1362 | |
| 1363 | /* The current signal (if any) is cleared, and is not sent to the |
| 1364 | process or LWP when it resumes. Returns non-zero for success, zero |
| 1365 | for failure. */ |
| 1366 | |
| 1367 | static int |
| 1368 | proc_clear_current_signal (procinfo *pi) |
| 1369 | { |
| 1370 | int win; |
| 1371 | |
| 1372 | /* We should never have to apply this operation to any procinfo |
| 1373 | except the one for the main process. If that ever changes for |
| 1374 | any reason, then take out the following clause and replace it |
| 1375 | with one that makes sure the ctl_fd is open. */ |
| 1376 | |
| 1377 | if (pi->tid != 0) |
| 1378 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1379 | |
| 1380 | struct { |
| 1381 | procfs_ctl_t cmd; |
| 1382 | /* Use char array to avoid alignment issues. */ |
| 1383 | char sinfo[sizeof (siginfo_t)]; |
| 1384 | } arg; |
| 1385 | siginfo_t mysinfo; |
| 1386 | |
| 1387 | arg.cmd = PCSSIG; |
| 1388 | /* The pointer is just a type alias. */ |
| 1389 | mysinfo.si_signo = 0; |
| 1390 | mysinfo.si_code = 0; |
| 1391 | mysinfo.si_errno = 0; |
| 1392 | mysinfo.si_pid = getpid (); /* ?why? */ |
| 1393 | mysinfo.si_uid = getuid (); /* ?why? */ |
| 1394 | memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t)); |
| 1395 | |
| 1396 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1397 | |
| 1398 | return win; |
| 1399 | } |
| 1400 | |
| 1401 | /* Return the general-purpose registers for the process or LWP |
| 1402 | corresponding to PI. Upon failure, return NULL. */ |
| 1403 | |
| 1404 | static gdb_gregset_t * |
| 1405 | proc_get_gregs (procinfo *pi) |
| 1406 | { |
| 1407 | if (!pi->status_valid || !pi->gregs_valid) |
| 1408 | if (!proc_get_status (pi)) |
| 1409 | return NULL; |
| 1410 | |
| 1411 | return &pi->prstatus.pr_lwp.pr_reg; |
| 1412 | } |
| 1413 | |
| 1414 | /* Return the general-purpose registers for the process or LWP |
| 1415 | corresponding to PI. Upon failure, return NULL. */ |
| 1416 | |
| 1417 | static gdb_fpregset_t * |
| 1418 | proc_get_fpregs (procinfo *pi) |
| 1419 | { |
| 1420 | if (!pi->status_valid || !pi->fpregs_valid) |
| 1421 | if (!proc_get_status (pi)) |
| 1422 | return NULL; |
| 1423 | |
| 1424 | return &pi->prstatus.pr_lwp.pr_fpreg; |
| 1425 | } |
| 1426 | |
| 1427 | /* Write the general-purpose registers back to the process or LWP |
| 1428 | corresponding to PI. Return non-zero for success, zero for |
| 1429 | failure. */ |
| 1430 | |
| 1431 | static int |
| 1432 | proc_set_gregs (procinfo *pi) |
| 1433 | { |
| 1434 | gdb_gregset_t *gregs; |
| 1435 | int win; |
| 1436 | |
| 1437 | gregs = proc_get_gregs (pi); |
| 1438 | if (gregs == NULL) |
| 1439 | return 0; /* proc_get_regs has already warned. */ |
| 1440 | |
| 1441 | if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0) |
| 1442 | { |
| 1443 | return 0; |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | struct { |
| 1448 | procfs_ctl_t cmd; |
| 1449 | /* Use char array to avoid alignment issues. */ |
| 1450 | char gregs[sizeof (gdb_gregset_t)]; |
| 1451 | } arg; |
| 1452 | |
| 1453 | arg.cmd = PCSREG; |
| 1454 | memcpy (&arg.gregs, gregs, sizeof (arg.gregs)); |
| 1455 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1456 | } |
| 1457 | |
| 1458 | /* Policy: writing the registers invalidates our cache. */ |
| 1459 | pi->gregs_valid = 0; |
| 1460 | return win; |
| 1461 | } |
| 1462 | |
| 1463 | /* Write the floating-pointer registers back to the process or LWP |
| 1464 | corresponding to PI. Return non-zero for success, zero for |
| 1465 | failure. */ |
| 1466 | |
| 1467 | static int |
| 1468 | proc_set_fpregs (procinfo *pi) |
| 1469 | { |
| 1470 | gdb_fpregset_t *fpregs; |
| 1471 | int win; |
| 1472 | |
| 1473 | fpregs = proc_get_fpregs (pi); |
| 1474 | if (fpregs == NULL) |
| 1475 | return 0; /* proc_get_fpregs has already warned. */ |
| 1476 | |
| 1477 | if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0) |
| 1478 | { |
| 1479 | return 0; |
| 1480 | } |
| 1481 | else |
| 1482 | { |
| 1483 | struct { |
| 1484 | procfs_ctl_t cmd; |
| 1485 | /* Use char array to avoid alignment issues. */ |
| 1486 | char fpregs[sizeof (gdb_fpregset_t)]; |
| 1487 | } arg; |
| 1488 | |
| 1489 | arg.cmd = PCSFPREG; |
| 1490 | memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs)); |
| 1491 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
| 1492 | } |
| 1493 | |
| 1494 | /* Policy: writing the registers invalidates our cache. */ |
| 1495 | pi->fpregs_valid = 0; |
| 1496 | return win; |
| 1497 | } |
| 1498 | |
| 1499 | /* Send a signal to the proc or lwp with the semantics of "kill()". |
| 1500 | Returns non-zero for success, zero for failure. */ |
| 1501 | |
| 1502 | static int |
| 1503 | proc_kill (procinfo *pi, int signo) |
| 1504 | { |
| 1505 | int win; |
| 1506 | |
| 1507 | /* We might conceivably apply this operation to an LWP, and the |
| 1508 | LWP's ctl file descriptor might not be open. */ |
| 1509 | |
| 1510 | if (pi->ctl_fd == 0 && |
| 1511 | open_procinfo_files (pi, FD_CTL) == 0) |
| 1512 | { |
| 1513 | return 0; |
| 1514 | } |
| 1515 | else |
| 1516 | { |
| 1517 | procfs_ctl_t cmd[2]; |
| 1518 | |
| 1519 | cmd[0] = PCKILL; |
| 1520 | cmd[1] = signo; |
| 1521 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
| 1522 | } |
| 1523 | |
| 1524 | return win; |
| 1525 | } |
| 1526 | |
| 1527 | /* Find the pid of the process that started this one. Returns the |
| 1528 | parent process pid, or zero. */ |
| 1529 | |
| 1530 | static int |
| 1531 | proc_parent_pid (procinfo *pi) |
| 1532 | { |
| 1533 | /* We should never have to apply this operation to any procinfo |
| 1534 | except the one for the main process. If that ever changes for |
| 1535 | any reason, then take out the following clause and replace it |
| 1536 | with one that makes sure the ctl_fd is open. */ |
| 1537 | |
| 1538 | if (pi->tid != 0) |
| 1539 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1540 | |
| 1541 | if (!pi->status_valid) |
| 1542 | if (!proc_get_status (pi)) |
| 1543 | return 0; |
| 1544 | |
| 1545 | return pi->prstatus.pr_ppid; |
| 1546 | } |
| 1547 | |
| 1548 | /* Convert a target address (a.k.a. CORE_ADDR) into a host address |
| 1549 | (a.k.a void pointer)! */ |
| 1550 | |
| 1551 | static void * |
| 1552 | procfs_address_to_host_pointer (CORE_ADDR addr) |
| 1553 | { |
| 1554 | struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; |
| 1555 | void *ptr; |
| 1556 | |
| 1557 | gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type)); |
| 1558 | gdbarch_address_to_pointer (target_gdbarch (), ptr_type, |
| 1559 | (gdb_byte *) &ptr, addr); |
| 1560 | return ptr; |
| 1561 | } |
| 1562 | |
| 1563 | static int |
| 1564 | proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags) |
| 1565 | { |
| 1566 | struct { |
| 1567 | procfs_ctl_t cmd; |
| 1568 | char watch[sizeof (prwatch_t)]; |
| 1569 | } arg; |
| 1570 | prwatch_t pwatch; |
| 1571 | |
| 1572 | /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to |
| 1573 | convert a target address into something that can be stored in a |
| 1574 | native data structure. */ |
| 1575 | pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr); |
| 1576 | pwatch.pr_size = len; |
| 1577 | pwatch.pr_wflags = wflags; |
| 1578 | arg.cmd = PCWATCH; |
| 1579 | memcpy (arg.watch, &pwatch, sizeof (prwatch_t)); |
| 1580 | return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg)); |
| 1581 | } |
| 1582 | |
| 1583 | #if (defined(__i386__) || defined(__x86_64__)) && defined (sun) |
| 1584 | |
| 1585 | #include <sys/sysi86.h> |
| 1586 | |
| 1587 | /* The KEY is actually the value of the lower 16 bits of the GS |
| 1588 | register for the LWP that we're interested in. Returns the |
| 1589 | matching ssh struct (LDT entry). */ |
| 1590 | |
| 1591 | static struct ssd * |
| 1592 | proc_get_LDT_entry (procinfo *pi, int key) |
| 1593 | { |
| 1594 | static struct ssd *ldt_entry = NULL; |
| 1595 | char pathname[MAX_PROC_NAME_SIZE]; |
| 1596 | |
| 1597 | /* Allocate space for one LDT entry. |
| 1598 | This alloc must persist, because we return a pointer to it. */ |
| 1599 | if (ldt_entry == NULL) |
| 1600 | ldt_entry = XNEW (struct ssd); |
| 1601 | |
| 1602 | /* Open the file descriptor for the LDT table. */ |
| 1603 | sprintf (pathname, "/proc/%d/ldt", pi->pid); |
| 1604 | scoped_fd fd (open_with_retry (pathname, O_RDONLY)); |
| 1605 | if (fd.get () < 0) |
| 1606 | { |
| 1607 | proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__); |
| 1608 | return NULL; |
| 1609 | } |
| 1610 | |
| 1611 | /* Now 'read' thru the table, find a match and return it. */ |
| 1612 | while (read (fd.get (), ldt_entry, sizeof (struct ssd)) |
| 1613 | == sizeof (struct ssd)) |
| 1614 | { |
| 1615 | if (ldt_entry->sel == 0 && |
| 1616 | ldt_entry->bo == 0 && |
| 1617 | ldt_entry->acc1 == 0 && |
| 1618 | ldt_entry->acc2 == 0) |
| 1619 | break; /* end of table */ |
| 1620 | /* If key matches, return this entry. */ |
| 1621 | if (ldt_entry->sel == key) |
| 1622 | return ldt_entry; |
| 1623 | } |
| 1624 | /* Loop ended, match not found. */ |
| 1625 | return NULL; |
| 1626 | } |
| 1627 | |
| 1628 | /* Returns the pointer to the LDT entry of PTID. */ |
| 1629 | |
| 1630 | struct ssd * |
| 1631 | procfs_find_LDT_entry (ptid_t ptid) |
| 1632 | { |
| 1633 | gdb_gregset_t *gregs; |
| 1634 | int key; |
| 1635 | procinfo *pi; |
| 1636 | |
| 1637 | /* Find procinfo for the lwp. */ |
| 1638 | if ((pi = find_procinfo (ptid_get_pid (ptid), ptid_get_lwp (ptid))) == NULL) |
| 1639 | { |
| 1640 | warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."), |
| 1641 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); |
| 1642 | return NULL; |
| 1643 | } |
| 1644 | /* get its general registers. */ |
| 1645 | if ((gregs = proc_get_gregs (pi)) == NULL) |
| 1646 | { |
| 1647 | warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."), |
| 1648 | ptid_get_pid (ptid), ptid_get_lwp (ptid)); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | /* Now extract the GS register's lower 16 bits. */ |
| 1652 | key = (*gregs)[GS] & 0xffff; |
| 1653 | |
| 1654 | /* Find the matching entry and return it. */ |
| 1655 | return proc_get_LDT_entry (pi, key); |
| 1656 | } |
| 1657 | |
| 1658 | #endif |
| 1659 | |
| 1660 | /* =============== END, non-thread part of /proc "MODULE" =============== */ |
| 1661 | |
| 1662 | /* =================== Thread "MODULE" =================== */ |
| 1663 | |
| 1664 | /* NOTE: you'll see more ifdefs and duplication of functions here, |
| 1665 | since there is a different way to do threads on every OS. */ |
| 1666 | |
| 1667 | /* Returns the number of threads for the process. */ |
| 1668 | |
| 1669 | static int |
| 1670 | proc_get_nthreads (procinfo *pi) |
| 1671 | { |
| 1672 | if (!pi->status_valid) |
| 1673 | if (!proc_get_status (pi)) |
| 1674 | return 0; |
| 1675 | |
| 1676 | /* Only works for the process procinfo, because the LWP procinfos do not |
| 1677 | get prstatus filled in. */ |
| 1678 | if (pi->tid != 0) /* Find the parent process procinfo. */ |
| 1679 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1680 | return pi->prstatus.pr_nlwp; |
| 1681 | } |
| 1682 | |
| 1683 | /* LWP version. |
| 1684 | |
| 1685 | Return the ID of the thread that had an event of interest. |
| 1686 | (ie. the one that hit a breakpoint or other traced event). All |
| 1687 | other things being equal, this should be the ID of a thread that is |
| 1688 | currently executing. */ |
| 1689 | |
| 1690 | static int |
| 1691 | proc_get_current_thread (procinfo *pi) |
| 1692 | { |
| 1693 | /* Note: this should be applied to the root procinfo for the |
| 1694 | process, not to the procinfo for an LWP. If applied to the |
| 1695 | procinfo for an LWP, it will simply return that LWP's ID. In |
| 1696 | that case, find the parent process procinfo. */ |
| 1697 | |
| 1698 | if (pi->tid != 0) |
| 1699 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1700 | |
| 1701 | if (!pi->status_valid) |
| 1702 | if (!proc_get_status (pi)) |
| 1703 | return 0; |
| 1704 | |
| 1705 | return pi->prstatus.pr_lwp.pr_lwpid; |
| 1706 | } |
| 1707 | |
| 1708 | /* Discover the IDs of all the threads within the process, and create |
| 1709 | a procinfo for each of them (chained to the parent). This |
| 1710 | unfortunately requires a different method on every OS. Returns |
| 1711 | non-zero for success, zero for failure. */ |
| 1712 | |
| 1713 | static int |
| 1714 | proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore) |
| 1715 | { |
| 1716 | if (thread && parent) /* sanity */ |
| 1717 | { |
| 1718 | thread->status_valid = 0; |
| 1719 | if (!proc_get_status (thread)) |
| 1720 | destroy_one_procinfo (&parent->thread_list, thread); |
| 1721 | } |
| 1722 | return 0; /* keep iterating */ |
| 1723 | } |
| 1724 | |
| 1725 | static int |
| 1726 | proc_update_threads (procinfo *pi) |
| 1727 | { |
| 1728 | char pathname[MAX_PROC_NAME_SIZE + 16]; |
| 1729 | struct dirent *direntry; |
| 1730 | procinfo *thread; |
| 1731 | gdb_dir_up dirp; |
| 1732 | int lwpid; |
| 1733 | |
| 1734 | /* We should never have to apply this operation to any procinfo |
| 1735 | except the one for the main process. If that ever changes for |
| 1736 | any reason, then take out the following clause and replace it |
| 1737 | with one that makes sure the ctl_fd is open. */ |
| 1738 | |
| 1739 | if (pi->tid != 0) |
| 1740 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1741 | |
| 1742 | proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); |
| 1743 | |
| 1744 | /* Note: this brute-force method was originally devised for Unixware |
| 1745 | (support removed since), and will also work on Solaris 2.6 and |
| 1746 | 2.7. The original comment mentioned the existence of a much |
| 1747 | simpler and more elegant way to do this on Solaris, but didn't |
| 1748 | point out what that was. */ |
| 1749 | |
| 1750 | strcpy (pathname, pi->pathname); |
| 1751 | strcat (pathname, "/lwp"); |
| 1752 | dirp.reset (opendir (pathname)); |
| 1753 | if (dirp == NULL) |
| 1754 | proc_error (pi, "update_threads, opendir", __LINE__); |
| 1755 | |
| 1756 | while ((direntry = readdir (dirp.get ())) != NULL) |
| 1757 | if (direntry->d_name[0] != '.') /* skip '.' and '..' */ |
| 1758 | { |
| 1759 | lwpid = atoi (&direntry->d_name[0]); |
| 1760 | if ((thread = create_procinfo (pi->pid, lwpid)) == NULL) |
| 1761 | proc_error (pi, "update_threads, create_procinfo", __LINE__); |
| 1762 | } |
| 1763 | pi->threads_valid = 1; |
| 1764 | return 1; |
| 1765 | } |
| 1766 | |
| 1767 | /* Given a pointer to a function, call that function once for each lwp |
| 1768 | in the procinfo list, until the function returns non-zero, in which |
| 1769 | event return the value returned by the function. |
| 1770 | |
| 1771 | Note: this function does NOT call update_threads. If you want to |
| 1772 | discover new threads first, you must call that function explicitly. |
| 1773 | This function just makes a quick pass over the currently-known |
| 1774 | procinfos. |
| 1775 | |
| 1776 | PI is the parent process procinfo. FUNC is the per-thread |
| 1777 | function. PTR is an opaque parameter for function. Returns the |
| 1778 | first non-zero return value from the callee, or zero. */ |
| 1779 | |
| 1780 | static int |
| 1781 | proc_iterate_over_threads (procinfo *pi, |
| 1782 | int (*func) (procinfo *, procinfo *, void *), |
| 1783 | void *ptr) |
| 1784 | { |
| 1785 | procinfo *thread, *next; |
| 1786 | int retval = 0; |
| 1787 | |
| 1788 | /* We should never have to apply this operation to any procinfo |
| 1789 | except the one for the main process. If that ever changes for |
| 1790 | any reason, then take out the following clause and replace it |
| 1791 | with one that makes sure the ctl_fd is open. */ |
| 1792 | |
| 1793 | if (pi->tid != 0) |
| 1794 | pi = find_procinfo_or_die (pi->pid, 0); |
| 1795 | |
| 1796 | for (thread = pi->thread_list; thread != NULL; thread = next) |
| 1797 | { |
| 1798 | next = thread->next; /* In case thread is destroyed. */ |
| 1799 | if ((retval = (*func) (pi, thread, ptr)) != 0) |
| 1800 | break; |
| 1801 | } |
| 1802 | |
| 1803 | return retval; |
| 1804 | } |
| 1805 | |
| 1806 | /* =================== END, Thread "MODULE" =================== */ |
| 1807 | |
| 1808 | /* =================== END, /proc "MODULE" =================== */ |
| 1809 | |
| 1810 | /* =================== GDB "MODULE" =================== */ |
| 1811 | |
| 1812 | /* Here are all of the gdb target vector functions and their |
| 1813 | friends. */ |
| 1814 | |
| 1815 | static ptid_t do_attach (ptid_t ptid); |
| 1816 | static void do_detach (); |
| 1817 | static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum, |
| 1818 | int entry_or_exit, int mode, int from_tty); |
| 1819 | |
| 1820 | /* Sets up the inferior to be debugged. Registers to trace signals, |
| 1821 | hardware faults, and syscalls. Note: does not set RLC flag: caller |
| 1822 | may want to customize that. Returns zero for success (note! |
| 1823 | unlike most functions in this module); on failure, returns the LINE |
| 1824 | NUMBER where it failed! */ |
| 1825 | |
| 1826 | static int |
| 1827 | procfs_debug_inferior (procinfo *pi) |
| 1828 | { |
| 1829 | fltset_t traced_faults; |
| 1830 | sigset_t traced_signals; |
| 1831 | sysset_t *traced_syscall_entries; |
| 1832 | sysset_t *traced_syscall_exits; |
| 1833 | int status; |
| 1834 | |
| 1835 | /* Register to trace hardware faults in the child. */ |
| 1836 | prfillset (&traced_faults); /* trace all faults... */ |
| 1837 | prdelset (&traced_faults, FLTPAGE); /* except page fault. */ |
| 1838 | if (!proc_set_traced_faults (pi, &traced_faults)) |
| 1839 | return __LINE__; |
| 1840 | |
| 1841 | /* Initially, register to trace all signals in the child. */ |
| 1842 | prfillset (&traced_signals); |
| 1843 | if (!proc_set_traced_signals (pi, &traced_signals)) |
| 1844 | return __LINE__; |
| 1845 | |
| 1846 | |
| 1847 | /* Register to trace the 'exit' system call (on entry). */ |
| 1848 | traced_syscall_entries = sysset_t_alloc (pi); |
| 1849 | premptyset (traced_syscall_entries); |
| 1850 | praddset (traced_syscall_entries, SYS_exit); |
| 1851 | praddset (traced_syscall_entries, SYS_lwp_exit); |
| 1852 | |
| 1853 | status = proc_set_traced_sysentry (pi, traced_syscall_entries); |
| 1854 | xfree (traced_syscall_entries); |
| 1855 | if (!status) |
| 1856 | return __LINE__; |
| 1857 | |
| 1858 | /* Method for tracing exec syscalls. */ |
| 1859 | /* GW: Rationale... |
| 1860 | Not all systems with /proc have all the exec* syscalls with the same |
| 1861 | names. On the SGI, for example, there is no SYS_exec, but there |
| 1862 | *is* a SYS_execv. So, we try to account for that. */ |
| 1863 | |
| 1864 | traced_syscall_exits = sysset_t_alloc (pi); |
| 1865 | premptyset (traced_syscall_exits); |
| 1866 | #ifdef SYS_exec |
| 1867 | praddset (traced_syscall_exits, SYS_exec); |
| 1868 | #endif |
| 1869 | praddset (traced_syscall_exits, SYS_execve); |
| 1870 | praddset (traced_syscall_exits, SYS_lwp_create); |
| 1871 | praddset (traced_syscall_exits, SYS_lwp_exit); |
| 1872 | |
| 1873 | status = proc_set_traced_sysexit (pi, traced_syscall_exits); |
| 1874 | xfree (traced_syscall_exits); |
| 1875 | if (!status) |
| 1876 | return __LINE__; |
| 1877 | |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | void |
| 1882 | procfs_target::attach (const char *args, int from_tty) |
| 1883 | { |
| 1884 | char *exec_file; |
| 1885 | int pid; |
| 1886 | |
| 1887 | pid = parse_pid_to_attach (args); |
| 1888 | |
| 1889 | if (pid == getpid ()) |
| 1890 | error (_("Attaching GDB to itself is not a good idea...")); |
| 1891 | |
| 1892 | if (from_tty) |
| 1893 | { |
| 1894 | exec_file = get_exec_file (0); |
| 1895 | |
| 1896 | if (exec_file) |
| 1897 | printf_filtered (_("Attaching to program `%s', %s\n"), |
| 1898 | exec_file, target_pid_to_str (pid_to_ptid (pid))); |
| 1899 | else |
| 1900 | printf_filtered (_("Attaching to %s\n"), |
| 1901 | target_pid_to_str (pid_to_ptid (pid))); |
| 1902 | |
| 1903 | fflush (stdout); |
| 1904 | } |
| 1905 | inferior_ptid = do_attach (pid_to_ptid (pid)); |
| 1906 | if (!target_is_pushed (this)) |
| 1907 | push_target (this); |
| 1908 | } |
| 1909 | |
| 1910 | void |
| 1911 | procfs_target::detach (inferior *inf, int from_tty) |
| 1912 | { |
| 1913 | int pid = ptid_get_pid (inferior_ptid); |
| 1914 | |
| 1915 | if (from_tty) |
| 1916 | { |
| 1917 | const char *exec_file; |
| 1918 | |
| 1919 | exec_file = get_exec_file (0); |
| 1920 | if (exec_file == NULL) |
| 1921 | exec_file = ""; |
| 1922 | |
| 1923 | printf_filtered (_("Detaching from program: %s, %s\n"), exec_file, |
| 1924 | target_pid_to_str (pid_to_ptid (pid))); |
| 1925 | gdb_flush (gdb_stdout); |
| 1926 | } |
| 1927 | |
| 1928 | do_detach (); |
| 1929 | |
| 1930 | inferior_ptid = null_ptid; |
| 1931 | detach_inferior (pid); |
| 1932 | maybe_unpush_target (); |
| 1933 | } |
| 1934 | |
| 1935 | static ptid_t |
| 1936 | do_attach (ptid_t ptid) |
| 1937 | { |
| 1938 | procinfo *pi; |
| 1939 | struct inferior *inf; |
| 1940 | int fail; |
| 1941 | int lwpid; |
| 1942 | |
| 1943 | if ((pi = create_procinfo (ptid_get_pid (ptid), 0)) == NULL) |
| 1944 | perror (_("procfs: out of memory in 'attach'")); |
| 1945 | |
| 1946 | if (!open_procinfo_files (pi, FD_CTL)) |
| 1947 | { |
| 1948 | fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__); |
| 1949 | sprintf (errmsg, "do_attach: couldn't open /proc file for process %d", |
| 1950 | ptid_get_pid (ptid)); |
| 1951 | dead_procinfo (pi, errmsg, NOKILL); |
| 1952 | } |
| 1953 | |
| 1954 | /* Stop the process (if it isn't already stopped). */ |
| 1955 | if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) |
| 1956 | { |
| 1957 | pi->was_stopped = 1; |
| 1958 | proc_prettyprint_why (proc_why (pi), proc_what (pi), 1); |
| 1959 | } |
| 1960 | else |
| 1961 | { |
| 1962 | pi->was_stopped = 0; |
| 1963 | /* Set the process to run again when we close it. */ |
| 1964 | if (!proc_set_run_on_last_close (pi)) |
| 1965 | dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL); |
| 1966 | |
| 1967 | /* Now stop the process. */ |
| 1968 | if (!proc_stop_process (pi)) |
| 1969 | dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL); |
| 1970 | pi->ignore_next_sigstop = 1; |
| 1971 | } |
| 1972 | /* Save some of the /proc state to be restored if we detach. */ |
| 1973 | if (!proc_get_traced_faults (pi, &pi->saved_fltset)) |
| 1974 | dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL); |
| 1975 | if (!proc_get_traced_signals (pi, &pi->saved_sigset)) |
| 1976 | dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL); |
| 1977 | if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) |
| 1978 | dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.", |
| 1979 | NOKILL); |
| 1980 | if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) |
| 1981 | dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.", |
| 1982 | NOKILL); |
| 1983 | if (!proc_get_held_signals (pi, &pi->saved_sighold)) |
| 1984 | dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL); |
| 1985 | |
| 1986 | if ((fail = procfs_debug_inferior (pi)) != 0) |
| 1987 | dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL); |
| 1988 | |
| 1989 | inf = current_inferior (); |
| 1990 | inferior_appeared (inf, pi->pid); |
| 1991 | /* Let GDB know that the inferior was attached. */ |
| 1992 | inf->attach_flag = 1; |
| 1993 | |
| 1994 | /* Create a procinfo for the current lwp. */ |
| 1995 | lwpid = proc_get_current_thread (pi); |
| 1996 | create_procinfo (pi->pid, lwpid); |
| 1997 | |
| 1998 | /* Add it to gdb's thread list. */ |
| 1999 | ptid = ptid_build (pi->pid, lwpid, 0); |
| 2000 | add_thread (ptid); |
| 2001 | |
| 2002 | return ptid; |
| 2003 | } |
| 2004 | |
| 2005 | static void |
| 2006 | do_detach () |
| 2007 | { |
| 2008 | procinfo *pi; |
| 2009 | |
| 2010 | /* Find procinfo for the main process. */ |
| 2011 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), |
| 2012 | 0); /* FIXME: threads */ |
| 2013 | |
| 2014 | if (!proc_set_traced_signals (pi, &pi->saved_sigset)) |
| 2015 | proc_warn (pi, "do_detach, set_traced_signal", __LINE__); |
| 2016 | |
| 2017 | if (!proc_set_traced_faults (pi, &pi->saved_fltset)) |
| 2018 | proc_warn (pi, "do_detach, set_traced_faults", __LINE__); |
| 2019 | |
| 2020 | if (!proc_set_traced_sysentry (pi, pi->saved_entryset)) |
| 2021 | proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__); |
| 2022 | |
| 2023 | if (!proc_set_traced_sysexit (pi, pi->saved_exitset)) |
| 2024 | proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__); |
| 2025 | |
| 2026 | if (!proc_set_held_signals (pi, &pi->saved_sighold)) |
| 2027 | proc_warn (pi, "do_detach, set_held_signals", __LINE__); |
| 2028 | |
| 2029 | if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) |
| 2030 | if (!(pi->was_stopped) |
| 2031 | || query (_("Was stopped when attached, make it runnable again? "))) |
| 2032 | { |
| 2033 | /* Clear any pending signal. */ |
| 2034 | if (!proc_clear_current_fault (pi)) |
| 2035 | proc_warn (pi, "do_detach, clear_current_fault", __LINE__); |
| 2036 | |
| 2037 | if (!proc_clear_current_signal (pi)) |
| 2038 | proc_warn (pi, "do_detach, clear_current_signal", __LINE__); |
| 2039 | |
| 2040 | if (!proc_set_run_on_last_close (pi)) |
| 2041 | proc_warn (pi, "do_detach, set_rlc", __LINE__); |
| 2042 | } |
| 2043 | |
| 2044 | destroy_procinfo (pi); |
| 2045 | } |
| 2046 | |
| 2047 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
| 2048 | for all registers. |
| 2049 | |
| 2050 | ??? Is the following note still relevant? We can't get individual |
| 2051 | registers with the PT_GETREGS ptrace(2) request either, yet we |
| 2052 | don't bother with caching at all in that case. |
| 2053 | |
| 2054 | NOTE: Since the /proc interface cannot give us individual |
| 2055 | registers, we pay no attention to REGNUM, and just fetch them all. |
| 2056 | This results in the possibility that we will do unnecessarily many |
| 2057 | fetches, since we may be called repeatedly for individual |
| 2058 | registers. So we cache the results, and mark the cache invalid |
| 2059 | when the process is resumed. */ |
| 2060 | |
| 2061 | void |
| 2062 | procfs_target::fetch_registers (struct regcache *regcache, int regnum) |
| 2063 | { |
| 2064 | gdb_gregset_t *gregs; |
| 2065 | procinfo *pi; |
| 2066 | ptid_t ptid = regcache_get_ptid (regcache); |
| 2067 | int pid = ptid_get_pid (ptid); |
| 2068 | int tid = ptid_get_lwp (ptid); |
| 2069 | struct gdbarch *gdbarch = regcache->arch (); |
| 2070 | |
| 2071 | pi = find_procinfo_or_die (pid, tid); |
| 2072 | |
| 2073 | if (pi == NULL) |
| 2074 | error (_("procfs: fetch_registers failed to find procinfo for %s"), |
| 2075 | target_pid_to_str (ptid)); |
| 2076 | |
| 2077 | gregs = proc_get_gregs (pi); |
| 2078 | if (gregs == NULL) |
| 2079 | proc_error (pi, "fetch_registers, get_gregs", __LINE__); |
| 2080 | |
| 2081 | supply_gregset (regcache, (const gdb_gregset_t *) gregs); |
| 2082 | |
| 2083 | if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */ |
| 2084 | { |
| 2085 | gdb_fpregset_t *fpregs; |
| 2086 | |
| 2087 | if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch)) |
| 2088 | || regnum == gdbarch_pc_regnum (gdbarch) |
| 2089 | || regnum == gdbarch_sp_regnum (gdbarch)) |
| 2090 | return; /* Not a floating point register. */ |
| 2091 | |
| 2092 | fpregs = proc_get_fpregs (pi); |
| 2093 | if (fpregs == NULL) |
| 2094 | proc_error (pi, "fetch_registers, get_fpregs", __LINE__); |
| 2095 | |
| 2096 | supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs); |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
| 2101 | this for all registers. |
| 2102 | |
| 2103 | NOTE: Since the /proc interface will not read individual registers, |
| 2104 | we will cache these requests until the process is resumed, and only |
| 2105 | then write them back to the inferior process. |
| 2106 | |
| 2107 | FIXME: is that a really bad idea? Have to think about cases where |
| 2108 | writing one register might affect the value of others, etc. */ |
| 2109 | |
| 2110 | void |
| 2111 | procfs_target::store_registers (struct regcache *regcache, int regnum) |
| 2112 | { |
| 2113 | gdb_gregset_t *gregs; |
| 2114 | procinfo *pi; |
| 2115 | ptid_t ptid = regcache_get_ptid (regcache); |
| 2116 | int pid = ptid_get_pid (ptid); |
| 2117 | int tid = ptid_get_lwp (ptid); |
| 2118 | struct gdbarch *gdbarch = regcache->arch (); |
| 2119 | |
| 2120 | pi = find_procinfo_or_die (pid, tid); |
| 2121 | |
| 2122 | if (pi == NULL) |
| 2123 | error (_("procfs: store_registers: failed to find procinfo for %s"), |
| 2124 | target_pid_to_str (ptid)); |
| 2125 | |
| 2126 | gregs = proc_get_gregs (pi); |
| 2127 | if (gregs == NULL) |
| 2128 | proc_error (pi, "store_registers, get_gregs", __LINE__); |
| 2129 | |
| 2130 | fill_gregset (regcache, gregs, regnum); |
| 2131 | if (!proc_set_gregs (pi)) |
| 2132 | proc_error (pi, "store_registers, set_gregs", __LINE__); |
| 2133 | |
| 2134 | if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */ |
| 2135 | { |
| 2136 | gdb_fpregset_t *fpregs; |
| 2137 | |
| 2138 | if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch)) |
| 2139 | || regnum == gdbarch_pc_regnum (gdbarch) |
| 2140 | || regnum == gdbarch_sp_regnum (gdbarch)) |
| 2141 | return; /* Not a floating point register. */ |
| 2142 | |
| 2143 | fpregs = proc_get_fpregs (pi); |
| 2144 | if (fpregs == NULL) |
| 2145 | proc_error (pi, "store_registers, get_fpregs", __LINE__); |
| 2146 | |
| 2147 | fill_fpregset (regcache, fpregs, regnum); |
| 2148 | if (!proc_set_fpregs (pi)) |
| 2149 | proc_error (pi, "store_registers, set_fpregs", __LINE__); |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | static int |
| 2154 | syscall_is_lwp_exit (procinfo *pi, int scall) |
| 2155 | { |
| 2156 | if (scall == SYS_lwp_exit) |
| 2157 | return 1; |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | static int |
| 2162 | syscall_is_exit (procinfo *pi, int scall) |
| 2163 | { |
| 2164 | if (scall == SYS_exit) |
| 2165 | return 1; |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | static int |
| 2170 | syscall_is_exec (procinfo *pi, int scall) |
| 2171 | { |
| 2172 | #ifdef SYS_exec |
| 2173 | if (scall == SYS_exec) |
| 2174 | return 1; |
| 2175 | #endif |
| 2176 | if (scall == SYS_execve) |
| 2177 | return 1; |
| 2178 | return 0; |
| 2179 | } |
| 2180 | |
| 2181 | static int |
| 2182 | syscall_is_lwp_create (procinfo *pi, int scall) |
| 2183 | { |
| 2184 | if (scall == SYS_lwp_create) |
| 2185 | return 1; |
| 2186 | return 0; |
| 2187 | } |
| 2188 | |
| 2189 | /* Retrieve the next stop event from the child process. If child has |
| 2190 | not stopped yet, wait for it to stop. Translate /proc eventcodes |
| 2191 | (or possibly wait eventcodes) into gdb internal event codes. |
| 2192 | Returns the id of process (and possibly thread) that incurred the |
| 2193 | event. Event codes are returned through a pointer parameter. */ |
| 2194 | |
| 2195 | ptid_t |
| 2196 | procfs_target::wait (ptid_t ptid, struct target_waitstatus *status, |
| 2197 | int options) |
| 2198 | { |
| 2199 | /* First cut: loosely based on original version 2.1. */ |
| 2200 | procinfo *pi; |
| 2201 | int wstat; |
| 2202 | int temp_tid; |
| 2203 | ptid_t retval, temp_ptid; |
| 2204 | int why, what, flags; |
| 2205 | int retry = 0; |
| 2206 | |
| 2207 | wait_again: |
| 2208 | |
| 2209 | retry++; |
| 2210 | wstat = 0; |
| 2211 | retval = pid_to_ptid (-1); |
| 2212 | |
| 2213 | /* Find procinfo for main process. */ |
| 2214 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 2215 | if (pi) |
| 2216 | { |
| 2217 | /* We must assume that the status is stale now... */ |
| 2218 | pi->status_valid = 0; |
| 2219 | pi->gregs_valid = 0; |
| 2220 | pi->fpregs_valid = 0; |
| 2221 | |
| 2222 | #if 0 /* just try this out... */ |
| 2223 | flags = proc_flags (pi); |
| 2224 | why = proc_why (pi); |
| 2225 | if ((flags & PR_STOPPED) && (why == PR_REQUESTED)) |
| 2226 | pi->status_valid = 0; /* re-read again, IMMEDIATELY... */ |
| 2227 | #endif |
| 2228 | /* If child is not stopped, wait for it to stop. */ |
| 2229 | if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) && |
| 2230 | !proc_wait_for_stop (pi)) |
| 2231 | { |
| 2232 | /* wait_for_stop failed: has the child terminated? */ |
| 2233 | if (errno == ENOENT) |
| 2234 | { |
| 2235 | int wait_retval; |
| 2236 | |
| 2237 | /* /proc file not found; presumably child has terminated. */ |
| 2238 | wait_retval = ::wait (&wstat); /* "wait" for the child's exit. */ |
| 2239 | |
| 2240 | /* Wrong child? */ |
| 2241 | if (wait_retval != ptid_get_pid (inferior_ptid)) |
| 2242 | error (_("procfs: couldn't stop " |
| 2243 | "process %d: wait returned %d."), |
| 2244 | ptid_get_pid (inferior_ptid), wait_retval); |
| 2245 | /* FIXME: might I not just use waitpid? |
| 2246 | Or try find_procinfo to see if I know about this child? */ |
| 2247 | retval = pid_to_ptid (wait_retval); |
| 2248 | } |
| 2249 | else if (errno == EINTR) |
| 2250 | goto wait_again; |
| 2251 | else |
| 2252 | { |
| 2253 | /* Unknown error from wait_for_stop. */ |
| 2254 | proc_error (pi, "target_wait (wait_for_stop)", __LINE__); |
| 2255 | } |
| 2256 | } |
| 2257 | else |
| 2258 | { |
| 2259 | /* This long block is reached if either: |
| 2260 | a) the child was already stopped, or |
| 2261 | b) we successfully waited for the child with wait_for_stop. |
| 2262 | This block will analyze the /proc status, and translate it |
| 2263 | into a waitstatus for GDB. |
| 2264 | |
| 2265 | If we actually had to call wait because the /proc file |
| 2266 | is gone (child terminated), then we skip this block, |
| 2267 | because we already have a waitstatus. */ |
| 2268 | |
| 2269 | flags = proc_flags (pi); |
| 2270 | why = proc_why (pi); |
| 2271 | what = proc_what (pi); |
| 2272 | |
| 2273 | if (flags & (PR_STOPPED | PR_ISTOP)) |
| 2274 | { |
| 2275 | /* If it's running async (for single_thread control), |
| 2276 | set it back to normal again. */ |
| 2277 | if (flags & PR_ASYNC) |
| 2278 | if (!proc_unset_async (pi)) |
| 2279 | proc_error (pi, "target_wait, unset_async", __LINE__); |
| 2280 | |
| 2281 | if (info_verbose) |
| 2282 | proc_prettyprint_why (why, what, 1); |
| 2283 | |
| 2284 | /* The 'pid' we will return to GDB is composed of |
| 2285 | the process ID plus the lwp ID. */ |
| 2286 | retval = ptid_build (pi->pid, proc_get_current_thread (pi), 0); |
| 2287 | |
| 2288 | switch (why) { |
| 2289 | case PR_SIGNALLED: |
| 2290 | wstat = (what << 8) | 0177; |
| 2291 | break; |
| 2292 | case PR_SYSENTRY: |
| 2293 | if (syscall_is_lwp_exit (pi, what)) |
| 2294 | { |
| 2295 | if (print_thread_events) |
| 2296 | printf_unfiltered (_("[%s exited]\n"), |
| 2297 | target_pid_to_str (retval)); |
| 2298 | delete_thread (retval); |
| 2299 | status->kind = TARGET_WAITKIND_SPURIOUS; |
| 2300 | return retval; |
| 2301 | } |
| 2302 | else if (syscall_is_exit (pi, what)) |
| 2303 | { |
| 2304 | struct inferior *inf; |
| 2305 | |
| 2306 | /* Handle SYS_exit call only. */ |
| 2307 | /* Stopped at entry to SYS_exit. |
| 2308 | Make it runnable, resume it, then use |
| 2309 | the wait system call to get its exit code. |
| 2310 | Proc_run_process always clears the current |
| 2311 | fault and signal. |
| 2312 | Then return its exit status. */ |
| 2313 | pi->status_valid = 0; |
| 2314 | wstat = 0; |
| 2315 | /* FIXME: what we should do is return |
| 2316 | TARGET_WAITKIND_SPURIOUS. */ |
| 2317 | if (!proc_run_process (pi, 0, 0)) |
| 2318 | proc_error (pi, "target_wait, run_process", __LINE__); |
| 2319 | |
| 2320 | inf = find_inferior_pid (pi->pid); |
| 2321 | if (inf->attach_flag) |
| 2322 | { |
| 2323 | /* Don't call wait: simulate waiting for exit, |
| 2324 | return a "success" exit code. Bogus: what if |
| 2325 | it returns something else? */ |
| 2326 | wstat = 0; |
| 2327 | retval = inferior_ptid; /* ? ? ? */ |
| 2328 | } |
| 2329 | else |
| 2330 | { |
| 2331 | int temp = ::wait (&wstat); |
| 2332 | |
| 2333 | /* FIXME: shouldn't I make sure I get the right |
| 2334 | event from the right process? If (for |
| 2335 | instance) I have killed an earlier inferior |
| 2336 | process but failed to clean up after it |
| 2337 | somehow, I could get its termination event |
| 2338 | here. */ |
| 2339 | |
| 2340 | /* If wait returns -1, that's what we return |
| 2341 | to GDB. */ |
| 2342 | if (temp < 0) |
| 2343 | retval = pid_to_ptid (temp); |
| 2344 | } |
| 2345 | } |
| 2346 | else |
| 2347 | { |
| 2348 | printf_filtered (_("procfs: trapped on entry to ")); |
| 2349 | proc_prettyprint_syscall (proc_what (pi), 0); |
| 2350 | printf_filtered ("\n"); |
| 2351 | |
| 2352 | long i, nsysargs, *sysargs; |
| 2353 | |
| 2354 | if ((nsysargs = proc_nsysarg (pi)) > 0 && |
| 2355 | (sysargs = proc_sysargs (pi)) != NULL) |
| 2356 | { |
| 2357 | printf_filtered (_("%ld syscall arguments:\n"), |
| 2358 | nsysargs); |
| 2359 | for (i = 0; i < nsysargs; i++) |
| 2360 | printf_filtered ("#%ld: 0x%08lx\n", |
| 2361 | i, sysargs[i]); |
| 2362 | } |
| 2363 | |
| 2364 | if (status) |
| 2365 | { |
| 2366 | /* How to exit gracefully, returning "unknown |
| 2367 | event". */ |
| 2368 | status->kind = TARGET_WAITKIND_SPURIOUS; |
| 2369 | return inferior_ptid; |
| 2370 | } |
| 2371 | else |
| 2372 | { |
| 2373 | /* How to keep going without returning to wfi: */ |
| 2374 | target_continue_no_signal (ptid); |
| 2375 | goto wait_again; |
| 2376 | } |
| 2377 | } |
| 2378 | break; |
| 2379 | case PR_SYSEXIT: |
| 2380 | if (syscall_is_exec (pi, what)) |
| 2381 | { |
| 2382 | /* Hopefully this is our own "fork-child" execing |
| 2383 | the real child. Hoax this event into a trap, and |
| 2384 | GDB will see the child about to execute its start |
| 2385 | address. */ |
| 2386 | wstat = (SIGTRAP << 8) | 0177; |
| 2387 | } |
| 2388 | else if (syscall_is_lwp_create (pi, what)) |
| 2389 | { |
| 2390 | /* This syscall is somewhat like fork/exec. We |
| 2391 | will get the event twice: once for the parent |
| 2392 | LWP, and once for the child. We should already |
| 2393 | know about the parent LWP, but the child will |
| 2394 | be new to us. So, whenever we get this event, |
| 2395 | if it represents a new thread, simply add the |
| 2396 | thread to the list. */ |
| 2397 | |
| 2398 | /* If not in procinfo list, add it. */ |
| 2399 | temp_tid = proc_get_current_thread (pi); |
| 2400 | if (!find_procinfo (pi->pid, temp_tid)) |
| 2401 | create_procinfo (pi->pid, temp_tid); |
| 2402 | |
| 2403 | temp_ptid = ptid_build (pi->pid, temp_tid, 0); |
| 2404 | /* If not in GDB's thread list, add it. */ |
| 2405 | if (!in_thread_list (temp_ptid)) |
| 2406 | add_thread (temp_ptid); |
| 2407 | |
| 2408 | /* Return to WFI, but tell it to immediately resume. */ |
| 2409 | status->kind = TARGET_WAITKIND_SPURIOUS; |
| 2410 | return inferior_ptid; |
| 2411 | } |
| 2412 | else if (syscall_is_lwp_exit (pi, what)) |
| 2413 | { |
| 2414 | if (print_thread_events) |
| 2415 | printf_unfiltered (_("[%s exited]\n"), |
| 2416 | target_pid_to_str (retval)); |
| 2417 | delete_thread (retval); |
| 2418 | status->kind = TARGET_WAITKIND_SPURIOUS; |
| 2419 | return retval; |
| 2420 | } |
| 2421 | else if (0) |
| 2422 | { |
| 2423 | /* FIXME: Do we need to handle SYS_sproc, |
| 2424 | SYS_fork, or SYS_vfork here? The old procfs |
| 2425 | seemed to use this event to handle threads on |
| 2426 | older (non-LWP) systems, where I'm assuming |
| 2427 | that threads were actually separate processes. |
| 2428 | Irix, maybe? Anyway, low priority for now. */ |
| 2429 | } |
| 2430 | else |
| 2431 | { |
| 2432 | printf_filtered (_("procfs: trapped on exit from ")); |
| 2433 | proc_prettyprint_syscall (proc_what (pi), 0); |
| 2434 | printf_filtered ("\n"); |
| 2435 | |
| 2436 | long i, nsysargs, *sysargs; |
| 2437 | |
| 2438 | if ((nsysargs = proc_nsysarg (pi)) > 0 && |
| 2439 | (sysargs = proc_sysargs (pi)) != NULL) |
| 2440 | { |
| 2441 | printf_filtered (_("%ld syscall arguments:\n"), |
| 2442 | nsysargs); |
| 2443 | for (i = 0; i < nsysargs; i++) |
| 2444 | printf_filtered ("#%ld: 0x%08lx\n", |
| 2445 | i, sysargs[i]); |
| 2446 | } |
| 2447 | |
| 2448 | status->kind = TARGET_WAITKIND_SPURIOUS; |
| 2449 | return inferior_ptid; |
| 2450 | } |
| 2451 | break; |
| 2452 | case PR_REQUESTED: |
| 2453 | #if 0 /* FIXME */ |
| 2454 | wstat = (SIGSTOP << 8) | 0177; |
| 2455 | break; |
| 2456 | #else |
| 2457 | if (retry < 5) |
| 2458 | { |
| 2459 | printf_filtered (_("Retry #%d:\n"), retry); |
| 2460 | pi->status_valid = 0; |
| 2461 | goto wait_again; |
| 2462 | } |
| 2463 | else |
| 2464 | { |
| 2465 | /* If not in procinfo list, add it. */ |
| 2466 | temp_tid = proc_get_current_thread (pi); |
| 2467 | if (!find_procinfo (pi->pid, temp_tid)) |
| 2468 | create_procinfo (pi->pid, temp_tid); |
| 2469 | |
| 2470 | /* If not in GDB's thread list, add it. */ |
| 2471 | temp_ptid = ptid_build (pi->pid, temp_tid, 0); |
| 2472 | if (!in_thread_list (temp_ptid)) |
| 2473 | add_thread (temp_ptid); |
| 2474 | |
| 2475 | status->kind = TARGET_WAITKIND_STOPPED; |
| 2476 | status->value.sig = GDB_SIGNAL_0; |
| 2477 | return retval; |
| 2478 | } |
| 2479 | #endif |
| 2480 | case PR_JOBCONTROL: |
| 2481 | wstat = (what << 8) | 0177; |
| 2482 | break; |
| 2483 | case PR_FAULTED: |
| 2484 | switch (what) { |
| 2485 | case FLTWATCH: |
| 2486 | wstat = (SIGTRAP << 8) | 0177; |
| 2487 | break; |
| 2488 | /* FIXME: use si_signo where possible. */ |
| 2489 | case FLTPRIV: |
| 2490 | case FLTILL: |
| 2491 | wstat = (SIGILL << 8) | 0177; |
| 2492 | break; |
| 2493 | case FLTBPT: |
| 2494 | case FLTTRACE: |
| 2495 | wstat = (SIGTRAP << 8) | 0177; |
| 2496 | break; |
| 2497 | case FLTSTACK: |
| 2498 | case FLTACCESS: |
| 2499 | case FLTBOUNDS: |
| 2500 | wstat = (SIGSEGV << 8) | 0177; |
| 2501 | break; |
| 2502 | case FLTIOVF: |
| 2503 | case FLTIZDIV: |
| 2504 | case FLTFPE: |
| 2505 | wstat = (SIGFPE << 8) | 0177; |
| 2506 | break; |
| 2507 | case FLTPAGE: /* Recoverable page fault */ |
| 2508 | default: /* FIXME: use si_signo if possible for |
| 2509 | fault. */ |
| 2510 | retval = pid_to_ptid (-1); |
| 2511 | printf_filtered ("procfs:%d -- ", __LINE__); |
| 2512 | printf_filtered (_("child stopped for unknown reason:\n")); |
| 2513 | proc_prettyprint_why (why, what, 1); |
| 2514 | error (_("... giving up...")); |
| 2515 | break; |
| 2516 | } |
| 2517 | break; /* case PR_FAULTED: */ |
| 2518 | default: /* switch (why) unmatched */ |
| 2519 | printf_filtered ("procfs:%d -- ", __LINE__); |
| 2520 | printf_filtered (_("child stopped for unknown reason:\n")); |
| 2521 | proc_prettyprint_why (why, what, 1); |
| 2522 | error (_("... giving up...")); |
| 2523 | break; |
| 2524 | } |
| 2525 | /* Got this far without error: If retval isn't in the |
| 2526 | threads database, add it. */ |
| 2527 | if (ptid_get_pid (retval) > 0 && |
| 2528 | !ptid_equal (retval, inferior_ptid) && |
| 2529 | !in_thread_list (retval)) |
| 2530 | { |
| 2531 | /* We have a new thread. We need to add it both to |
| 2532 | GDB's list and to our own. If we don't create a |
| 2533 | procinfo, resume may be unhappy later. */ |
| 2534 | add_thread (retval); |
| 2535 | if (find_procinfo (ptid_get_pid (retval), |
| 2536 | ptid_get_lwp (retval)) == NULL) |
| 2537 | create_procinfo (ptid_get_pid (retval), |
| 2538 | ptid_get_lwp (retval)); |
| 2539 | } |
| 2540 | } |
| 2541 | else /* Flags do not indicate STOPPED. */ |
| 2542 | { |
| 2543 | /* surely this can't happen... */ |
| 2544 | printf_filtered ("procfs:%d -- process not stopped.\n", |
| 2545 | __LINE__); |
| 2546 | proc_prettyprint_flags (flags, 1); |
| 2547 | error (_("procfs: ...giving up...")); |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | if (status) |
| 2552 | store_waitstatus (status, wstat); |
| 2553 | } |
| 2554 | |
| 2555 | return retval; |
| 2556 | } |
| 2557 | |
| 2558 | /* Perform a partial transfer to/from the specified object. For |
| 2559 | memory transfers, fall back to the old memory xfer functions. */ |
| 2560 | |
| 2561 | enum target_xfer_status |
| 2562 | procfs_target::xfer_partial (enum target_object object, |
| 2563 | const char *annex, gdb_byte *readbuf, |
| 2564 | const gdb_byte *writebuf, ULONGEST offset, |
| 2565 | ULONGEST len, ULONGEST *xfered_len) |
| 2566 | { |
| 2567 | switch (object) |
| 2568 | { |
| 2569 | case TARGET_OBJECT_MEMORY: |
| 2570 | return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len); |
| 2571 | |
| 2572 | case TARGET_OBJECT_AUXV: |
| 2573 | return memory_xfer_auxv (this, object, annex, readbuf, writebuf, |
| 2574 | offset, len, xfered_len); |
| 2575 | |
| 2576 | default: |
| 2577 | return this->beneath->xfer_partial (object, annex, |
| 2578 | readbuf, writebuf, offset, len, |
| 2579 | xfered_len); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | /* Helper for procfs_xfer_partial that handles memory transfers. |
| 2584 | Arguments are like target_xfer_partial. */ |
| 2585 | |
| 2586 | static enum target_xfer_status |
| 2587 | procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf, |
| 2588 | ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len) |
| 2589 | { |
| 2590 | procinfo *pi; |
| 2591 | int nbytes; |
| 2592 | |
| 2593 | /* Find procinfo for main process. */ |
| 2594 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 2595 | if (pi->as_fd == 0 && |
| 2596 | open_procinfo_files (pi, FD_AS) == 0) |
| 2597 | { |
| 2598 | proc_warn (pi, "xfer_memory, open_proc_files", __LINE__); |
| 2599 | return TARGET_XFER_E_IO; |
| 2600 | } |
| 2601 | |
| 2602 | if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr) |
| 2603 | return TARGET_XFER_E_IO; |
| 2604 | |
| 2605 | if (writebuf != NULL) |
| 2606 | { |
| 2607 | PROCFS_NOTE ("write memory:\n"); |
| 2608 | nbytes = write (pi->as_fd, writebuf, len); |
| 2609 | } |
| 2610 | else |
| 2611 | { |
| 2612 | PROCFS_NOTE ("read memory:\n"); |
| 2613 | nbytes = read (pi->as_fd, readbuf, len); |
| 2614 | } |
| 2615 | if (nbytes <= 0) |
| 2616 | return TARGET_XFER_E_IO; |
| 2617 | *xfered_len = nbytes; |
| 2618 | return TARGET_XFER_OK; |
| 2619 | } |
| 2620 | |
| 2621 | /* Called by target_resume before making child runnable. Mark cached |
| 2622 | registers and status's invalid. If there are "dirty" caches that |
| 2623 | need to be written back to the child process, do that. |
| 2624 | |
| 2625 | File descriptors are also cached. As they are a limited resource, |
| 2626 | we cannot hold onto them indefinitely. However, as they are |
| 2627 | expensive to open, we don't want to throw them away |
| 2628 | indescriminately either. As a compromise, we will keep the file |
| 2629 | descriptors for the parent process, but discard any file |
| 2630 | descriptors we may have accumulated for the threads. |
| 2631 | |
| 2632 | As this function is called by iterate_over_threads, it always |
| 2633 | returns zero (so that iterate_over_threads will keep |
| 2634 | iterating). */ |
| 2635 | |
| 2636 | static int |
| 2637 | invalidate_cache (procinfo *parent, procinfo *pi, void *ptr) |
| 2638 | { |
| 2639 | /* About to run the child; invalidate caches and do any other |
| 2640 | cleanup. */ |
| 2641 | |
| 2642 | #if 0 |
| 2643 | if (pi->gregs_dirty) |
| 2644 | if (parent == NULL || |
| 2645 | proc_get_current_thread (parent) != pi->tid) |
| 2646 | if (!proc_set_gregs (pi)) /* flush gregs cache */ |
| 2647 | proc_warn (pi, "target_resume, set_gregs", |
| 2648 | __LINE__); |
| 2649 | if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0) |
| 2650 | if (pi->fpregs_dirty) |
| 2651 | if (parent == NULL || |
| 2652 | proc_get_current_thread (parent) != pi->tid) |
| 2653 | if (!proc_set_fpregs (pi)) /* flush fpregs cache */ |
| 2654 | proc_warn (pi, "target_resume, set_fpregs", |
| 2655 | __LINE__); |
| 2656 | #endif |
| 2657 | |
| 2658 | if (parent != NULL) |
| 2659 | { |
| 2660 | /* The presence of a parent indicates that this is an LWP. |
| 2661 | Close any file descriptors that it might have open. |
| 2662 | We don't do this to the master (parent) procinfo. */ |
| 2663 | |
| 2664 | close_procinfo_files (pi); |
| 2665 | } |
| 2666 | pi->gregs_valid = 0; |
| 2667 | pi->fpregs_valid = 0; |
| 2668 | #if 0 |
| 2669 | pi->gregs_dirty = 0; |
| 2670 | pi->fpregs_dirty = 0; |
| 2671 | #endif |
| 2672 | pi->status_valid = 0; |
| 2673 | pi->threads_valid = 0; |
| 2674 | |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | #if 0 |
| 2679 | /* A callback function for iterate_over_threads. Find the |
| 2680 | asynchronous signal thread, and make it runnable. See if that |
| 2681 | helps matters any. */ |
| 2682 | |
| 2683 | static int |
| 2684 | make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr) |
| 2685 | { |
| 2686 | #ifdef PR_ASLWP |
| 2687 | if (proc_flags (pi) & PR_ASLWP) |
| 2688 | { |
| 2689 | if (!proc_run_process (pi, 0, -1)) |
| 2690 | proc_error (pi, "make_signal_thread_runnable", __LINE__); |
| 2691 | return 1; |
| 2692 | } |
| 2693 | #endif |
| 2694 | return 0; |
| 2695 | } |
| 2696 | #endif |
| 2697 | |
| 2698 | /* Make the child process runnable. Normally we will then call |
| 2699 | procfs_wait and wait for it to stop again (unless gdb is async). |
| 2700 | |
| 2701 | If STEP is true, then arrange for the child to stop again after |
| 2702 | executing a single instruction. If SIGNO is zero, then cancel any |
| 2703 | pending signal; if non-zero, then arrange for the indicated signal |
| 2704 | to be delivered to the child when it runs. If PID is -1, then |
| 2705 | allow any child thread to run; if non-zero, then allow only the |
| 2706 | indicated thread to run. (not implemented yet). */ |
| 2707 | |
| 2708 | void |
| 2709 | procfs_target::resume (ptid_t ptid, int step, enum gdb_signal signo) |
| 2710 | { |
| 2711 | procinfo *pi, *thread; |
| 2712 | int native_signo; |
| 2713 | |
| 2714 | /* 2.1: |
| 2715 | prrun.prflags |= PRSVADDR; |
| 2716 | prrun.pr_vaddr = $PC; set resume address |
| 2717 | prrun.prflags |= PRSTRACE; trace signals in pr_trace (all) |
| 2718 | prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE) |
| 2719 | prrun.prflags |= PRCFAULT; clear current fault. |
| 2720 | |
| 2721 | PRSTRACE and PRSFAULT can be done by other means |
| 2722 | (proc_trace_signals, proc_trace_faults) |
| 2723 | PRSVADDR is unnecessary. |
| 2724 | PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault) |
| 2725 | This basically leaves PRSTEP and PRCSIG. |
| 2726 | PRCSIG is like PIOCSSIG (proc_clear_current_signal). |
| 2727 | So basically PR_STEP is the sole argument that must be passed |
| 2728 | to proc_run_process (for use in the prrun struct by ioctl). */ |
| 2729 | |
| 2730 | /* Find procinfo for main process. */ |
| 2731 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 2732 | |
| 2733 | /* First cut: ignore pid argument. */ |
| 2734 | errno = 0; |
| 2735 | |
| 2736 | /* Convert signal to host numbering. */ |
| 2737 | if (signo == 0 || |
| 2738 | (signo == GDB_SIGNAL_STOP && pi->ignore_next_sigstop)) |
| 2739 | native_signo = 0; |
| 2740 | else |
| 2741 | native_signo = gdb_signal_to_host (signo); |
| 2742 | |
| 2743 | pi->ignore_next_sigstop = 0; |
| 2744 | |
| 2745 | /* Running the process voids all cached registers and status. */ |
| 2746 | /* Void the threads' caches first. */ |
| 2747 | proc_iterate_over_threads (pi, invalidate_cache, NULL); |
| 2748 | /* Void the process procinfo's caches. */ |
| 2749 | invalidate_cache (NULL, pi, NULL); |
| 2750 | |
| 2751 | if (ptid_get_pid (ptid) != -1) |
| 2752 | { |
| 2753 | /* Resume a specific thread, presumably suppressing the |
| 2754 | others. */ |
| 2755 | thread = find_procinfo (ptid_get_pid (ptid), ptid_get_lwp (ptid)); |
| 2756 | if (thread != NULL) |
| 2757 | { |
| 2758 | if (thread->tid != 0) |
| 2759 | { |
| 2760 | /* We're to resume a specific thread, and not the |
| 2761 | others. Set the child process's PR_ASYNC flag. */ |
| 2762 | if (!proc_set_async (pi)) |
| 2763 | proc_error (pi, "target_resume, set_async", __LINE__); |
| 2764 | #if 0 |
| 2765 | proc_iterate_over_threads (pi, |
| 2766 | make_signal_thread_runnable, |
| 2767 | NULL); |
| 2768 | #endif |
| 2769 | pi = thread; /* Substitute the thread's procinfo |
| 2770 | for run. */ |
| 2771 | } |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | if (!proc_run_process (pi, step, native_signo)) |
| 2776 | { |
| 2777 | if (errno == EBUSY) |
| 2778 | warning (_("resume: target already running. " |
| 2779 | "Pretend to resume, and hope for the best!")); |
| 2780 | else |
| 2781 | proc_error (pi, "target_resume", __LINE__); |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | /* Set up to trace signals in the child process. */ |
| 2786 | |
| 2787 | void |
| 2788 | procfs_target::pass_signals (int numsigs, unsigned char *pass_signals) |
| 2789 | { |
| 2790 | sigset_t signals; |
| 2791 | procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 2792 | int signo; |
| 2793 | |
| 2794 | prfillset (&signals); |
| 2795 | |
| 2796 | for (signo = 0; signo < NSIG; signo++) |
| 2797 | { |
| 2798 | int target_signo = gdb_signal_from_host (signo); |
| 2799 | if (target_signo < numsigs && pass_signals[target_signo]) |
| 2800 | prdelset (&signals, signo); |
| 2801 | } |
| 2802 | |
| 2803 | if (!proc_set_traced_signals (pi, &signals)) |
| 2804 | proc_error (pi, "pass_signals", __LINE__); |
| 2805 | } |
| 2806 | |
| 2807 | /* Print status information about the child process. */ |
| 2808 | |
| 2809 | void |
| 2810 | procfs_target::files_info () |
| 2811 | { |
| 2812 | struct inferior *inf = current_inferior (); |
| 2813 | |
| 2814 | printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"), |
| 2815 | inf->attach_flag? "attached": "child", |
| 2816 | target_pid_to_str (inferior_ptid)); |
| 2817 | } |
| 2818 | |
| 2819 | /* Make it die. Wait for it to die. Clean up after it. Note: this |
| 2820 | should only be applied to the real process, not to an LWP, because |
| 2821 | of the check for parent-process. If we need this to work for an |
| 2822 | LWP, it needs some more logic. */ |
| 2823 | |
| 2824 | static void |
| 2825 | unconditionally_kill_inferior (procinfo *pi) |
| 2826 | { |
| 2827 | int parent_pid; |
| 2828 | |
| 2829 | parent_pid = proc_parent_pid (pi); |
| 2830 | if (!proc_kill (pi, SIGKILL)) |
| 2831 | proc_error (pi, "unconditionally_kill, proc_kill", __LINE__); |
| 2832 | destroy_procinfo (pi); |
| 2833 | |
| 2834 | /* If pi is GDB's child, wait for it to die. */ |
| 2835 | if (parent_pid == getpid ()) |
| 2836 | /* FIXME: should we use waitpid to make sure we get the right event? |
| 2837 | Should we check the returned event? */ |
| 2838 | { |
| 2839 | #if 0 |
| 2840 | int status, ret; |
| 2841 | |
| 2842 | ret = waitpid (pi->pid, &status, 0); |
| 2843 | #else |
| 2844 | wait (NULL); |
| 2845 | #endif |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | /* We're done debugging it, and we want it to go away. Then we want |
| 2850 | GDB to forget all about it. */ |
| 2851 | |
| 2852 | void |
| 2853 | procfs_target::kill () |
| 2854 | { |
| 2855 | if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */ |
| 2856 | { |
| 2857 | /* Find procinfo for main process. */ |
| 2858 | procinfo *pi = find_procinfo (ptid_get_pid (inferior_ptid), 0); |
| 2859 | |
| 2860 | if (pi) |
| 2861 | unconditionally_kill_inferior (pi); |
| 2862 | target_mourn_inferior (inferior_ptid); |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | /* Forget we ever debugged this thing! */ |
| 2867 | |
| 2868 | void |
| 2869 | procfs_target::mourn_inferior () |
| 2870 | { |
| 2871 | procinfo *pi; |
| 2872 | |
| 2873 | if (!ptid_equal (inferior_ptid, null_ptid)) |
| 2874 | { |
| 2875 | /* Find procinfo for main process. */ |
| 2876 | pi = find_procinfo (ptid_get_pid (inferior_ptid), 0); |
| 2877 | if (pi) |
| 2878 | destroy_procinfo (pi); |
| 2879 | } |
| 2880 | |
| 2881 | generic_mourn_inferior (); |
| 2882 | |
| 2883 | maybe_unpush_target (); |
| 2884 | } |
| 2885 | |
| 2886 | /* When GDB forks to create a runnable inferior process, this function |
| 2887 | is called on the parent side of the fork. It's job is to do |
| 2888 | whatever is necessary to make the child ready to be debugged, and |
| 2889 | then wait for the child to synchronize. */ |
| 2890 | |
| 2891 | static void |
| 2892 | procfs_init_inferior (struct target_ops *ops, int pid) |
| 2893 | { |
| 2894 | procinfo *pi; |
| 2895 | sigset_t signals; |
| 2896 | int fail; |
| 2897 | int lwpid; |
| 2898 | |
| 2899 | /* This routine called on the parent side (GDB side) |
| 2900 | after GDB forks the inferior. */ |
| 2901 | if (!target_is_pushed (ops)) |
| 2902 | push_target (ops); |
| 2903 | |
| 2904 | if ((pi = create_procinfo (pid, 0)) == NULL) |
| 2905 | perror (_("procfs: out of memory in 'init_inferior'")); |
| 2906 | |
| 2907 | if (!open_procinfo_files (pi, FD_CTL)) |
| 2908 | proc_error (pi, "init_inferior, open_proc_files", __LINE__); |
| 2909 | |
| 2910 | /* |
| 2911 | xmalloc // done |
| 2912 | open_procinfo_files // done |
| 2913 | link list // done |
| 2914 | prfillset (trace) |
| 2915 | procfs_notice_signals |
| 2916 | prfillset (fault) |
| 2917 | prdelset (FLTPAGE) |
| 2918 | PIOCWSTOP |
| 2919 | PIOCSFAULT |
| 2920 | */ |
| 2921 | |
| 2922 | /* If not stopped yet, wait for it to stop. */ |
| 2923 | if (!(proc_flags (pi) & PR_STOPPED) && |
| 2924 | !(proc_wait_for_stop (pi))) |
| 2925 | dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL); |
| 2926 | |
| 2927 | /* Save some of the /proc state to be restored if we detach. */ |
| 2928 | /* FIXME: Why? In case another debugger was debugging it? |
| 2929 | We're it's parent, for Ghu's sake! */ |
| 2930 | if (!proc_get_traced_signals (pi, &pi->saved_sigset)) |
| 2931 | proc_error (pi, "init_inferior, get_traced_signals", __LINE__); |
| 2932 | if (!proc_get_held_signals (pi, &pi->saved_sighold)) |
| 2933 | proc_error (pi, "init_inferior, get_held_signals", __LINE__); |
| 2934 | if (!proc_get_traced_faults (pi, &pi->saved_fltset)) |
| 2935 | proc_error (pi, "init_inferior, get_traced_faults", __LINE__); |
| 2936 | if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) |
| 2937 | proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__); |
| 2938 | if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) |
| 2939 | proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__); |
| 2940 | |
| 2941 | if ((fail = procfs_debug_inferior (pi)) != 0) |
| 2942 | proc_error (pi, "init_inferior (procfs_debug_inferior)", fail); |
| 2943 | |
| 2944 | /* FIXME: logically, we should really be turning OFF run-on-last-close, |
| 2945 | and possibly even turning ON kill-on-last-close at this point. But |
| 2946 | I can't make that change without careful testing which I don't have |
| 2947 | time to do right now... */ |
| 2948 | /* Turn on run-on-last-close flag so that the child |
| 2949 | will die if GDB goes away for some reason. */ |
| 2950 | if (!proc_set_run_on_last_close (pi)) |
| 2951 | proc_error (pi, "init_inferior, set_RLC", __LINE__); |
| 2952 | |
| 2953 | /* We now have have access to the lwpid of the main thread/lwp. */ |
| 2954 | lwpid = proc_get_current_thread (pi); |
| 2955 | |
| 2956 | /* Create a procinfo for the main lwp. */ |
| 2957 | create_procinfo (pid, lwpid); |
| 2958 | |
| 2959 | /* We already have a main thread registered in the thread table at |
| 2960 | this point, but it didn't have any lwp info yet. Notify the core |
| 2961 | about it. This changes inferior_ptid as well. */ |
| 2962 | thread_change_ptid (pid_to_ptid (pid), |
| 2963 | ptid_build (pid, lwpid, 0)); |
| 2964 | |
| 2965 | gdb_startup_inferior (pid, START_INFERIOR_TRAPS_EXPECTED); |
| 2966 | } |
| 2967 | |
| 2968 | /* When GDB forks to create a new process, this function is called on |
| 2969 | the child side of the fork before GDB exec's the user program. Its |
| 2970 | job is to make the child minimally debuggable, so that the parent |
| 2971 | GDB process can connect to the child and take over. This function |
| 2972 | should do only the minimum to make that possible, and to |
| 2973 | synchronize with the parent process. The parent process should |
| 2974 | take care of the details. */ |
| 2975 | |
| 2976 | static void |
| 2977 | procfs_set_exec_trap (void) |
| 2978 | { |
| 2979 | /* This routine called on the child side (inferior side) |
| 2980 | after GDB forks the inferior. It must use only local variables, |
| 2981 | because it may be sharing data space with its parent. */ |
| 2982 | |
| 2983 | procinfo *pi; |
| 2984 | sysset_t *exitset; |
| 2985 | |
| 2986 | if ((pi = create_procinfo (getpid (), 0)) == NULL) |
| 2987 | perror_with_name (_("procfs: create_procinfo failed in child.")); |
| 2988 | |
| 2989 | if (open_procinfo_files (pi, FD_CTL) == 0) |
| 2990 | { |
| 2991 | proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__); |
| 2992 | gdb_flush (gdb_stderr); |
| 2993 | /* No need to call "dead_procinfo", because we're going to |
| 2994 | exit. */ |
| 2995 | _exit (127); |
| 2996 | } |
| 2997 | |
| 2998 | /* Method for tracing exec syscalls. */ |
| 2999 | /* GW: Rationale... |
| 3000 | Not all systems with /proc have all the exec* syscalls with the same |
| 3001 | names. On the SGI, for example, there is no SYS_exec, but there |
| 3002 | *is* a SYS_execv. So, we try to account for that. */ |
| 3003 | |
| 3004 | exitset = sysset_t_alloc (pi); |
| 3005 | premptyset (exitset); |
| 3006 | #ifdef SYS_exec |
| 3007 | praddset (exitset, SYS_exec); |
| 3008 | #endif |
| 3009 | praddset (exitset, SYS_execve); |
| 3010 | |
| 3011 | if (!proc_set_traced_sysexit (pi, exitset)) |
| 3012 | { |
| 3013 | proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__); |
| 3014 | gdb_flush (gdb_stderr); |
| 3015 | _exit (127); |
| 3016 | } |
| 3017 | |
| 3018 | /* FIXME: should this be done in the parent instead? */ |
| 3019 | /* Turn off inherit on fork flag so that all grand-children |
| 3020 | of gdb start with tracing flags cleared. */ |
| 3021 | if (!proc_unset_inherit_on_fork (pi)) |
| 3022 | proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__); |
| 3023 | |
| 3024 | /* Turn off run on last close flag, so that the child process |
| 3025 | cannot run away just because we close our handle on it. |
| 3026 | We want it to wait for the parent to attach. */ |
| 3027 | if (!proc_unset_run_on_last_close (pi)) |
| 3028 | proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__); |
| 3029 | |
| 3030 | /* FIXME: No need to destroy the procinfo -- |
| 3031 | we have our own address space, and we're about to do an exec! */ |
| 3032 | /*destroy_procinfo (pi);*/ |
| 3033 | } |
| 3034 | |
| 3035 | /* This function is called BEFORE gdb forks the inferior process. Its |
| 3036 | only real responsibility is to set things up for the fork, and tell |
| 3037 | GDB which two functions to call after the fork (one for the parent, |
| 3038 | and one for the child). |
| 3039 | |
| 3040 | This function does a complicated search for a unix shell program, |
| 3041 | which it then uses to parse arguments and environment variables to |
| 3042 | be sent to the child. I wonder whether this code could not be |
| 3043 | abstracted out and shared with other unix targets such as |
| 3044 | inf-ptrace? */ |
| 3045 | |
| 3046 | void |
| 3047 | procfs_target::create_inferior (const char *exec_file, |
| 3048 | const std::string &allargs, |
| 3049 | char **env, int from_tty) |
| 3050 | { |
| 3051 | char *shell_file = getenv ("SHELL"); |
| 3052 | char *tryname; |
| 3053 | int pid; |
| 3054 | |
| 3055 | if (shell_file != NULL && strchr (shell_file, '/') == NULL) |
| 3056 | { |
| 3057 | |
| 3058 | /* We will be looking down the PATH to find shell_file. If we |
| 3059 | just do this the normal way (via execlp, which operates by |
| 3060 | attempting an exec for each element of the PATH until it |
| 3061 | finds one which succeeds), then there will be an exec for |
| 3062 | each failed attempt, each of which will cause a PR_SYSEXIT |
| 3063 | stop, and we won't know how to distinguish the PR_SYSEXIT's |
| 3064 | for these failed execs with the ones for successful execs |
| 3065 | (whether the exec has succeeded is stored at that time in the |
| 3066 | carry bit or some such architecture-specific and |
| 3067 | non-ABI-specified place). |
| 3068 | |
| 3069 | So I can't think of anything better than to search the PATH |
| 3070 | now. This has several disadvantages: (1) There is a race |
| 3071 | condition; if we find a file now and it is deleted before we |
| 3072 | exec it, we lose, even if the deletion leaves a valid file |
| 3073 | further down in the PATH, (2) there is no way to know exactly |
| 3074 | what an executable (in the sense of "capable of being |
| 3075 | exec'd") file is. Using access() loses because it may lose |
| 3076 | if the caller is the superuser; failing to use it loses if |
| 3077 | there are ACLs or some such. */ |
| 3078 | |
| 3079 | const char *p; |
| 3080 | const char *p1; |
| 3081 | /* FIXME-maybe: might want "set path" command so user can change what |
| 3082 | path is used from within GDB. */ |
| 3083 | const char *path = getenv ("PATH"); |
| 3084 | int len; |
| 3085 | struct stat statbuf; |
| 3086 | |
| 3087 | if (path == NULL) |
| 3088 | path = "/bin:/usr/bin"; |
| 3089 | |
| 3090 | tryname = (char *) alloca (strlen (path) + strlen (shell_file) + 2); |
| 3091 | for (p = path; p != NULL; p = p1 ? p1 + 1: NULL) |
| 3092 | { |
| 3093 | p1 = strchr (p, ':'); |
| 3094 | if (p1 != NULL) |
| 3095 | len = p1 - p; |
| 3096 | else |
| 3097 | len = strlen (p); |
| 3098 | strncpy (tryname, p, len); |
| 3099 | tryname[len] = '\0'; |
| 3100 | strcat (tryname, "/"); |
| 3101 | strcat (tryname, shell_file); |
| 3102 | if (access (tryname, X_OK) < 0) |
| 3103 | continue; |
| 3104 | if (stat (tryname, &statbuf) < 0) |
| 3105 | continue; |
| 3106 | if (!S_ISREG (statbuf.st_mode)) |
| 3107 | /* We certainly need to reject directories. I'm not quite |
| 3108 | as sure about FIFOs, sockets, etc., but I kind of doubt |
| 3109 | that people want to exec() these things. */ |
| 3110 | continue; |
| 3111 | break; |
| 3112 | } |
| 3113 | if (p == NULL) |
| 3114 | /* Not found. This must be an error rather than merely passing |
| 3115 | the file to execlp(), because execlp() would try all the |
| 3116 | exec()s, causing GDB to get confused. */ |
| 3117 | error (_("procfs:%d -- Can't find shell %s in PATH"), |
| 3118 | __LINE__, shell_file); |
| 3119 | |
| 3120 | shell_file = tryname; |
| 3121 | } |
| 3122 | |
| 3123 | pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap, |
| 3124 | NULL, NULL, shell_file, NULL); |
| 3125 | |
| 3126 | /* We have something that executes now. We'll be running through |
| 3127 | the shell at this point (if startup-with-shell is true), but the |
| 3128 | pid shouldn't change. */ |
| 3129 | add_thread_silent (pid_to_ptid (pid)); |
| 3130 | |
| 3131 | procfs_init_inferior (this, pid); |
| 3132 | } |
| 3133 | |
| 3134 | /* An observer for the "inferior_created" event. */ |
| 3135 | |
| 3136 | static void |
| 3137 | procfs_inferior_created (struct target_ops *ops, int from_tty) |
| 3138 | { |
| 3139 | } |
| 3140 | |
| 3141 | /* Callback for update_thread_list. Calls "add_thread". */ |
| 3142 | |
| 3143 | static int |
| 3144 | procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr) |
| 3145 | { |
| 3146 | ptid_t gdb_threadid = ptid_build (pi->pid, thread->tid, 0); |
| 3147 | |
| 3148 | if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid)) |
| 3149 | add_thread (gdb_threadid); |
| 3150 | |
| 3151 | return 0; |
| 3152 | } |
| 3153 | |
| 3154 | /* Query all the threads that the target knows about, and give them |
| 3155 | back to GDB to add to its list. */ |
| 3156 | |
| 3157 | void |
| 3158 | procfs_target::update_thread_list () |
| 3159 | { |
| 3160 | procinfo *pi; |
| 3161 | |
| 3162 | prune_threads (); |
| 3163 | |
| 3164 | /* Find procinfo for main process. */ |
| 3165 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3166 | proc_update_threads (pi); |
| 3167 | proc_iterate_over_threads (pi, procfs_notice_thread, NULL); |
| 3168 | } |
| 3169 | |
| 3170 | /* Return true if the thread is still 'alive'. This guy doesn't |
| 3171 | really seem to be doing his job. Got to investigate how to tell |
| 3172 | when a thread is really gone. */ |
| 3173 | |
| 3174 | bool |
| 3175 | procfs_target::thread_alive (ptid_t ptid) |
| 3176 | { |
| 3177 | int proc, thread; |
| 3178 | procinfo *pi; |
| 3179 | |
| 3180 | proc = ptid_get_pid (ptid); |
| 3181 | thread = ptid_get_lwp (ptid); |
| 3182 | /* If I don't know it, it ain't alive! */ |
| 3183 | if ((pi = find_procinfo (proc, thread)) == NULL) |
| 3184 | return false; |
| 3185 | |
| 3186 | /* If I can't get its status, it ain't alive! |
| 3187 | What's more, I need to forget about it! */ |
| 3188 | if (!proc_get_status (pi)) |
| 3189 | { |
| 3190 | destroy_procinfo (pi); |
| 3191 | return false; |
| 3192 | } |
| 3193 | /* I couldn't have got its status if it weren't alive, so it's |
| 3194 | alive. */ |
| 3195 | return true; |
| 3196 | } |
| 3197 | |
| 3198 | /* Convert PTID to a string. Returns the string in a static |
| 3199 | buffer. */ |
| 3200 | |
| 3201 | const char * |
| 3202 | procfs_target::pid_to_str (ptid_t ptid) |
| 3203 | { |
| 3204 | static char buf[80]; |
| 3205 | |
| 3206 | if (ptid_get_lwp (ptid) == 0) |
| 3207 | sprintf (buf, "process %d", ptid_get_pid (ptid)); |
| 3208 | else |
| 3209 | sprintf (buf, "LWP %ld", ptid_get_lwp (ptid)); |
| 3210 | |
| 3211 | return buf; |
| 3212 | } |
| 3213 | |
| 3214 | /* Insert a watchpoint. */ |
| 3215 | |
| 3216 | static int |
| 3217 | procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag, |
| 3218 | int after) |
| 3219 | { |
| 3220 | int pflags = 0; |
| 3221 | procinfo *pi; |
| 3222 | |
| 3223 | pi = find_procinfo_or_die (ptid_get_pid (ptid) == -1 ? |
| 3224 | ptid_get_pid (inferior_ptid) : ptid_get_pid (ptid), |
| 3225 | 0); |
| 3226 | |
| 3227 | /* Translate from GDB's flags to /proc's. */ |
| 3228 | if (len > 0) /* len == 0 means delete watchpoint. */ |
| 3229 | { |
| 3230 | switch (rwflag) { /* FIXME: need an enum! */ |
| 3231 | case hw_write: /* default watchpoint (write) */ |
| 3232 | pflags = WRITE_WATCHFLAG; |
| 3233 | break; |
| 3234 | case hw_read: /* read watchpoint */ |
| 3235 | pflags = READ_WATCHFLAG; |
| 3236 | break; |
| 3237 | case hw_access: /* access watchpoint */ |
| 3238 | pflags = READ_WATCHFLAG | WRITE_WATCHFLAG; |
| 3239 | break; |
| 3240 | case hw_execute: /* execution HW breakpoint */ |
| 3241 | pflags = EXEC_WATCHFLAG; |
| 3242 | break; |
| 3243 | default: /* Something weird. Return error. */ |
| 3244 | return -1; |
| 3245 | } |
| 3246 | if (after) /* Stop after r/w access is completed. */ |
| 3247 | pflags |= AFTER_WATCHFLAG; |
| 3248 | } |
| 3249 | |
| 3250 | if (!proc_set_watchpoint (pi, addr, len, pflags)) |
| 3251 | { |
| 3252 | if (errno == E2BIG) /* Typical error for no resources. */ |
| 3253 | return -1; /* fail */ |
| 3254 | /* GDB may try to remove the same watchpoint twice. |
| 3255 | If a remove request returns no match, don't error. */ |
| 3256 | if (errno == ESRCH && len == 0) |
| 3257 | return 0; /* ignore */ |
| 3258 | proc_error (pi, "set_watchpoint", __LINE__); |
| 3259 | } |
| 3260 | return 0; |
| 3261 | } |
| 3262 | |
| 3263 | /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE |
| 3264 | is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint, |
| 3265 | or bp_hardware_watchpoint. CNT is the number of watchpoints used so |
| 3266 | far. |
| 3267 | |
| 3268 | Note: procfs_can_use_hw_breakpoint() is not yet used by all |
| 3269 | procfs.c targets due to the fact that some of them still define |
| 3270 | target_can_use_hardware_watchpoint. */ |
| 3271 | |
| 3272 | int |
| 3273 | procfs_target::can_use_hw_breakpoint (enum bptype type, int cnt, int othertype) |
| 3274 | { |
| 3275 | /* Due to the way that proc_set_watchpoint() is implemented, host |
| 3276 | and target pointers must be of the same size. If they are not, |
| 3277 | we can't use hardware watchpoints. This limitation is due to the |
| 3278 | fact that proc_set_watchpoint() calls |
| 3279 | procfs_address_to_host_pointer(); a close inspection of |
| 3280 | procfs_address_to_host_pointer will reveal that an internal error |
| 3281 | will be generated when the host and target pointer sizes are |
| 3282 | different. */ |
| 3283 | struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; |
| 3284 | |
| 3285 | if (sizeof (void *) != TYPE_LENGTH (ptr_type)) |
| 3286 | return 0; |
| 3287 | |
| 3288 | /* Other tests here??? */ |
| 3289 | |
| 3290 | return 1; |
| 3291 | } |
| 3292 | |
| 3293 | /* Returns non-zero if process is stopped on a hardware watchpoint |
| 3294 | fault, else returns zero. */ |
| 3295 | |
| 3296 | bool |
| 3297 | procfs_target::stopped_by_watchpoint () |
| 3298 | { |
| 3299 | procinfo *pi; |
| 3300 | |
| 3301 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3302 | |
| 3303 | if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) |
| 3304 | { |
| 3305 | if (proc_why (pi) == PR_FAULTED) |
| 3306 | { |
| 3307 | if (proc_what (pi) == FLTWATCH) |
| 3308 | return true; |
| 3309 | } |
| 3310 | } |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
| 3314 | /* Returns 1 if the OS knows the position of the triggered watchpoint, |
| 3315 | and sets *ADDR to that address. Returns 0 if OS cannot report that |
| 3316 | address. This function is only called if |
| 3317 | procfs_stopped_by_watchpoint returned 1, thus no further checks are |
| 3318 | done. The function also assumes that ADDR is not NULL. */ |
| 3319 | |
| 3320 | bool |
| 3321 | procfs_target::stopped_data_address (CORE_ADDR *addr) |
| 3322 | { |
| 3323 | procinfo *pi; |
| 3324 | |
| 3325 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3326 | return proc_watchpoint_address (pi, addr); |
| 3327 | } |
| 3328 | |
| 3329 | int |
| 3330 | procfs_target::insert_watchpoint (CORE_ADDR addr, int len, |
| 3331 | enum target_hw_bp_type type, |
| 3332 | struct expression *cond) |
| 3333 | { |
| 3334 | if (!target_have_steppable_watchpoint |
| 3335 | && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ())) |
| 3336 | { |
| 3337 | /* When a hardware watchpoint fires off the PC will be left at |
| 3338 | the instruction following the one which caused the |
| 3339 | watchpoint. It will *NOT* be necessary for GDB to step over |
| 3340 | the watchpoint. */ |
| 3341 | return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1); |
| 3342 | } |
| 3343 | else |
| 3344 | { |
| 3345 | /* When a hardware watchpoint fires off the PC will be left at |
| 3346 | the instruction which caused the watchpoint. It will be |
| 3347 | necessary for GDB to step over the watchpoint. */ |
| 3348 | return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0); |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | int |
| 3353 | procfs_target::remove_watchpoint (CORE_ADDR addr, int len, |
| 3354 | enum target_hw_bp_type type, |
| 3355 | struct expression *cond) |
| 3356 | { |
| 3357 | return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0); |
| 3358 | } |
| 3359 | |
| 3360 | int |
| 3361 | procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) |
| 3362 | { |
| 3363 | /* The man page for proc(4) on Solaris 2.6 and up says that the |
| 3364 | system can support "thousands" of hardware watchpoints, but gives |
| 3365 | no method for finding out how many; It doesn't say anything about |
| 3366 | the allowed size for the watched area either. So we just tell |
| 3367 | GDB 'yes'. */ |
| 3368 | return 1; |
| 3369 | } |
| 3370 | |
| 3371 | /* Memory Mappings Functions: */ |
| 3372 | |
| 3373 | /* Call a callback function once for each mapping, passing it the |
| 3374 | mapping, an optional secondary callback function, and some optional |
| 3375 | opaque data. Quit and return the first non-zero value returned |
| 3376 | from the callback. |
| 3377 | |
| 3378 | PI is the procinfo struct for the process to be mapped. FUNC is |
| 3379 | the callback function to be called by this iterator. DATA is the |
| 3380 | optional opaque data to be passed to the callback function. |
| 3381 | CHILD_FUNC is the optional secondary function pointer to be passed |
| 3382 | to the child function. Returns the first non-zero return value |
| 3383 | from the callback function, or zero. */ |
| 3384 | |
| 3385 | static int |
| 3386 | iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func, |
| 3387 | void *data, |
| 3388 | int (*func) (struct prmap *map, |
| 3389 | find_memory_region_ftype child_func, |
| 3390 | void *data)) |
| 3391 | { |
| 3392 | char pathname[MAX_PROC_NAME_SIZE]; |
| 3393 | struct prmap *prmaps; |
| 3394 | struct prmap *prmap; |
| 3395 | int funcstat; |
| 3396 | int nmap; |
| 3397 | struct stat sbuf; |
| 3398 | |
| 3399 | /* Get the number of mappings, allocate space, |
| 3400 | and read the mappings into prmaps. */ |
| 3401 | /* Open map fd. */ |
| 3402 | sprintf (pathname, "/proc/%d/map", pi->pid); |
| 3403 | |
| 3404 | scoped_fd map_fd (open (pathname, O_RDONLY)); |
| 3405 | if (map_fd.get () < 0) |
| 3406 | proc_error (pi, "iterate_over_mappings (open)", __LINE__); |
| 3407 | |
| 3408 | /* Use stat to determine the file size, and compute |
| 3409 | the number of prmap_t objects it contains. */ |
| 3410 | if (fstat (map_fd.get (), &sbuf) != 0) |
| 3411 | proc_error (pi, "iterate_over_mappings (fstat)", __LINE__); |
| 3412 | |
| 3413 | nmap = sbuf.st_size / sizeof (prmap_t); |
| 3414 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); |
| 3415 | if (read (map_fd.get (), (char *) prmaps, nmap * sizeof (*prmaps)) |
| 3416 | != (nmap * sizeof (*prmaps))) |
| 3417 | proc_error (pi, "iterate_over_mappings (read)", __LINE__); |
| 3418 | |
| 3419 | for (prmap = prmaps; nmap > 0; prmap++, nmap--) |
| 3420 | if ((funcstat = (*func) (prmap, child_func, data)) != 0) |
| 3421 | return funcstat; |
| 3422 | |
| 3423 | return 0; |
| 3424 | } |
| 3425 | |
| 3426 | /* Implements the to_find_memory_regions method. Calls an external |
| 3427 | function for each memory region. |
| 3428 | Returns the integer value returned by the callback. */ |
| 3429 | |
| 3430 | static int |
| 3431 | find_memory_regions_callback (struct prmap *map, |
| 3432 | find_memory_region_ftype func, void *data) |
| 3433 | { |
| 3434 | return (*func) ((CORE_ADDR) map->pr_vaddr, |
| 3435 | map->pr_size, |
| 3436 | (map->pr_mflags & MA_READ) != 0, |
| 3437 | (map->pr_mflags & MA_WRITE) != 0, |
| 3438 | (map->pr_mflags & MA_EXEC) != 0, |
| 3439 | 1, /* MODIFIED is unknown, pass it as true. */ |
| 3440 | data); |
| 3441 | } |
| 3442 | |
| 3443 | /* External interface. Calls a callback function once for each |
| 3444 | mapped memory region in the child process, passing as arguments: |
| 3445 | |
| 3446 | CORE_ADDR virtual_address, |
| 3447 | unsigned long size, |
| 3448 | int read, TRUE if region is readable by the child |
| 3449 | int write, TRUE if region is writable by the child |
| 3450 | int execute TRUE if region is executable by the child. |
| 3451 | |
| 3452 | Stops iterating and returns the first non-zero value returned by |
| 3453 | the callback. */ |
| 3454 | |
| 3455 | int |
| 3456 | procfs_target::find_memory_regions (find_memory_region_ftype func, void *data) |
| 3457 | { |
| 3458 | procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3459 | |
| 3460 | return iterate_over_mappings (pi, func, data, |
| 3461 | find_memory_regions_callback); |
| 3462 | } |
| 3463 | |
| 3464 | /* Returns an ascii representation of a memory mapping's flags. */ |
| 3465 | |
| 3466 | static char * |
| 3467 | mappingflags (long flags) |
| 3468 | { |
| 3469 | static char asciiflags[8]; |
| 3470 | |
| 3471 | strcpy (asciiflags, "-------"); |
| 3472 | if (flags & MA_STACK) |
| 3473 | asciiflags[1] = 's'; |
| 3474 | if (flags & MA_BREAK) |
| 3475 | asciiflags[2] = 'b'; |
| 3476 | if (flags & MA_SHARED) |
| 3477 | asciiflags[3] = 's'; |
| 3478 | if (flags & MA_READ) |
| 3479 | asciiflags[4] = 'r'; |
| 3480 | if (flags & MA_WRITE) |
| 3481 | asciiflags[5] = 'w'; |
| 3482 | if (flags & MA_EXEC) |
| 3483 | asciiflags[6] = 'x'; |
| 3484 | return (asciiflags); |
| 3485 | } |
| 3486 | |
| 3487 | /* Callback function, does the actual work for 'info proc |
| 3488 | mappings'. */ |
| 3489 | |
| 3490 | static int |
| 3491 | info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore, |
| 3492 | void *unused) |
| 3493 | { |
| 3494 | unsigned int pr_off; |
| 3495 | |
| 3496 | pr_off = (unsigned int) map->pr_offset; |
| 3497 | |
| 3498 | if (gdbarch_addr_bit (target_gdbarch ()) == 32) |
| 3499 | printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n", |
| 3500 | (unsigned long) map->pr_vaddr, |
| 3501 | (unsigned long) map->pr_vaddr + map->pr_size - 1, |
| 3502 | (unsigned long) map->pr_size, |
| 3503 | pr_off, |
| 3504 | mappingflags (map->pr_mflags)); |
| 3505 | else |
| 3506 | printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n", |
| 3507 | (unsigned long) map->pr_vaddr, |
| 3508 | (unsigned long) map->pr_vaddr + map->pr_size - 1, |
| 3509 | (unsigned long) map->pr_size, |
| 3510 | pr_off, |
| 3511 | mappingflags (map->pr_mflags)); |
| 3512 | |
| 3513 | return 0; |
| 3514 | } |
| 3515 | |
| 3516 | /* Implement the "info proc mappings" subcommand. */ |
| 3517 | |
| 3518 | static void |
| 3519 | info_proc_mappings (procinfo *pi, int summary) |
| 3520 | { |
| 3521 | if (summary) |
| 3522 | return; /* No output for summary mode. */ |
| 3523 | |
| 3524 | printf_filtered (_("Mapped address spaces:\n\n")); |
| 3525 | if (gdbarch_ptr_bit (target_gdbarch ()) == 32) |
| 3526 | printf_filtered ("\t%10s %10s %10s %10s %7s\n", |
| 3527 | "Start Addr", |
| 3528 | " End Addr", |
| 3529 | " Size", |
| 3530 | " Offset", |
| 3531 | "Flags"); |
| 3532 | else |
| 3533 | printf_filtered (" %18s %18s %10s %10s %7s\n", |
| 3534 | "Start Addr", |
| 3535 | " End Addr", |
| 3536 | " Size", |
| 3537 | " Offset", |
| 3538 | "Flags"); |
| 3539 | |
| 3540 | iterate_over_mappings (pi, NULL, NULL, info_mappings_callback); |
| 3541 | printf_filtered ("\n"); |
| 3542 | } |
| 3543 | |
| 3544 | /* Implement the "info proc" command. */ |
| 3545 | |
| 3546 | bool |
| 3547 | procfs_target::info_proc (const char *args, enum info_proc_what what) |
| 3548 | { |
| 3549 | struct cleanup *old_chain; |
| 3550 | procinfo *process = NULL; |
| 3551 | procinfo *thread = NULL; |
| 3552 | char *tmp = NULL; |
| 3553 | int pid = 0; |
| 3554 | int tid = 0; |
| 3555 | int mappings = 0; |
| 3556 | |
| 3557 | switch (what) |
| 3558 | { |
| 3559 | case IP_MINIMAL: |
| 3560 | break; |
| 3561 | |
| 3562 | case IP_MAPPINGS: |
| 3563 | case IP_ALL: |
| 3564 | mappings = 1; |
| 3565 | break; |
| 3566 | |
| 3567 | default: |
| 3568 | error (_("Not supported on this target.")); |
| 3569 | } |
| 3570 | |
| 3571 | old_chain = make_cleanup (null_cleanup, 0); |
| 3572 | gdb_argv built_argv (args); |
| 3573 | for (char *arg : built_argv) |
| 3574 | { |
| 3575 | if (isdigit (arg[0])) |
| 3576 | { |
| 3577 | pid = strtoul (arg, &tmp, 10); |
| 3578 | if (*tmp == '/') |
| 3579 | tid = strtoul (++tmp, NULL, 10); |
| 3580 | } |
| 3581 | else if (arg[0] == '/') |
| 3582 | { |
| 3583 | tid = strtoul (arg + 1, NULL, 10); |
| 3584 | } |
| 3585 | } |
| 3586 | if (pid == 0) |
| 3587 | pid = ptid_get_pid (inferior_ptid); |
| 3588 | if (pid == 0) |
| 3589 | error (_("No current process: you must name one.")); |
| 3590 | else |
| 3591 | { |
| 3592 | /* Have pid, will travel. |
| 3593 | First see if it's a process we're already debugging. */ |
| 3594 | process = find_procinfo (pid, 0); |
| 3595 | if (process == NULL) |
| 3596 | { |
| 3597 | /* No. So open a procinfo for it, but |
| 3598 | remember to close it again when finished. */ |
| 3599 | process = create_procinfo (pid, 0); |
| 3600 | make_cleanup (do_destroy_procinfo_cleanup, process); |
| 3601 | if (!open_procinfo_files (process, FD_CTL)) |
| 3602 | proc_error (process, "info proc, open_procinfo_files", __LINE__); |
| 3603 | } |
| 3604 | } |
| 3605 | if (tid != 0) |
| 3606 | thread = create_procinfo (pid, tid); |
| 3607 | |
| 3608 | if (process) |
| 3609 | { |
| 3610 | printf_filtered (_("process %d flags:\n"), process->pid); |
| 3611 | proc_prettyprint_flags (proc_flags (process), 1); |
| 3612 | if (proc_flags (process) & (PR_STOPPED | PR_ISTOP)) |
| 3613 | proc_prettyprint_why (proc_why (process), proc_what (process), 1); |
| 3614 | if (proc_get_nthreads (process) > 1) |
| 3615 | printf_filtered ("Process has %d threads.\n", |
| 3616 | proc_get_nthreads (process)); |
| 3617 | } |
| 3618 | if (thread) |
| 3619 | { |
| 3620 | printf_filtered (_("thread %d flags:\n"), thread->tid); |
| 3621 | proc_prettyprint_flags (proc_flags (thread), 1); |
| 3622 | if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP)) |
| 3623 | proc_prettyprint_why (proc_why (thread), proc_what (thread), 1); |
| 3624 | } |
| 3625 | |
| 3626 | if (mappings) |
| 3627 | { |
| 3628 | info_proc_mappings (process, 0); |
| 3629 | } |
| 3630 | |
| 3631 | do_cleanups (old_chain); |
| 3632 | |
| 3633 | return true; |
| 3634 | } |
| 3635 | |
| 3636 | /* Modify the status of the system call identified by SYSCALLNUM in |
| 3637 | the set of syscalls that are currently traced/debugged. |
| 3638 | |
| 3639 | If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set |
| 3640 | will be updated. Otherwise, the exit syscalls set will be updated. |
| 3641 | |
| 3642 | If MODE is FLAG_SET, then traces will be enabled. Otherwise, they |
| 3643 | will be disabled. */ |
| 3644 | |
| 3645 | static void |
| 3646 | proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit, |
| 3647 | int mode, int from_tty) |
| 3648 | { |
| 3649 | sysset_t *sysset; |
| 3650 | |
| 3651 | if (entry_or_exit == PR_SYSENTRY) |
| 3652 | sysset = proc_get_traced_sysentry (pi, NULL); |
| 3653 | else |
| 3654 | sysset = proc_get_traced_sysexit (pi, NULL); |
| 3655 | |
| 3656 | if (sysset == NULL) |
| 3657 | proc_error (pi, "proc-trace, get_traced_sysset", __LINE__); |
| 3658 | |
| 3659 | if (mode == FLAG_SET) |
| 3660 | praddset (sysset, syscallnum); |
| 3661 | else |
| 3662 | prdelset (sysset, syscallnum); |
| 3663 | |
| 3664 | if (entry_or_exit == PR_SYSENTRY) |
| 3665 | { |
| 3666 | if (!proc_set_traced_sysentry (pi, sysset)) |
| 3667 | proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__); |
| 3668 | } |
| 3669 | else |
| 3670 | { |
| 3671 | if (!proc_set_traced_sysexit (pi, sysset)) |
| 3672 | proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__); |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | static void |
| 3677 | proc_trace_syscalls (const char *args, int from_tty, int entry_or_exit, int mode) |
| 3678 | { |
| 3679 | procinfo *pi; |
| 3680 | |
| 3681 | if (ptid_get_pid (inferior_ptid) <= 0) |
| 3682 | error (_("you must be debugging a process to use this command.")); |
| 3683 | |
| 3684 | if (args == NULL || args[0] == 0) |
| 3685 | error_no_arg (_("system call to trace")); |
| 3686 | |
| 3687 | pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3688 | if (isdigit (args[0])) |
| 3689 | { |
| 3690 | const int syscallnum = atoi (args); |
| 3691 | |
| 3692 | proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty); |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | static void |
| 3697 | proc_trace_sysentry_cmd (const char *args, int from_tty) |
| 3698 | { |
| 3699 | proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET); |
| 3700 | } |
| 3701 | |
| 3702 | static void |
| 3703 | proc_trace_sysexit_cmd (const char *args, int from_tty) |
| 3704 | { |
| 3705 | proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET); |
| 3706 | } |
| 3707 | |
| 3708 | static void |
| 3709 | proc_untrace_sysentry_cmd (const char *args, int from_tty) |
| 3710 | { |
| 3711 | proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET); |
| 3712 | } |
| 3713 | |
| 3714 | static void |
| 3715 | proc_untrace_sysexit_cmd (const char *args, int from_tty) |
| 3716 | { |
| 3717 | proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET); |
| 3718 | } |
| 3719 | |
| 3720 | void |
| 3721 | _initialize_procfs (void) |
| 3722 | { |
| 3723 | gdb::observers::inferior_created.attach (procfs_inferior_created); |
| 3724 | |
| 3725 | add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd, |
| 3726 | _("Give a trace of entries into the syscall.")); |
| 3727 | add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd, |
| 3728 | _("Give a trace of exits from the syscall.")); |
| 3729 | add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd, |
| 3730 | _("Cancel a trace of entries into the syscall.")); |
| 3731 | add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd, |
| 3732 | _("Cancel a trace of exits from the syscall.")); |
| 3733 | |
| 3734 | add_target (&the_procfs_target); |
| 3735 | } |
| 3736 | |
| 3737 | /* =================== END, GDB "MODULE" =================== */ |
| 3738 | |
| 3739 | |
| 3740 | |
| 3741 | /* miscellaneous stubs: */ |
| 3742 | |
| 3743 | /* The following satisfy a few random symbols mostly created by the |
| 3744 | solaris threads implementation, which I will chase down later. */ |
| 3745 | |
| 3746 | /* Return a pid for which we guarantee we will be able to find a |
| 3747 | 'live' procinfo. */ |
| 3748 | |
| 3749 | ptid_t |
| 3750 | procfs_first_available (void) |
| 3751 | { |
| 3752 | return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1); |
| 3753 | } |
| 3754 | |
| 3755 | /* =================== GCORE .NOTE "MODULE" =================== */ |
| 3756 | |
| 3757 | static char * |
| 3758 | procfs_do_thread_registers (bfd *obfd, ptid_t ptid, |
| 3759 | char *note_data, int *note_size, |
| 3760 | enum gdb_signal stop_signal) |
| 3761 | { |
| 3762 | struct regcache *regcache = get_thread_regcache (ptid); |
| 3763 | gdb_gregset_t gregs; |
| 3764 | gdb_fpregset_t fpregs; |
| 3765 | unsigned long merged_pid; |
| 3766 | |
| 3767 | merged_pid = ptid_get_lwp (ptid) << 16 | ptid_get_pid (ptid); |
| 3768 | |
| 3769 | /* This part is the old method for fetching registers. |
| 3770 | It should be replaced by the newer one using regsets |
| 3771 | once it is implemented in this platform: |
| 3772 | gdbarch_iterate_over_regset_sections(). */ |
| 3773 | |
| 3774 | scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); |
| 3775 | inferior_ptid = ptid; |
| 3776 | target_fetch_registers (regcache, -1); |
| 3777 | |
| 3778 | fill_gregset (regcache, &gregs, -1); |
| 3779 | note_data = (char *) elfcore_write_lwpstatus (obfd, |
| 3780 | note_data, |
| 3781 | note_size, |
| 3782 | merged_pid, |
| 3783 | stop_signal, |
| 3784 | &gregs); |
| 3785 | fill_fpregset (regcache, &fpregs, -1); |
| 3786 | note_data = (char *) elfcore_write_prfpreg (obfd, |
| 3787 | note_data, |
| 3788 | note_size, |
| 3789 | &fpregs, |
| 3790 | sizeof (fpregs)); |
| 3791 | |
| 3792 | return note_data; |
| 3793 | } |
| 3794 | |
| 3795 | struct procfs_corefile_thread_data { |
| 3796 | bfd *obfd; |
| 3797 | char *note_data; |
| 3798 | int *note_size; |
| 3799 | enum gdb_signal stop_signal; |
| 3800 | }; |
| 3801 | |
| 3802 | static int |
| 3803 | procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data) |
| 3804 | { |
| 3805 | struct procfs_corefile_thread_data *args |
| 3806 | = (struct procfs_corefile_thread_data *) data; |
| 3807 | |
| 3808 | if (pi != NULL) |
| 3809 | { |
| 3810 | ptid_t ptid = ptid_build (pi->pid, thread->tid, 0); |
| 3811 | |
| 3812 | args->note_data = procfs_do_thread_registers (args->obfd, ptid, |
| 3813 | args->note_data, |
| 3814 | args->note_size, |
| 3815 | args->stop_signal); |
| 3816 | } |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | static int |
| 3821 | find_signalled_thread (struct thread_info *info, void *data) |
| 3822 | { |
| 3823 | if (info->suspend.stop_signal != GDB_SIGNAL_0 |
| 3824 | && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid)) |
| 3825 | return 1; |
| 3826 | |
| 3827 | return 0; |
| 3828 | } |
| 3829 | |
| 3830 | static enum gdb_signal |
| 3831 | find_stop_signal (void) |
| 3832 | { |
| 3833 | struct thread_info *info = |
| 3834 | iterate_over_threads (find_signalled_thread, NULL); |
| 3835 | |
| 3836 | if (info) |
| 3837 | return info->suspend.stop_signal; |
| 3838 | else |
| 3839 | return GDB_SIGNAL_0; |
| 3840 | } |
| 3841 | |
| 3842 | char * |
| 3843 | procfs_target::make_corefile_notes (bfd *obfd, int *note_size) |
| 3844 | { |
| 3845 | struct cleanup *old_chain; |
| 3846 | gdb_gregset_t gregs; |
| 3847 | gdb_fpregset_t fpregs; |
| 3848 | char fname[16] = {'\0'}; |
| 3849 | char psargs[80] = {'\0'}; |
| 3850 | procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0); |
| 3851 | char *note_data = NULL; |
| 3852 | char *inf_args; |
| 3853 | struct procfs_corefile_thread_data thread_args; |
| 3854 | gdb_byte *auxv; |
| 3855 | int auxv_len; |
| 3856 | enum gdb_signal stop_signal; |
| 3857 | |
| 3858 | if (get_exec_file (0)) |
| 3859 | { |
| 3860 | strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname)); |
| 3861 | fname[sizeof (fname) - 1] = 0; |
| 3862 | strncpy (psargs, get_exec_file (0), sizeof (psargs)); |
| 3863 | psargs[sizeof (psargs) - 1] = 0; |
| 3864 | |
| 3865 | inf_args = get_inferior_args (); |
| 3866 | if (inf_args && *inf_args && |
| 3867 | strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs))) |
| 3868 | { |
| 3869 | strncat (psargs, " ", |
| 3870 | sizeof (psargs) - strlen (psargs)); |
| 3871 | strncat (psargs, inf_args, |
| 3872 | sizeof (psargs) - strlen (psargs)); |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | note_data = (char *) elfcore_write_prpsinfo (obfd, |
| 3877 | note_data, |
| 3878 | note_size, |
| 3879 | fname, |
| 3880 | psargs); |
| 3881 | |
| 3882 | stop_signal = find_stop_signal (); |
| 3883 | |
| 3884 | fill_gregset (get_current_regcache (), &gregs, -1); |
| 3885 | note_data = elfcore_write_pstatus (obfd, note_data, note_size, |
| 3886 | ptid_get_pid (inferior_ptid), |
| 3887 | stop_signal, &gregs); |
| 3888 | |
| 3889 | thread_args.obfd = obfd; |
| 3890 | thread_args.note_data = note_data; |
| 3891 | thread_args.note_size = note_size; |
| 3892 | thread_args.stop_signal = stop_signal; |
| 3893 | proc_iterate_over_threads (pi, procfs_corefile_thread_callback, |
| 3894 | &thread_args); |
| 3895 | note_data = thread_args.note_data; |
| 3896 | |
| 3897 | auxv_len = target_read_alloc (target_stack, TARGET_OBJECT_AUXV, |
| 3898 | NULL, &auxv); |
| 3899 | if (auxv_len > 0) |
| 3900 | { |
| 3901 | note_data = elfcore_write_note (obfd, note_data, note_size, |
| 3902 | "CORE", NT_AUXV, auxv, auxv_len); |
| 3903 | xfree (auxv); |
| 3904 | } |
| 3905 | |
| 3906 | return note_data; |
| 3907 | } |
| 3908 | /* =================== END GCORE .NOTE "MODULE" =================== */ |