| 1 | /* Low level Unix child interface to ttrace, for GDB when running under HP-UX. |
| 2 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, |
| 3 | 1999, 2000, 2001 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | Boston, MA 02111-1307, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "frame.h" |
| 25 | #include "inferior.h" |
| 26 | #include "target.h" |
| 27 | #include "gdb_string.h" |
| 28 | #include "gdb_wait.h" |
| 29 | #include "command.h" |
| 30 | |
| 31 | /* We need pstat functionality so that we can get the exec file |
| 32 | for a process we attach to. |
| 33 | |
| 34 | According to HP, we should use the 64bit interfaces, so we |
| 35 | define _PSTAT64 to achieve this. */ |
| 36 | #define _PSTAT64 |
| 37 | #include <sys/pstat.h> |
| 38 | |
| 39 | /* Some hackery to work around a use of the #define name NO_FLAGS |
| 40 | * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h). |
| 41 | */ |
| 42 | #ifdef NO_FLAGS |
| 43 | #define INFTTRACE_TEMP_HACK NO_FLAGS |
| 44 | #undef NO_FLAGS |
| 45 | #endif |
| 46 | |
| 47 | #ifdef USG |
| 48 | #include <sys/types.h> |
| 49 | #endif |
| 50 | |
| 51 | #include <sys/param.h> |
| 52 | #include <sys/dir.h> |
| 53 | #include <signal.h> |
| 54 | #include <sys/ioctl.h> |
| 55 | |
| 56 | #include <sys/ttrace.h> |
| 57 | #include <sys/mman.h> |
| 58 | |
| 59 | #ifndef NO_PTRACE_H |
| 60 | #ifdef PTRACE_IN_WRONG_PLACE |
| 61 | #include <ptrace.h> |
| 62 | #else |
| 63 | #include <sys/ptrace.h> |
| 64 | #endif |
| 65 | #endif /* NO_PTRACE_H */ |
| 66 | |
| 67 | /* Second half of the hackery above. Non-ANSI C, so |
| 68 | * we can't use "#error", alas. |
| 69 | */ |
| 70 | #ifdef NO_FLAGS |
| 71 | #if (NO_FLAGS != INFTTRACE_TEMP_HACK ) |
| 72 | /* #error "Hackery to remove warning didn't work right" */ |
| 73 | #else |
| 74 | /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */ |
| 75 | #endif |
| 76 | #else |
| 77 | /* #error "Didn't get expected re-definition of NO_FLAGS" */ |
| 78 | #define NO_FLAGS INFTTRACE_TEMP_HACK |
| 79 | #endif |
| 80 | |
| 81 | #if !defined (PT_SETTRC) |
| 82 | #define PT_SETTRC 0 /* Make process traceable by parent */ |
| 83 | #endif |
| 84 | #if !defined (PT_READ_I) |
| 85 | #define PT_READ_I 1 /* Read word from text space */ |
| 86 | #endif |
| 87 | #if !defined (PT_READ_D) |
| 88 | #define PT_READ_D 2 /* Read word from data space */ |
| 89 | #endif |
| 90 | #if !defined (PT_READ_U) |
| 91 | #define PT_READ_U 3 /* Read word from kernel user struct */ |
| 92 | #endif |
| 93 | #if !defined (PT_WRITE_I) |
| 94 | #define PT_WRITE_I 4 /* Write word to text space */ |
| 95 | #endif |
| 96 | #if !defined (PT_WRITE_D) |
| 97 | #define PT_WRITE_D 5 /* Write word to data space */ |
| 98 | #endif |
| 99 | #if !defined (PT_WRITE_U) |
| 100 | #define PT_WRITE_U 6 /* Write word to kernel user struct */ |
| 101 | #endif |
| 102 | #if !defined (PT_CONTINUE) |
| 103 | #define PT_CONTINUE 7 /* Continue after signal */ |
| 104 | #endif |
| 105 | #if !defined (PT_STEP) |
| 106 | #define PT_STEP 9 /* Set flag for single stepping */ |
| 107 | #endif |
| 108 | #if !defined (PT_KILL) |
| 109 | #define PT_KILL 8 /* Send child a SIGKILL signal */ |
| 110 | #endif |
| 111 | |
| 112 | #ifndef PT_ATTACH |
| 113 | #define PT_ATTACH PTRACE_ATTACH |
| 114 | #endif |
| 115 | #ifndef PT_DETACH |
| 116 | #define PT_DETACH PTRACE_DETACH |
| 117 | #endif |
| 118 | |
| 119 | #include "gdbcore.h" |
| 120 | #ifndef NO_SYS_FILE |
| 121 | #include <sys/file.h> |
| 122 | #endif |
| 123 | |
| 124 | /* This semaphore is used to coordinate the child and parent processes |
| 125 | after a fork(), and before an exec() by the child. See parent_attach_all |
| 126 | for details. |
| 127 | */ |
| 128 | typedef struct |
| 129 | { |
| 130 | int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */ |
| 131 | int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */ |
| 132 | } |
| 133 | startup_semaphore_t; |
| 134 | |
| 135 | #define SEM_TALK (1) |
| 136 | #define SEM_LISTEN (0) |
| 137 | |
| 138 | static startup_semaphore_t startup_semaphore; |
| 139 | |
| 140 | /* See can_touch_threads_of_process for details. */ |
| 141 | static int vforking_child_pid = 0; |
| 142 | static int vfork_in_flight = 0; |
| 143 | |
| 144 | /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed). |
| 145 | */ |
| 146 | static pid_t old_gdb_pid = 0; |
| 147 | static pid_t reported_pid = 0; |
| 148 | static int reported_bpt = 0; |
| 149 | |
| 150 | /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise. |
| 151 | */ |
| 152 | #define TT_OK( _status, _errno ) \ |
| 153 | (((_status) == 1) && ((_errno) == 0)) |
| 154 | |
| 155 | #define TTRACE_ARG_TYPE uint64_t |
| 156 | |
| 157 | /* When supplied as the "addr" operand, ttrace interprets this |
| 158 | to mean, "from the current address". |
| 159 | */ |
| 160 | #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC) |
| 161 | |
| 162 | /* When supplied as the "addr", "data" or "addr2" operand for most |
| 163 | requests, ttrace interprets this to mean, "pay no heed to this |
| 164 | argument". |
| 165 | */ |
| 166 | #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG) |
| 167 | |
| 168 | /* This is capable of holding the value of a 32-bit register. The |
| 169 | value is always left-aligned in the buffer; i.e., [0] contains |
| 170 | the most-significant byte of the register's value, and [sizeof(reg)] |
| 171 | contains the least-significant value. |
| 172 | |
| 173 | ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and |
| 174 | that registers are 32-bits on HP-UX. The latter assumption changes |
| 175 | with PA2.0. |
| 176 | */ |
| 177 | typedef int register_value_t; |
| 178 | |
| 179 | /******************************************************************** |
| 180 | |
| 181 | How this works: |
| 182 | |
| 183 | 1. Thread numbers |
| 184 | |
| 185 | The rest of GDB sees threads as being things with different |
| 186 | "pid" (process id) values. See "thread.c" for details. The |
| 187 | separate threads will be seen and reacted to if infttrace passes |
| 188 | back different pid values (for _events_). See wait_for_inferior |
| 189 | in inftarg.c. |
| 190 | |
| 191 | So infttrace is going to use thread ids externally, pretending |
| 192 | they are process ids, and keep track internally so that it can |
| 193 | use the real process id (and thread id) when calling ttrace. |
| 194 | |
| 195 | The data structure that supports this is a linked list of the |
| 196 | current threads. Since at some date infttrace will have to |
| 197 | deal with multiple processes, each list element records its |
| 198 | corresponding pid, rather than having a single global. |
| 199 | |
| 200 | Note that the list is only approximately current; that's ok, as |
| 201 | it's up to date when we need it (we hope!). Also, it can contain |
| 202 | dead threads, as there's no harm if it does. |
| 203 | |
| 204 | The approach taken here is to bury the translation from external |
| 205 | to internal inside "call_ttrace" and a few other places. |
| 206 | |
| 207 | There are some wrinkles: |
| 208 | |
| 209 | o When GDB forks itself to create the debug target process, |
| 210 | there's only a pid of 0 around in the child, so the |
| 211 | TT_PROC_SETTRC operation uses a more direct call to ttrace; |
| 212 | Similiarly, the initial setting of the event mask happens |
| 213 | early as well, and so is also special-cased, and an attach |
| 214 | uses a real pid; |
| 215 | |
| 216 | o We define an unthreaded application as having a "pseudo" |
| 217 | thread; |
| 218 | |
| 219 | o To keep from confusing the rest of GDB, we don't switch |
| 220 | the PID for the pseudo thread to a TID. A table will help: |
| 221 | |
| 222 | Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ... |
| 223 | |
| 224 | Our thread list stores: pid pid pid pid ... |
| 225 | tid0 tid1 tid2 tid3 |
| 226 | |
| 227 | Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ... |
| 228 | |
| 229 | Both pid and tid0 will map to tid0, as there are infttrace.c-internal |
| 230 | calls to ttrace using tid0. |
| 231 | |
| 232 | 2. Step and Continue |
| 233 | |
| 234 | Since we're implementing the "stop the world" model, sub-model |
| 235 | "other threads run during step", we have some stuff to do: |
| 236 | |
| 237 | o User steps require continuing all threads other than the |
| 238 | one the user is stepping; |
| 239 | |
| 240 | o Internal debugger steps (such as over a breakpoint or watchpoint, |
| 241 | but not out of a library load thunk) require stepping only |
| 242 | the selected thread; this means that we have to report the |
| 243 | step finish on that thread, which can lead to complications; |
| 244 | |
| 245 | o When a thread is created, it is created running, rather |
| 246 | than stopped--so we have to stop it. |
| 247 | |
| 248 | The OS doesn't guarantee the stopped thread list will be stable, |
| 249 | no does it guarantee where on the stopped thread list a thread |
| 250 | that is single-stepped will wind up: it's possible that it will |
| 251 | be off the list for a while, it's possible the step will complete |
| 252 | and it will be re-posted to the end... |
| 253 | |
| 254 | This means we have to scan the stopped thread list, build up |
| 255 | a work-list, and then run down the work list; we can't do the |
| 256 | step/continue during the scan. |
| 257 | |
| 258 | 3. Buffering events |
| 259 | |
| 260 | Then there's the issue of waiting for an event. We do this by |
| 261 | noticing how many events are reported at the end of each wait. |
| 262 | From then on, we "fake" all resumes and steps, returning instantly, |
| 263 | and don't do another wait. Once all pending events are reported, |
| 264 | we can really resume again. |
| 265 | |
| 266 | To keep this hidden, all the routines which know about tids and |
| 267 | pids or real events and simulated ones are static (file-local). |
| 268 | |
| 269 | This code can make lots of calls to ttrace, in particular it |
| 270 | can spin down the list of thread states more than once. If this |
| 271 | becomes a performance hit, the spin could be done once and the |
| 272 | various "tsp" blocks saved, keeping all later spins in this |
| 273 | process. |
| 274 | |
| 275 | The O/S doesn't promise to keep the list straight, and so we must |
| 276 | re-scan a lot. By observation, it looks like a single-step/wait |
| 277 | puts the stepped thread at the end of the list but doesn't change |
| 278 | it otherwise. |
| 279 | |
| 280 | **************************************************************** |
| 281 | */ |
| 282 | |
| 283 | /* Uncomment these to turn on various debugging output */ |
| 284 | /* #define THREAD_DEBUG */ |
| 285 | /* #define WAIT_BUFFER_DEBUG */ |
| 286 | /* #define PARANOIA */ |
| 287 | |
| 288 | |
| 289 | #define INFTTRACE_ALL_THREADS (-1) |
| 290 | #define INFTTRACE_STEP (1) |
| 291 | #define INFTTRACE_CONTINUE (0) |
| 292 | |
| 293 | /* FIX: this is used in inftarg.c/child_wait, in a hack. |
| 294 | */ |
| 295 | extern int not_same_real_pid; |
| 296 | |
| 297 | /* This is used to count buffered events. |
| 298 | */ |
| 299 | static unsigned int more_events_left = 0; |
| 300 | |
| 301 | /* Process state. |
| 302 | */ |
| 303 | typedef enum process_state_enum |
| 304 | { |
| 305 | STOPPED, |
| 306 | FAKE_STEPPING, |
| 307 | FAKE_CONTINUE, /* For later use */ |
| 308 | RUNNING, |
| 309 | FORKING, |
| 310 | VFORKING |
| 311 | } |
| 312 | process_state_t; |
| 313 | |
| 314 | static process_state_t process_state = STOPPED; |
| 315 | |
| 316 | /* User-specified stepping modality. |
| 317 | */ |
| 318 | typedef enum stepping_mode_enum |
| 319 | { |
| 320 | DO_DEFAULT, /* ...which is a continue! */ |
| 321 | DO_STEP, |
| 322 | DO_CONTINUE |
| 323 | } |
| 324 | stepping_mode_t; |
| 325 | |
| 326 | /* Action to take on an attach, depends on |
| 327 | * what kind (user command, fork, vfork). |
| 328 | * |
| 329 | * At the moment, this is either: |
| 330 | * |
| 331 | * o continue with a SIGTRAP signal, or |
| 332 | * |
| 333 | * o leave stopped. |
| 334 | */ |
| 335 | typedef enum attach_continue_enum |
| 336 | { |
| 337 | DO_ATTACH_CONTINUE, |
| 338 | DONT_ATTACH_CONTINUE |
| 339 | } |
| 340 | attach_continue_t; |
| 341 | |
| 342 | /* This flag is true if we are doing a step-over-bpt |
| 343 | * with buffered events. We will have to be sure to |
| 344 | * report the right thread, as otherwise the spaghetti |
| 345 | * code in "infrun.c/wait_for_inferior" will get |
| 346 | * confused. |
| 347 | */ |
| 348 | static int doing_fake_step = 0; |
| 349 | static lwpid_t fake_step_tid = 0; |
| 350 | \f |
| 351 | |
| 352 | /**************************************************** |
| 353 | * Thread information structure routines and types. * |
| 354 | **************************************************** |
| 355 | */ |
| 356 | typedef |
| 357 | struct thread_info_struct |
| 358 | { |
| 359 | int am_pseudo; /* This is a pseudo-thread for the process. */ |
| 360 | int pid; /* Process ID */ |
| 361 | lwpid_t tid; /* Thread ID */ |
| 362 | int handled; /* 1 if a buffered event was handled. */ |
| 363 | int seen; /* 1 if this thread was seen on a traverse. */ |
| 364 | int terminated; /* 1 if thread has terminated. */ |
| 365 | int have_signal; /* 1 if signal to be sent */ |
| 366 | enum target_signal signal_value; /* Signal to send */ |
| 367 | int have_start; /* 1 if alternate starting address */ |
| 368 | stepping_mode_t stepping_mode; /* Whether to step or continue */ |
| 369 | CORE_ADDR start; /* Where to start */ |
| 370 | int have_state; /* 1 if the event state has been set */ |
| 371 | ttstate_t last_stop_state; /* The most recently-waited event for this thread. */ |
| 372 | struct thread_info_struct |
| 373 | *next; /* All threads are linked via this field. */ |
| 374 | struct thread_info_struct |
| 375 | *next_pseudo; /* All pseudo-threads are linked via this field. */ |
| 376 | } |
| 377 | thread_info; |
| 378 | |
| 379 | typedef |
| 380 | struct thread_info_header_struct |
| 381 | { |
| 382 | int count; |
| 383 | thread_info *head; |
| 384 | thread_info *head_pseudo; |
| 385 | |
| 386 | } |
| 387 | thread_info_header; |
| 388 | |
| 389 | static thread_info_header thread_head = |
| 390 | {0, NULL, NULL}; |
| 391 | static thread_info_header deleted_threads = |
| 392 | {0, NULL, NULL}; |
| 393 | |
| 394 | static ptid_t saved_real_ptid; |
| 395 | \f |
| 396 | |
| 397 | /************************************************* |
| 398 | * Debugging support functions * |
| 399 | ************************************************* |
| 400 | */ |
| 401 | CORE_ADDR |
| 402 | get_raw_pc (lwpid_t ttid) |
| 403 | { |
| 404 | unsigned long pc_val; |
| 405 | int offset; |
| 406 | int res; |
| 407 | |
| 408 | offset = register_addr (PC_REGNUM, U_REGS_OFFSET); |
| 409 | res = read_from_register_save_state ( |
| 410 | ttid, |
| 411 | (TTRACE_ARG_TYPE) offset, |
| 412 | (char *) &pc_val, |
| 413 | sizeof (pc_val)); |
| 414 | if (res <= 0) |
| 415 | { |
| 416 | return (CORE_ADDR) pc_val; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | return (CORE_ADDR) 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | static char * |
| 425 | get_printable_name_of_stepping_mode (stepping_mode_t mode) |
| 426 | { |
| 427 | switch (mode) |
| 428 | { |
| 429 | case DO_DEFAULT: |
| 430 | return "DO_DEFAULT"; |
| 431 | case DO_STEP: |
| 432 | return "DO_STEP"; |
| 433 | case DO_CONTINUE: |
| 434 | return "DO_CONTINUE"; |
| 435 | default: |
| 436 | return "?unknown mode?"; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* This function returns a pointer to a string describing the |
| 441 | * ttrace event being reported. |
| 442 | */ |
| 443 | char * |
| 444 | get_printable_name_of_ttrace_event (ttevents_t event) |
| 445 | { |
| 446 | /* This enumeration is "gappy", so don't use a table. */ |
| 447 | switch (event) |
| 448 | { |
| 449 | |
| 450 | case TTEVT_NONE: |
| 451 | return "TTEVT_NONE"; |
| 452 | case TTEVT_SIGNAL: |
| 453 | return "TTEVT_SIGNAL"; |
| 454 | case TTEVT_FORK: |
| 455 | return "TTEVT_FORK"; |
| 456 | case TTEVT_EXEC: |
| 457 | return "TTEVT_EXEC"; |
| 458 | case TTEVT_EXIT: |
| 459 | return "TTEVT_EXIT"; |
| 460 | case TTEVT_VFORK: |
| 461 | return "TTEVT_VFORK"; |
| 462 | case TTEVT_SYSCALL_RETURN: |
| 463 | return "TTEVT_SYSCALL_RETURN"; |
| 464 | case TTEVT_LWP_CREATE: |
| 465 | return "TTEVT_LWP_CREATE"; |
| 466 | case TTEVT_LWP_TERMINATE: |
| 467 | return "TTEVT_LWP_TERMINATE"; |
| 468 | case TTEVT_LWP_EXIT: |
| 469 | return "TTEVT_LWP_EXIT"; |
| 470 | case TTEVT_LWP_ABORT_SYSCALL: |
| 471 | return "TTEVT_LWP_ABORT_SYSCALL"; |
| 472 | case TTEVT_SYSCALL_ENTRY: |
| 473 | return "TTEVT_SYSCALL_ENTRY"; |
| 474 | case TTEVT_SYSCALL_RESTART: |
| 475 | return "TTEVT_SYSCALL_RESTART"; |
| 476 | default: |
| 477 | return "?new event?"; |
| 478 | } |
| 479 | } |
| 480 | \f |
| 481 | |
| 482 | /* This function translates the ttrace request enumeration into |
| 483 | * a character string that is its printable (aka "human readable") |
| 484 | * name. |
| 485 | */ |
| 486 | char * |
| 487 | get_printable_name_of_ttrace_request (ttreq_t request) |
| 488 | { |
| 489 | if (!IS_TTRACE_REQ (request)) |
| 490 | return "?bad req?"; |
| 491 | |
| 492 | /* This enumeration is "gappy", so don't use a table. */ |
| 493 | switch (request) |
| 494 | { |
| 495 | case TT_PROC_SETTRC: |
| 496 | return "TT_PROC_SETTRC"; |
| 497 | case TT_PROC_ATTACH: |
| 498 | return "TT_PROC_ATTACH"; |
| 499 | case TT_PROC_DETACH: |
| 500 | return "TT_PROC_DETACH"; |
| 501 | case TT_PROC_RDTEXT: |
| 502 | return "TT_PROC_RDTEXT"; |
| 503 | case TT_PROC_WRTEXT: |
| 504 | return "TT_PROC_WRTEXT"; |
| 505 | case TT_PROC_RDDATA: |
| 506 | return "TT_PROC_RDDATA"; |
| 507 | case TT_PROC_WRDATA: |
| 508 | return "TT_PROC_WRDATA"; |
| 509 | case TT_PROC_STOP: |
| 510 | return "TT_PROC_STOP"; |
| 511 | case TT_PROC_CONTINUE: |
| 512 | return "TT_PROC_CONTINUE"; |
| 513 | case TT_PROC_GET_PATHNAME: |
| 514 | return "TT_PROC_GET_PATHNAME"; |
| 515 | case TT_PROC_GET_EVENT_MASK: |
| 516 | return "TT_PROC_GET_EVENT_MASK"; |
| 517 | case TT_PROC_SET_EVENT_MASK: |
| 518 | return "TT_PROC_SET_EVENT_MASK"; |
| 519 | case TT_PROC_GET_FIRST_LWP_STATE: |
| 520 | return "TT_PROC_GET_FIRST_LWP_STATE"; |
| 521 | case TT_PROC_GET_NEXT_LWP_STATE: |
| 522 | return "TT_PROC_GET_NEXT_LWP_STATE"; |
| 523 | case TT_PROC_EXIT: |
| 524 | return "TT_PROC_EXIT"; |
| 525 | case TT_PROC_GET_MPROTECT: |
| 526 | return "TT_PROC_GET_MPROTECT"; |
| 527 | case TT_PROC_SET_MPROTECT: |
| 528 | return "TT_PROC_SET_MPROTECT"; |
| 529 | case TT_PROC_SET_SCBM: |
| 530 | return "TT_PROC_SET_SCBM"; |
| 531 | case TT_LWP_STOP: |
| 532 | return "TT_LWP_STOP"; |
| 533 | case TT_LWP_CONTINUE: |
| 534 | return "TT_LWP_CONTINUE"; |
| 535 | case TT_LWP_SINGLE: |
| 536 | return "TT_LWP_SINGLE"; |
| 537 | case TT_LWP_RUREGS: |
| 538 | return "TT_LWP_RUREGS"; |
| 539 | case TT_LWP_WUREGS: |
| 540 | return "TT_LWP_WUREGS"; |
| 541 | case TT_LWP_GET_EVENT_MASK: |
| 542 | return "TT_LWP_GET_EVENT_MASK"; |
| 543 | case TT_LWP_SET_EVENT_MASK: |
| 544 | return "TT_LWP_SET_EVENT_MASK"; |
| 545 | case TT_LWP_GET_STATE: |
| 546 | return "TT_LWP_GET_STATE"; |
| 547 | default: |
| 548 | return "?new req?"; |
| 549 | } |
| 550 | } |
| 551 | \f |
| 552 | |
| 553 | /* This function translates the process state enumeration into |
| 554 | * a character string that is its printable (aka "human readable") |
| 555 | * name. |
| 556 | */ |
| 557 | static char * |
| 558 | get_printable_name_of_process_state (process_state_t process_state) |
| 559 | { |
| 560 | switch (process_state) |
| 561 | { |
| 562 | case STOPPED: |
| 563 | return "STOPPED"; |
| 564 | case FAKE_STEPPING: |
| 565 | return "FAKE_STEPPING"; |
| 566 | case RUNNING: |
| 567 | return "RUNNING"; |
| 568 | case FORKING: |
| 569 | return "FORKING"; |
| 570 | case VFORKING: |
| 571 | return "VFORKING"; |
| 572 | default: |
| 573 | return "?some unknown state?"; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* Set a ttrace thread state to a safe, initial state. |
| 578 | */ |
| 579 | static void |
| 580 | clear_ttstate_t (ttstate_t *tts) |
| 581 | { |
| 582 | tts->tts_pid = 0; |
| 583 | tts->tts_lwpid = 0; |
| 584 | tts->tts_user_tid = 0; |
| 585 | tts->tts_event = TTEVT_NONE; |
| 586 | } |
| 587 | |
| 588 | /* Copy ttrace thread state TTS_FROM into TTS_TO. |
| 589 | */ |
| 590 | static void |
| 591 | copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from) |
| 592 | { |
| 593 | memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to)); |
| 594 | } |
| 595 | |
| 596 | /* Are there any live threads we know about? |
| 597 | */ |
| 598 | static int |
| 599 | any_thread_records (void) |
| 600 | { |
| 601 | return (thread_head.count > 0); |
| 602 | } |
| 603 | |
| 604 | /* Create, fill in and link in a thread descriptor. |
| 605 | */ |
| 606 | static thread_info * |
| 607 | create_thread_info (int pid, lwpid_t tid) |
| 608 | { |
| 609 | thread_info *new_p; |
| 610 | thread_info *p; |
| 611 | int thread_count_of_pid; |
| 612 | |
| 613 | new_p = xmalloc (sizeof (thread_info)); |
| 614 | new_p->pid = pid; |
| 615 | new_p->tid = tid; |
| 616 | new_p->have_signal = 0; |
| 617 | new_p->have_start = 0; |
| 618 | new_p->have_state = 0; |
| 619 | clear_ttstate_t (&new_p->last_stop_state); |
| 620 | new_p->am_pseudo = 0; |
| 621 | new_p->handled = 0; |
| 622 | new_p->seen = 0; |
| 623 | new_p->terminated = 0; |
| 624 | new_p->next = NULL; |
| 625 | new_p->next_pseudo = NULL; |
| 626 | new_p->stepping_mode = DO_DEFAULT; |
| 627 | |
| 628 | if (0 == thread_head.count) |
| 629 | { |
| 630 | #ifdef THREAD_DEBUG |
| 631 | if (debug_on) |
| 632 | printf ("First thread, pid %d tid %d!\n", pid, tid); |
| 633 | #endif |
| 634 | saved_real_ptid = inferior_ptid; |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | #ifdef THREAD_DEBUG |
| 639 | if (debug_on) |
| 640 | printf ("Subsequent thread, pid %d tid %d\n", pid, tid); |
| 641 | #endif |
| 642 | } |
| 643 | |
| 644 | /* Another day, another thread... |
| 645 | */ |
| 646 | thread_head.count++; |
| 647 | |
| 648 | /* The new thread always goes at the head of the list. |
| 649 | */ |
| 650 | new_p->next = thread_head.head; |
| 651 | thread_head.head = new_p; |
| 652 | |
| 653 | /* Is this the "pseudo" thread of a process? It is if there's |
| 654 | * no other thread for this process on the list. (Note that this |
| 655 | * accomodates multiple processes, such as we see even for simple |
| 656 | * cases like forking "non-threaded" programs.) |
| 657 | */ |
| 658 | p = thread_head.head; |
| 659 | thread_count_of_pid = 0; |
| 660 | while (p) |
| 661 | { |
| 662 | if (p->pid == new_p->pid) |
| 663 | thread_count_of_pid++; |
| 664 | p = p->next; |
| 665 | } |
| 666 | |
| 667 | /* Did we see any other threads for this pid? (Recall that we just |
| 668 | * added this thread to the list...) |
| 669 | */ |
| 670 | if (thread_count_of_pid == 1) |
| 671 | { |
| 672 | new_p->am_pseudo = 1; |
| 673 | new_p->next_pseudo = thread_head.head_pseudo; |
| 674 | thread_head.head_pseudo = new_p; |
| 675 | } |
| 676 | |
| 677 | return new_p; |
| 678 | } |
| 679 | |
| 680 | /* Get rid of our thread info. |
| 681 | */ |
| 682 | static void |
| 683 | clear_thread_info (void) |
| 684 | { |
| 685 | thread_info *p; |
| 686 | thread_info *q; |
| 687 | |
| 688 | #ifdef THREAD_DEBUG |
| 689 | if (debug_on) |
| 690 | printf ("Clearing all thread info\n"); |
| 691 | #endif |
| 692 | |
| 693 | p = thread_head.head; |
| 694 | while (p) |
| 695 | { |
| 696 | q = p; |
| 697 | p = p->next; |
| 698 | xfree (q); |
| 699 | } |
| 700 | |
| 701 | thread_head.head = NULL; |
| 702 | thread_head.head_pseudo = NULL; |
| 703 | thread_head.count = 0; |
| 704 | |
| 705 | p = deleted_threads.head; |
| 706 | while (p) |
| 707 | { |
| 708 | q = p; |
| 709 | p = p->next; |
| 710 | xfree (q); |
| 711 | } |
| 712 | |
| 713 | deleted_threads.head = NULL; |
| 714 | deleted_threads.head_pseudo = NULL; |
| 715 | deleted_threads.count = 0; |
| 716 | |
| 717 | /* No threads, so can't have pending events. |
| 718 | */ |
| 719 | more_events_left = 0; |
| 720 | } |
| 721 | |
| 722 | /* Given a tid, find the thread block for it. |
| 723 | */ |
| 724 | static thread_info * |
| 725 | find_thread_info (lwpid_t tid) |
| 726 | { |
| 727 | thread_info *p; |
| 728 | |
| 729 | for (p = thread_head.head; p; p = p->next) |
| 730 | { |
| 731 | if (p->tid == tid) |
| 732 | { |
| 733 | return p; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | for (p = deleted_threads.head; p; p = p->next) |
| 738 | { |
| 739 | if (p->tid == tid) |
| 740 | { |
| 741 | return p; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | /* For any but the pseudo thread, this maps to the |
| 749 | * thread ID. For the pseudo thread, if you pass either |
| 750 | * the thread id or the PID, you get the pseudo thread ID. |
| 751 | * |
| 752 | * We have to be prepared for core gdb to ask about |
| 753 | * deleted threads. We do the map, but we don't like it. |
| 754 | */ |
| 755 | static lwpid_t |
| 756 | map_from_gdb_tid (lwpid_t gdb_tid) |
| 757 | { |
| 758 | thread_info *p; |
| 759 | |
| 760 | /* First assume gdb_tid really is a tid, and try to find a |
| 761 | * matching entry on the threads list. |
| 762 | */ |
| 763 | for (p = thread_head.head; p; p = p->next) |
| 764 | { |
| 765 | if (p->tid == gdb_tid) |
| 766 | return gdb_tid; |
| 767 | } |
| 768 | |
| 769 | /* It doesn't appear to be a tid; perhaps it's really a pid? |
| 770 | * Try to find a "pseudo" thread entry on the threads list. |
| 771 | */ |
| 772 | for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo) |
| 773 | { |
| 774 | if (p->pid == gdb_tid) |
| 775 | return p->tid; |
| 776 | } |
| 777 | |
| 778 | /* Perhaps it's the tid of a deleted thread we may still |
| 779 | * have some knowledge of? |
| 780 | */ |
| 781 | for (p = deleted_threads.head; p; p = p->next) |
| 782 | { |
| 783 | if (p->tid == gdb_tid) |
| 784 | return gdb_tid; |
| 785 | } |
| 786 | |
| 787 | /* Or perhaps it's the pid of a deleted process we may still |
| 788 | * have knowledge of? |
| 789 | */ |
| 790 | for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo) |
| 791 | { |
| 792 | if (p->pid == gdb_tid) |
| 793 | return p->tid; |
| 794 | } |
| 795 | |
| 796 | return 0; /* Error? */ |
| 797 | } |
| 798 | |
| 799 | /* Map the other way: from a real tid to the |
| 800 | * "pid" known by core gdb. This tid may be |
| 801 | * for a thread that just got deleted, so we |
| 802 | * also need to consider deleted threads. |
| 803 | */ |
| 804 | static lwpid_t |
| 805 | map_to_gdb_tid (lwpid_t real_tid) |
| 806 | { |
| 807 | thread_info *p; |
| 808 | |
| 809 | for (p = thread_head.head; p; p = p->next) |
| 810 | { |
| 811 | if (p->tid == real_tid) |
| 812 | { |
| 813 | if (p->am_pseudo) |
| 814 | return p->pid; |
| 815 | else |
| 816 | return real_tid; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | for (p = deleted_threads.head; p; p = p->next) |
| 821 | { |
| 822 | if (p->tid == real_tid) |
| 823 | if (p->am_pseudo) |
| 824 | return p->pid; /* Error? */ |
| 825 | else |
| 826 | return real_tid; |
| 827 | } |
| 828 | |
| 829 | return 0; /* Error? Never heard of this thread! */ |
| 830 | } |
| 831 | |
| 832 | /* Do any threads have saved signals? |
| 833 | */ |
| 834 | static int |
| 835 | saved_signals_exist (void) |
| 836 | { |
| 837 | thread_info *p; |
| 838 | |
| 839 | for (p = thread_head.head; p; p = p->next) |
| 840 | { |
| 841 | if (p->have_signal) |
| 842 | { |
| 843 | return 1; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /* Is this the tid for the zero-th thread? |
| 851 | */ |
| 852 | static int |
| 853 | is_pseudo_thread (lwpid_t tid) |
| 854 | { |
| 855 | thread_info *p = find_thread_info (tid); |
| 856 | if (NULL == p || p->terminated) |
| 857 | return 0; |
| 858 | else |
| 859 | return p->am_pseudo; |
| 860 | } |
| 861 | |
| 862 | /* Is this thread terminated? |
| 863 | */ |
| 864 | static int |
| 865 | is_terminated (lwpid_t tid) |
| 866 | { |
| 867 | thread_info *p = find_thread_info (tid); |
| 868 | |
| 869 | if (NULL != p) |
| 870 | return p->terminated; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Is this pid a real PID or a TID? |
| 876 | */ |
| 877 | static int |
| 878 | is_process_id (int pid) |
| 879 | { |
| 880 | lwpid_t tid; |
| 881 | thread_info *tinfo; |
| 882 | pid_t this_pid; |
| 883 | int this_pid_count; |
| 884 | |
| 885 | /* What does PID really represent? |
| 886 | */ |
| 887 | tid = map_from_gdb_tid (pid); |
| 888 | if (tid <= 0) |
| 889 | return 0; /* Actually, is probably an error... */ |
| 890 | |
| 891 | tinfo = find_thread_info (tid); |
| 892 | |
| 893 | /* Does it appear to be a true thread? |
| 894 | */ |
| 895 | if (!tinfo->am_pseudo) |
| 896 | return 0; |
| 897 | |
| 898 | /* Else, it looks like it may be a process. See if there's any other |
| 899 | * threads with the same process ID, though. If there are, then TID |
| 900 | * just happens to be the first thread of several for this process. |
| 901 | */ |
| 902 | this_pid = tinfo->pid; |
| 903 | this_pid_count = 0; |
| 904 | for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next) |
| 905 | { |
| 906 | if (tinfo->pid == this_pid) |
| 907 | this_pid_count++; |
| 908 | } |
| 909 | |
| 910 | return (this_pid_count == 1); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | /* Add a thread to our info. Prevent duplicate entries. |
| 915 | */ |
| 916 | static thread_info * |
| 917 | add_tthread (int pid, lwpid_t tid) |
| 918 | { |
| 919 | thread_info *p; |
| 920 | |
| 921 | p = find_thread_info (tid); |
| 922 | if (NULL == p) |
| 923 | p = create_thread_info (pid, tid); |
| 924 | |
| 925 | return p; |
| 926 | } |
| 927 | |
| 928 | /* Notice that a thread was deleted. |
| 929 | */ |
| 930 | static void |
| 931 | del_tthread (lwpid_t tid) |
| 932 | { |
| 933 | thread_info *p; |
| 934 | thread_info *chase; |
| 935 | |
| 936 | if (thread_head.count <= 0) |
| 937 | { |
| 938 | error ("Internal error in thread database."); |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | chase = NULL; |
| 943 | for (p = thread_head.head; p; p = p->next) |
| 944 | { |
| 945 | if (p->tid == tid) |
| 946 | { |
| 947 | |
| 948 | #ifdef THREAD_DEBUG |
| 949 | if (debug_on) |
| 950 | printf ("Delete here: %d \n", tid); |
| 951 | #endif |
| 952 | |
| 953 | if (p->am_pseudo) |
| 954 | { |
| 955 | /* |
| 956 | * Deleting a main thread is ok if we're doing |
| 957 | * a parent-follow on a child; this is odd but |
| 958 | * not wrong. It apparently _doesn't_ happen |
| 959 | * on the child-follow, as we don't just delete |
| 960 | * the pseudo while keeping the rest of the |
| 961 | * threads around--instead, we clear out the whole |
| 962 | * thread list at once. |
| 963 | */ |
| 964 | thread_info *q; |
| 965 | thread_info *q_chase; |
| 966 | |
| 967 | q_chase = NULL; |
| 968 | for (q = thread_head.head_pseudo; q; q = q->next) |
| 969 | { |
| 970 | if (q == p) |
| 971 | { |
| 972 | /* Remove from pseudo list. |
| 973 | */ |
| 974 | if (q_chase == NULL) |
| 975 | thread_head.head_pseudo = p->next_pseudo; |
| 976 | else |
| 977 | q_chase->next = p->next_pseudo; |
| 978 | } |
| 979 | else |
| 980 | q_chase = q; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /* Remove from live list. |
| 985 | */ |
| 986 | thread_head.count--; |
| 987 | |
| 988 | if (NULL == chase) |
| 989 | thread_head.head = p->next; |
| 990 | else |
| 991 | chase->next = p->next; |
| 992 | |
| 993 | /* Add to deleted thread list. |
| 994 | */ |
| 995 | p->next = deleted_threads.head; |
| 996 | deleted_threads.head = p; |
| 997 | deleted_threads.count++; |
| 998 | if (p->am_pseudo) |
| 999 | { |
| 1000 | p->next_pseudo = deleted_threads.head_pseudo; |
| 1001 | deleted_threads.head_pseudo = p; |
| 1002 | } |
| 1003 | p->terminated = 1; |
| 1004 | |
| 1005 | return; |
| 1006 | } |
| 1007 | |
| 1008 | else |
| 1009 | chase = p; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | /* Get the pid for this tid. (Has to be a real TID!). |
| 1014 | */ |
| 1015 | static int |
| 1016 | get_pid_for (lwpid_t tid) |
| 1017 | { |
| 1018 | thread_info *p; |
| 1019 | |
| 1020 | for (p = thread_head.head; p; p = p->next) |
| 1021 | { |
| 1022 | if (p->tid == tid) |
| 1023 | { |
| 1024 | return p->pid; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | for (p = deleted_threads.head; p; p = p->next) |
| 1029 | { |
| 1030 | if (p->tid == tid) |
| 1031 | { |
| 1032 | return p->pid; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /* Note that this thread's current event has been handled. |
| 1040 | */ |
| 1041 | static void |
| 1042 | set_handled (int pid, lwpid_t tid) |
| 1043 | { |
| 1044 | thread_info *p; |
| 1045 | |
| 1046 | p = find_thread_info (tid); |
| 1047 | if (NULL == p) |
| 1048 | p = add_tthread (pid, tid); |
| 1049 | |
| 1050 | p->handled = 1; |
| 1051 | } |
| 1052 | |
| 1053 | /* Was this thread's current event handled? |
| 1054 | */ |
| 1055 | static int |
| 1056 | was_handled (lwpid_t tid) |
| 1057 | { |
| 1058 | thread_info *p; |
| 1059 | |
| 1060 | p = find_thread_info (tid); |
| 1061 | if (NULL != p) |
| 1062 | return p->handled; |
| 1063 | |
| 1064 | return 0; /* New threads have not been handled */ |
| 1065 | } |
| 1066 | |
| 1067 | /* Set this thread to unhandled. |
| 1068 | */ |
| 1069 | static void |
| 1070 | clear_handled (lwpid_t tid) |
| 1071 | { |
| 1072 | thread_info *p; |
| 1073 | |
| 1074 | #ifdef WAIT_BUFFER_DEBUG |
| 1075 | if (debug_on) |
| 1076 | printf ("clear_handled %d\n", (int) tid); |
| 1077 | #endif |
| 1078 | |
| 1079 | p = find_thread_info (tid); |
| 1080 | if (p == NULL) |
| 1081 | error ("Internal error: No thread state to clear?"); |
| 1082 | |
| 1083 | p->handled = 0; |
| 1084 | } |
| 1085 | |
| 1086 | /* Set all threads to unhandled. |
| 1087 | */ |
| 1088 | static void |
| 1089 | clear_all_handled (void) |
| 1090 | { |
| 1091 | thread_info *p; |
| 1092 | |
| 1093 | #ifdef WAIT_BUFFER_DEBUG |
| 1094 | if (debug_on) |
| 1095 | printf ("clear_all_handled\n"); |
| 1096 | #endif |
| 1097 | |
| 1098 | for (p = thread_head.head; p; p = p->next) |
| 1099 | { |
| 1100 | p->handled = 0; |
| 1101 | } |
| 1102 | |
| 1103 | for (p = deleted_threads.head; p; p = p->next) |
| 1104 | { |
| 1105 | p->handled = 0; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /* Set this thread to default stepping mode. |
| 1110 | */ |
| 1111 | static void |
| 1112 | clear_stepping_mode (lwpid_t tid) |
| 1113 | { |
| 1114 | thread_info *p; |
| 1115 | |
| 1116 | #ifdef WAIT_BUFFER_DEBUG |
| 1117 | if (debug_on) |
| 1118 | printf ("clear_stepping_mode %d\n", (int) tid); |
| 1119 | #endif |
| 1120 | |
| 1121 | p = find_thread_info (tid); |
| 1122 | if (p == NULL) |
| 1123 | error ("Internal error: No thread state to clear?"); |
| 1124 | |
| 1125 | p->stepping_mode = DO_DEFAULT; |
| 1126 | } |
| 1127 | |
| 1128 | /* Set all threads to do default continue on resume. |
| 1129 | */ |
| 1130 | static void |
| 1131 | clear_all_stepping_mode (void) |
| 1132 | { |
| 1133 | thread_info *p; |
| 1134 | |
| 1135 | #ifdef WAIT_BUFFER_DEBUG |
| 1136 | if (debug_on) |
| 1137 | printf ("clear_all_stepping_mode\n"); |
| 1138 | #endif |
| 1139 | |
| 1140 | for (p = thread_head.head; p; p = p->next) |
| 1141 | { |
| 1142 | p->stepping_mode = DO_DEFAULT; |
| 1143 | } |
| 1144 | |
| 1145 | for (p = deleted_threads.head; p; p = p->next) |
| 1146 | { |
| 1147 | p->stepping_mode = DO_DEFAULT; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | /* Set all threads to unseen on this pass. |
| 1152 | */ |
| 1153 | static void |
| 1154 | set_all_unseen (void) |
| 1155 | { |
| 1156 | thread_info *p; |
| 1157 | |
| 1158 | for (p = thread_head.head; p; p = p->next) |
| 1159 | { |
| 1160 | p->seen = 0; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | #if (defined( THREAD_DEBUG ) || defined( PARANOIA )) |
| 1165 | /* debugging routine. |
| 1166 | */ |
| 1167 | static void |
| 1168 | print_tthread (thread_info *p) |
| 1169 | { |
| 1170 | printf (" Thread pid %d, tid %d", p->pid, p->tid); |
| 1171 | if (p->have_state) |
| 1172 | printf (", event is %s", |
| 1173 | get_printable_name_of_ttrace_event (p->last_stop_state.tts_event)); |
| 1174 | |
| 1175 | if (p->am_pseudo) |
| 1176 | printf (", pseudo thread"); |
| 1177 | |
| 1178 | if (p->have_signal) |
| 1179 | printf (", have signal 0x%x", p->signal_value); |
| 1180 | |
| 1181 | if (p->have_start) |
| 1182 | printf (", have start at 0x%x", p->start); |
| 1183 | |
| 1184 | printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode)); |
| 1185 | |
| 1186 | if (p->handled) |
| 1187 | printf (", handled"); |
| 1188 | else |
| 1189 | printf (", not handled"); |
| 1190 | |
| 1191 | if (p->seen) |
| 1192 | printf (", seen"); |
| 1193 | else |
| 1194 | printf (", not seen"); |
| 1195 | |
| 1196 | printf ("\n"); |
| 1197 | } |
| 1198 | |
| 1199 | static void |
| 1200 | print_tthreads (void) |
| 1201 | { |
| 1202 | thread_info *p; |
| 1203 | |
| 1204 | if (thread_head.count == 0) |
| 1205 | printf ("Thread list is empty\n"); |
| 1206 | else |
| 1207 | { |
| 1208 | printf ("Thread list has "); |
| 1209 | if (thread_head.count == 1) |
| 1210 | printf ("1 entry:\n"); |
| 1211 | else |
| 1212 | printf ("%d entries:\n", thread_head.count); |
| 1213 | for (p = thread_head.head; p; p = p->next) |
| 1214 | { |
| 1215 | print_tthread (p); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (deleted_threads.count == 0) |
| 1220 | printf ("Deleted thread list is empty\n"); |
| 1221 | else |
| 1222 | { |
| 1223 | printf ("Deleted thread list has "); |
| 1224 | if (deleted_threads.count == 1) |
| 1225 | printf ("1 entry:\n"); |
| 1226 | else |
| 1227 | printf ("%d entries:\n", deleted_threads.count); |
| 1228 | |
| 1229 | for (p = deleted_threads.head; p; p = p->next) |
| 1230 | { |
| 1231 | print_tthread (p); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | #endif |
| 1236 | |
| 1237 | /* Update the thread list based on the "seen" bits. |
| 1238 | */ |
| 1239 | static void |
| 1240 | update_thread_list (void) |
| 1241 | { |
| 1242 | thread_info *p; |
| 1243 | thread_info *chase; |
| 1244 | |
| 1245 | chase = NULL; |
| 1246 | for (p = thread_head.head; p; p = p->next) |
| 1247 | { |
| 1248 | /* Is this an "unseen" thread which really happens to be a process? |
| 1249 | If so, is it inferior_ptid and is a vfork in flight? If yes to |
| 1250 | all, then DON'T REMOVE IT! We're in the midst of moving a vfork |
| 1251 | operation, which is a multiple step thing, to the point where we |
| 1252 | can touch the parent again. We've most likely stopped to examine |
| 1253 | the child at a late stage in the vfork, and if we're not following |
| 1254 | the child, we'd best not treat the parent as a dead "thread"... |
| 1255 | */ |
| 1256 | if ((!p->seen) && p->am_pseudo && vfork_in_flight |
| 1257 | && (p->pid != vforking_child_pid)) |
| 1258 | p->seen = 1; |
| 1259 | |
| 1260 | if (!p->seen) |
| 1261 | { |
| 1262 | /* Remove this one |
| 1263 | */ |
| 1264 | |
| 1265 | #ifdef THREAD_DEBUG |
| 1266 | if (debug_on) |
| 1267 | printf ("Delete unseen thread: %d \n", p->tid); |
| 1268 | #endif |
| 1269 | del_tthread (p->tid); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | \f |
| 1274 | |
| 1275 | |
| 1276 | /************************************************ |
| 1277 | * O/S call wrappers * |
| 1278 | ************************************************ |
| 1279 | */ |
| 1280 | |
| 1281 | /* This function simply calls ttrace with the given arguments. |
| 1282 | * It exists so that all calls to ttrace are isolated. All |
| 1283 | * parameters should be as specified by "man 2 ttrace". |
| 1284 | * |
| 1285 | * No other "raw" calls to ttrace should exist in this module. |
| 1286 | */ |
| 1287 | static int |
| 1288 | call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr, |
| 1289 | TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2) |
| 1290 | { |
| 1291 | int tt_status; |
| 1292 | |
| 1293 | errno = 0; |
| 1294 | tt_status = ttrace (request, pid, tid, addr, data, addr2); |
| 1295 | |
| 1296 | #ifdef THREAD_DEBUG |
| 1297 | if (errno) |
| 1298 | { |
| 1299 | /* Don't bother for a known benign error: if you ask for the |
| 1300 | * first thread state, but there is only one thread and it's |
| 1301 | * not stopped, ttrace complains. |
| 1302 | * |
| 1303 | * We have this inside the #ifdef because our caller will do |
| 1304 | * this check for real. |
| 1305 | */ |
| 1306 | if (request != TT_PROC_GET_FIRST_LWP_STATE |
| 1307 | || errno != EPROTO) |
| 1308 | { |
| 1309 | if (debug_on) |
| 1310 | printf ("TT fail for %s, with pid %d, tid %d, status %d \n", |
| 1311 | get_printable_name_of_ttrace_request (request), |
| 1312 | pid, tid, tt_status); |
| 1313 | } |
| 1314 | } |
| 1315 | #endif |
| 1316 | |
| 1317 | #if 0 |
| 1318 | /* ??rehrauer: It would probably be most robust to catch and report |
| 1319 | * failed requests here. However, some clients of this interface |
| 1320 | * seem to expect to catch & deal with them, so we'd best not. |
| 1321 | */ |
| 1322 | if (errno) |
| 1323 | { |
| 1324 | strcpy (reason_for_failure, "ttrace ("); |
| 1325 | strcat (reason_for_failure, get_printable_name_of_ttrace_request (request)); |
| 1326 | strcat (reason_for_failure, ")"); |
| 1327 | printf ("ttrace error, errno = %d\n", errno); |
| 1328 | perror_with_name (reason_for_failure); |
| 1329 | } |
| 1330 | #endif |
| 1331 | |
| 1332 | return tt_status; |
| 1333 | } |
| 1334 | \f |
| 1335 | |
| 1336 | /* This function simply calls ttrace_wait with the given arguments. |
| 1337 | * It exists so that all calls to ttrace_wait are isolated. |
| 1338 | * |
| 1339 | * No "raw" calls to ttrace_wait should exist elsewhere. |
| 1340 | */ |
| 1341 | static int |
| 1342 | call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp, |
| 1343 | size_t tsp_size) |
| 1344 | { |
| 1345 | int ttw_status; |
| 1346 | thread_info *tinfo = NULL; |
| 1347 | |
| 1348 | errno = 0; |
| 1349 | ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size); |
| 1350 | |
| 1351 | if (errno) |
| 1352 | { |
| 1353 | #ifdef THREAD_DEBUG |
| 1354 | if (debug_on) |
| 1355 | printf ("TW fail with pid %d, tid %d \n", pid, tid); |
| 1356 | #endif |
| 1357 | |
| 1358 | perror_with_name ("ttrace wait"); |
| 1359 | } |
| 1360 | |
| 1361 | return ttw_status; |
| 1362 | } |
| 1363 | \f |
| 1364 | |
| 1365 | /* A process may have one or more kernel threads, of which all or |
| 1366 | none may be stopped. This function returns the ID of the first |
| 1367 | kernel thread in a stopped state, or 0 if none are stopped. |
| 1368 | |
| 1369 | This function can be used with get_process_next_stopped_thread_id |
| 1370 | to iterate over the IDs of all stopped threads of this process. |
| 1371 | */ |
| 1372 | static lwpid_t |
| 1373 | get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state) |
| 1374 | { |
| 1375 | int tt_status; |
| 1376 | |
| 1377 | tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE, |
| 1378 | (pid_t) pid, |
| 1379 | (lwpid_t) TT_NIL, |
| 1380 | (TTRACE_ARG_TYPE) thread_state, |
| 1381 | (TTRACE_ARG_TYPE) sizeof (*thread_state), |
| 1382 | TT_NIL); |
| 1383 | |
| 1384 | if (errno) |
| 1385 | { |
| 1386 | if (errno == EPROTO) |
| 1387 | { |
| 1388 | /* This is an error we can handle: there isn't any stopped |
| 1389 | * thread. This happens when we're re-starting the application |
| 1390 | * and it has only one thread. GET_NEXT handles the case of |
| 1391 | * no more stopped threads well; GET_FIRST doesn't. (A ttrace |
| 1392 | * "feature".) |
| 1393 | */ |
| 1394 | tt_status = 1; |
| 1395 | errno = 0; |
| 1396 | return 0; |
| 1397 | } |
| 1398 | else |
| 1399 | perror_with_name ("ttrace"); |
| 1400 | } |
| 1401 | |
| 1402 | if (tt_status < 0) |
| 1403 | /* Failed somehow. |
| 1404 | */ |
| 1405 | return 0; |
| 1406 | |
| 1407 | return thread_state->tts_lwpid; |
| 1408 | } |
| 1409 | \f |
| 1410 | |
| 1411 | /* This function returns the ID of the "next" kernel thread in a |
| 1412 | stopped state, or 0 if there are none. "Next" refers to the |
| 1413 | thread following that of the last successful call to this |
| 1414 | function or to get_process_first_stopped_thread_id, using |
| 1415 | the value of thread_state returned by that call. |
| 1416 | |
| 1417 | This function can be used with get_process_first_stopped_thread_id |
| 1418 | to iterate over the IDs of all stopped threads of this process. |
| 1419 | */ |
| 1420 | static lwpid_t |
| 1421 | get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state) |
| 1422 | { |
| 1423 | int tt_status; |
| 1424 | |
| 1425 | tt_status = call_real_ttrace ( |
| 1426 | TT_PROC_GET_NEXT_LWP_STATE, |
| 1427 | (pid_t) pid, |
| 1428 | (lwpid_t) TT_NIL, |
| 1429 | (TTRACE_ARG_TYPE) thread_state, |
| 1430 | (TTRACE_ARG_TYPE) sizeof (*thread_state), |
| 1431 | TT_NIL); |
| 1432 | if (errno) |
| 1433 | perror_with_name ("ttrace"); |
| 1434 | |
| 1435 | if (tt_status < 0) |
| 1436 | /* Failed |
| 1437 | */ |
| 1438 | return 0; |
| 1439 | |
| 1440 | else if (tt_status == 0) |
| 1441 | { |
| 1442 | /* End of list, no next state. Don't return the |
| 1443 | * tts_lwpid, as it's a meaningless "240". |
| 1444 | * |
| 1445 | * This is an HPUX "feature". |
| 1446 | */ |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | return thread_state->tts_lwpid; |
| 1451 | } |
| 1452 | |
| 1453 | /* ??rehrauer: Eventually this function perhaps should be calling |
| 1454 | pid_to_thread_id. However, that function currently does nothing |
| 1455 | for HP-UX. Even then, I'm not clear whether that function |
| 1456 | will return a "kernel" thread ID, or a "user" thread ID. If |
| 1457 | the former, we can just call it here. If the latter, we must |
| 1458 | map from the "user" tid to a "kernel" tid. |
| 1459 | |
| 1460 | NOTE: currently not called. |
| 1461 | */ |
| 1462 | static lwpid_t |
| 1463 | get_active_tid_of_pid (int pid) |
| 1464 | { |
| 1465 | ttstate_t thread_state; |
| 1466 | |
| 1467 | return get_process_first_stopped_thread_id (pid, &thread_state); |
| 1468 | } |
| 1469 | |
| 1470 | /* This function returns 1 if tt_request is a ttrace request that |
| 1471 | * operates upon all threads of a (i.e., the entire) process. |
| 1472 | */ |
| 1473 | int |
| 1474 | is_process_ttrace_request (ttreq_t tt_request) |
| 1475 | { |
| 1476 | return IS_TTRACE_PROCREQ (tt_request); |
| 1477 | } |
| 1478 | \f |
| 1479 | |
| 1480 | /* This function translates a thread ttrace request into |
| 1481 | * the equivalent process request for a one-thread process. |
| 1482 | */ |
| 1483 | static ttreq_t |
| 1484 | make_process_version (ttreq_t request) |
| 1485 | { |
| 1486 | if (!IS_TTRACE_REQ (request)) |
| 1487 | { |
| 1488 | error ("Internal error, bad ttrace request made\n"); |
| 1489 | return -1; |
| 1490 | } |
| 1491 | |
| 1492 | switch (request) |
| 1493 | { |
| 1494 | case TT_LWP_STOP: |
| 1495 | return TT_PROC_STOP; |
| 1496 | |
| 1497 | case TT_LWP_CONTINUE: |
| 1498 | return TT_PROC_CONTINUE; |
| 1499 | |
| 1500 | case TT_LWP_GET_EVENT_MASK: |
| 1501 | return TT_PROC_GET_EVENT_MASK; |
| 1502 | |
| 1503 | case TT_LWP_SET_EVENT_MASK: |
| 1504 | return TT_PROC_SET_EVENT_MASK; |
| 1505 | |
| 1506 | case TT_LWP_SINGLE: |
| 1507 | case TT_LWP_RUREGS: |
| 1508 | case TT_LWP_WUREGS: |
| 1509 | case TT_LWP_GET_STATE: |
| 1510 | return -1; /* No equivalent */ |
| 1511 | |
| 1512 | default: |
| 1513 | return request; |
| 1514 | } |
| 1515 | } |
| 1516 | \f |
| 1517 | |
| 1518 | /* This function translates the "pid" used by the rest of |
| 1519 | * gdb to a real pid and a tid. It then calls "call_real_ttrace" |
| 1520 | * with the given arguments. |
| 1521 | * |
| 1522 | * In general, other parts of this module should call this |
| 1523 | * function when they are dealing with external users, who only |
| 1524 | * have tids to pass (but they call it "pid" for historical |
| 1525 | * reasons). |
| 1526 | */ |
| 1527 | static int |
| 1528 | call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr, |
| 1529 | TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2) |
| 1530 | { |
| 1531 | lwpid_t real_tid; |
| 1532 | int real_pid; |
| 1533 | ttreq_t new_request; |
| 1534 | int tt_status; |
| 1535 | char reason_for_failure[100]; /* Arbitrary size, should be big enough. */ |
| 1536 | |
| 1537 | #ifdef THREAD_DEBUG |
| 1538 | int is_interesting = 0; |
| 1539 | |
| 1540 | if (TT_LWP_RUREGS == request) |
| 1541 | { |
| 1542 | is_interesting = 1; /* Adjust code here as desired */ |
| 1543 | } |
| 1544 | |
| 1545 | if (is_interesting && 0 && debug_on) |
| 1546 | { |
| 1547 | if (!is_process_ttrace_request (request)) |
| 1548 | { |
| 1549 | printf ("TT: Thread request, tid is %d", gdb_tid); |
| 1550 | printf ("== SINGLE at %x", addr); |
| 1551 | } |
| 1552 | else |
| 1553 | { |
| 1554 | printf ("TT: Process request, tid is %d\n", gdb_tid); |
| 1555 | printf ("==! SINGLE at %x", addr); |
| 1556 | } |
| 1557 | } |
| 1558 | #endif |
| 1559 | |
| 1560 | /* The initial SETTRC and SET_EVENT_MASK calls (and all others |
| 1561 | * which happen before any threads get set up) should go |
| 1562 | * directly to "call_real_ttrace", so they don't happen here. |
| 1563 | * |
| 1564 | * But hardware watchpoints do a SET_EVENT_MASK, so we can't |
| 1565 | * rule them out.... |
| 1566 | */ |
| 1567 | #ifdef THREAD_DEBUG |
| 1568 | if (request == TT_PROC_SETTRC && debug_on) |
| 1569 | printf ("Unexpected call for TT_PROC_SETTRC\n"); |
| 1570 | #endif |
| 1571 | |
| 1572 | /* Sometimes we get called with a bogus tid (e.g., if a |
| 1573 | * thread has terminated, we return 0; inftarg later asks |
| 1574 | * whether the thread has exited/forked/vforked). |
| 1575 | */ |
| 1576 | if (gdb_tid == 0) |
| 1577 | { |
| 1578 | errno = ESRCH; /* ttrace's response would probably be "No such process". */ |
| 1579 | return -1; |
| 1580 | } |
| 1581 | |
| 1582 | /* All other cases should be able to expect that there are |
| 1583 | * thread records. |
| 1584 | */ |
| 1585 | if (!any_thread_records ()) |
| 1586 | { |
| 1587 | #ifdef THREAD_DEBUG |
| 1588 | if (debug_on) |
| 1589 | warning ("No thread records for ttrace call"); |
| 1590 | #endif |
| 1591 | errno = ESRCH; /* ttrace's response would be "No such process". */ |
| 1592 | return -1; |
| 1593 | } |
| 1594 | |
| 1595 | /* OK, now the task is to translate the incoming tid into |
| 1596 | * a pid/tid pair. |
| 1597 | */ |
| 1598 | real_tid = map_from_gdb_tid (gdb_tid); |
| 1599 | real_pid = get_pid_for (real_tid); |
| 1600 | |
| 1601 | /* Now check the result. "Real_pid" is NULL if our list |
| 1602 | * didn't find it. We have some tricks we can play to fix |
| 1603 | * this, however. |
| 1604 | */ |
| 1605 | if (0 == real_pid) |
| 1606 | { |
| 1607 | ttstate_t thread_state; |
| 1608 | |
| 1609 | #ifdef THREAD_DEBUG |
| 1610 | if (debug_on) |
| 1611 | printf ("No saved pid for tid %d\n", gdb_tid); |
| 1612 | #endif |
| 1613 | |
| 1614 | if (is_process_ttrace_request (request)) |
| 1615 | { |
| 1616 | |
| 1617 | /* Ok, we couldn't get a tid. Try to translate to |
| 1618 | * the equivalent process operation. We expect this |
| 1619 | * NOT to happen, so this is a desparation-type |
| 1620 | * move. It can happen if there is an internal |
| 1621 | * error and so no "wait()" call is ever done. |
| 1622 | */ |
| 1623 | new_request = make_process_version (request); |
| 1624 | if (new_request == -1) |
| 1625 | { |
| 1626 | |
| 1627 | #ifdef THREAD_DEBUG |
| 1628 | if (debug_on) |
| 1629 | printf ("...and couldn't make process version of thread operation\n"); |
| 1630 | #endif |
| 1631 | |
| 1632 | /* Use hacky saved pid, which won't always be correct |
| 1633 | * in the multi-process future. Use tid as thread, |
| 1634 | * probably dooming this to failure. FIX! |
| 1635 | */ |
| 1636 | if (! ptid_equal (saved_real_ptid, null_ptid)) |
| 1637 | { |
| 1638 | #ifdef THREAD_DEBUG |
| 1639 | if (debug_on) |
| 1640 | printf ("...using saved pid %d\n", |
| 1641 | PIDGET (saved_real_ptid)); |
| 1642 | #endif |
| 1643 | |
| 1644 | real_pid = PIDGET (saved_real_ptid); |
| 1645 | real_tid = gdb_tid; |
| 1646 | } |
| 1647 | |
| 1648 | else |
| 1649 | error ("Unable to perform thread operation"); |
| 1650 | } |
| 1651 | |
| 1652 | else |
| 1653 | { |
| 1654 | /* Sucessfully translated this to a process request, |
| 1655 | * which needs no thread value. |
| 1656 | */ |
| 1657 | real_pid = gdb_tid; |
| 1658 | real_tid = 0; |
| 1659 | request = new_request; |
| 1660 | |
| 1661 | #ifdef THREAD_DEBUG |
| 1662 | if (debug_on) |
| 1663 | { |
| 1664 | printf ("Translated thread request to process request\n"); |
| 1665 | if (ptid_equal (saved_real_ptid, null_ptid)) |
| 1666 | printf ("...but there's no saved pid\n"); |
| 1667 | |
| 1668 | else |
| 1669 | { |
| 1670 | if (gdb_tid != PIDGET (saved_real_ptid)) |
| 1671 | printf ("...but have the wrong pid (%d rather than %d)\n", |
| 1672 | gdb_tid, PIDGET (saved_real_ptid)); |
| 1673 | } |
| 1674 | } |
| 1675 | #endif |
| 1676 | } /* Translated to a process request */ |
| 1677 | } /* Is a process request */ |
| 1678 | |
| 1679 | else |
| 1680 | { |
| 1681 | /* We have to have a thread. Ooops. |
| 1682 | */ |
| 1683 | error ("Thread request with no threads (%s)", |
| 1684 | get_printable_name_of_ttrace_request (request)); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* Ttrace doesn't like to see tid values on process requests, |
| 1689 | * even if we have the right one. |
| 1690 | */ |
| 1691 | if (is_process_ttrace_request (request)) |
| 1692 | { |
| 1693 | real_tid = 0; |
| 1694 | } |
| 1695 | |
| 1696 | #ifdef THREAD_DEBUG |
| 1697 | if (is_interesting && 0 && debug_on) |
| 1698 | { |
| 1699 | printf (" now tid %d, pid %d\n", real_tid, real_pid); |
| 1700 | printf (" request is %s\n", get_printable_name_of_ttrace_request (request)); |
| 1701 | } |
| 1702 | #endif |
| 1703 | |
| 1704 | /* Finally, the (almost) real call. |
| 1705 | */ |
| 1706 | tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2); |
| 1707 | |
| 1708 | #ifdef THREAD_DEBUG |
| 1709 | if (is_interesting && debug_on) |
| 1710 | { |
| 1711 | if (!TT_OK (tt_status, errno) |
| 1712 | && !(tt_status == 0 & errno == 0)) |
| 1713 | printf (" got error (errno==%d, status==%d)\n", errno, tt_status); |
| 1714 | } |
| 1715 | #endif |
| 1716 | |
| 1717 | return tt_status; |
| 1718 | } |
| 1719 | |
| 1720 | |
| 1721 | /* Stop all the threads of a process. |
| 1722 | |
| 1723 | * NOTE: use of TT_PROC_STOP can cause a thread with a real event |
| 1724 | * to get a TTEVT_NONE event, discarding the old event. Be |
| 1725 | * very careful, and only call TT_PROC_STOP when you mean it! |
| 1726 | */ |
| 1727 | static void |
| 1728 | stop_all_threads_of_process (pid_t real_pid) |
| 1729 | { |
| 1730 | int ttw_status; |
| 1731 | |
| 1732 | ttw_status = call_real_ttrace (TT_PROC_STOP, |
| 1733 | (pid_t) real_pid, |
| 1734 | (lwpid_t) TT_NIL, |
| 1735 | (TTRACE_ARG_TYPE) TT_NIL, |
| 1736 | (TTRACE_ARG_TYPE) TT_NIL, |
| 1737 | TT_NIL); |
| 1738 | if (errno) |
| 1739 | perror_with_name ("ttrace stop of other threads"); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | /* Under some circumstances, it's unsafe to attempt to stop, or even |
| 1744 | query the state of, a process' threads. |
| 1745 | |
| 1746 | In ttrace-based HP-UX, an example is a vforking child process. The |
| 1747 | vforking parent and child are somewhat fragile, w/r/t what we can do |
| 1748 | what we can do to them with ttrace, until after the child exits or |
| 1749 | execs, or until the parent's vfork event is delivered. Until that |
| 1750 | time, we must not try to stop the process' threads, or inquire how |
| 1751 | many there are, or even alter its data segments, or it typically dies |
| 1752 | with a SIGILL. Sigh. |
| 1753 | |
| 1754 | This function returns 1 if this stopped process, and the event that |
| 1755 | we're told was responsible for its current stopped state, cannot safely |
| 1756 | have its threads examined. |
| 1757 | */ |
| 1758 | #define CHILD_VFORKED(evt,pid) \ |
| 1759 | (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid))) |
| 1760 | #define CHILD_URPED(evt,pid) \ |
| 1761 | ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid)) |
| 1762 | #define PARENT_VFORKED(evt,pid) \ |
| 1763 | (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid))) |
| 1764 | |
| 1765 | static int |
| 1766 | can_touch_threads_of_process (int pid, ttevents_t stopping_event) |
| 1767 | { |
| 1768 | if (CHILD_VFORKED (stopping_event, pid)) |
| 1769 | { |
| 1770 | vforking_child_pid = pid; |
| 1771 | vfork_in_flight = 1; |
| 1772 | } |
| 1773 | |
| 1774 | else if (vfork_in_flight && |
| 1775 | (PARENT_VFORKED (stopping_event, pid) || |
| 1776 | CHILD_URPED (stopping_event, pid))) |
| 1777 | { |
| 1778 | vfork_in_flight = 0; |
| 1779 | vforking_child_pid = 0; |
| 1780 | } |
| 1781 | |
| 1782 | return !vfork_in_flight; |
| 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | /* If we can find an as-yet-unhandled thread state of a |
| 1787 | * stopped thread of this process return 1 and set "tsp". |
| 1788 | * Return 0 if we can't. |
| 1789 | * |
| 1790 | * If this function is used when the threads of PIS haven't |
| 1791 | * been stopped, undefined behaviour is guaranteed! |
| 1792 | */ |
| 1793 | static int |
| 1794 | select_stopped_thread_of_process (int pid, ttstate_t *tsp) |
| 1795 | { |
| 1796 | lwpid_t candidate_tid, tid; |
| 1797 | ttstate_t candidate_tstate, tstate; |
| 1798 | |
| 1799 | /* If we're not allowed to touch the process now, then just |
| 1800 | * return the current value of *TSP. |
| 1801 | * |
| 1802 | * This supports "vfork". It's ok, really, to double the |
| 1803 | * current event (the child EXEC, we hope!). |
| 1804 | */ |
| 1805 | if (!can_touch_threads_of_process (pid, tsp->tts_event)) |
| 1806 | return 1; |
| 1807 | |
| 1808 | /* Decide which of (possibly more than one) events to |
| 1809 | * return as the first one. We scan them all so that |
| 1810 | * we always return the result of a fake-step first. |
| 1811 | */ |
| 1812 | candidate_tid = 0; |
| 1813 | for (tid = get_process_first_stopped_thread_id (pid, &tstate); |
| 1814 | tid != 0; |
| 1815 | tid = get_process_next_stopped_thread_id (pid, &tstate)) |
| 1816 | { |
| 1817 | /* TTEVT_NONE events are uninteresting to our clients. They're |
| 1818 | * an artifact of our "stop the world" model--the thread is |
| 1819 | * stopped because we stopped it. |
| 1820 | */ |
| 1821 | if (tstate.tts_event == TTEVT_NONE) |
| 1822 | { |
| 1823 | set_handled (pid, tstate.tts_lwpid); |
| 1824 | } |
| 1825 | |
| 1826 | /* Did we just single-step a single thread, without letting any |
| 1827 | * of the others run? Is this an event for that thread? |
| 1828 | * |
| 1829 | * If so, we believe our client would prefer to see this event |
| 1830 | * over any others. (Typically the client wants to just push |
| 1831 | * one thread a little farther forward, and then go around |
| 1832 | * checking for what all threads are doing.) |
| 1833 | */ |
| 1834 | else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid)) |
| 1835 | { |
| 1836 | #ifdef WAIT_BUFFER_DEBUG |
| 1837 | /* It's possible here to see either a SIGTRAP (due to |
| 1838 | * successful completion of a step) or a SYSCALL_ENTRY |
| 1839 | * (due to a step completion with active hardware |
| 1840 | * watchpoints). |
| 1841 | */ |
| 1842 | if (debug_on) |
| 1843 | printf ("Ending fake step with tid %d, state %s\n", |
| 1844 | tstate.tts_lwpid, |
| 1845 | get_printable_name_of_ttrace_event (tstate.tts_event)); |
| 1846 | #endif |
| 1847 | |
| 1848 | /* Remember this one, and throw away any previous |
| 1849 | * candidate. |
| 1850 | */ |
| 1851 | candidate_tid = tstate.tts_lwpid; |
| 1852 | candidate_tstate = tstate; |
| 1853 | } |
| 1854 | |
| 1855 | #ifdef FORGET_DELETED_BPTS |
| 1856 | |
| 1857 | /* We can't just do this, as if we do, and then wind |
| 1858 | * up the loop with no unhandled events, we need to |
| 1859 | * handle that case--the appropriate reaction is to |
| 1860 | * just continue, but there's no easy way to do that. |
| 1861 | * |
| 1862 | * Better to put this in the ttrace_wait call--if, when |
| 1863 | * we fake a wait, we update our events based on the |
| 1864 | * breakpoint_here_pc call and find there are no more events, |
| 1865 | * then we better continue and so on. |
| 1866 | * |
| 1867 | * Or we could put it in the next/continue fake. |
| 1868 | * But it has to go in the buffering code, not in the |
| 1869 | * real go/wait code. |
| 1870 | */ |
| 1871 | else if ((TTEVT_SIGNAL == tstate.tts_event) |
| 1872 | && (5 == tstate.tts_u.tts_signal.tts_signo) |
| 1873 | && (0 != get_raw_pc (tstate.tts_lwpid)) |
| 1874 | && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid))) |
| 1875 | { |
| 1876 | /* |
| 1877 | * If the user deleted a breakpoint while this |
| 1878 | * breakpoint-hit event was buffered, we can forget |
| 1879 | * it now. |
| 1880 | */ |
| 1881 | #ifdef WAIT_BUFFER_DEBUG |
| 1882 | if (debug_on) |
| 1883 | printf ("Forgetting deleted bp hit for thread %d\n", |
| 1884 | tstate.tts_lwpid); |
| 1885 | #endif |
| 1886 | |
| 1887 | set_handled (pid, tstate.tts_lwpid); |
| 1888 | } |
| 1889 | #endif |
| 1890 | |
| 1891 | /* Else, is this the first "unhandled" event? If so, |
| 1892 | * we believe our client wants to see it (if we don't |
| 1893 | * see a fake-step later on in the scan). |
| 1894 | */ |
| 1895 | else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0) |
| 1896 | { |
| 1897 | candidate_tid = tstate.tts_lwpid; |
| 1898 | candidate_tstate = tstate; |
| 1899 | } |
| 1900 | |
| 1901 | /* This is either an event that has already been "handled", |
| 1902 | * and thus we believe is uninteresting to our client, or we |
| 1903 | * already have a candidate event. Ignore it... |
| 1904 | */ |
| 1905 | } |
| 1906 | |
| 1907 | /* What do we report? |
| 1908 | */ |
| 1909 | if (doing_fake_step) |
| 1910 | { |
| 1911 | if (candidate_tid == fake_step_tid) |
| 1912 | { |
| 1913 | /* Fake step. |
| 1914 | */ |
| 1915 | tstate = candidate_tstate; |
| 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | warning ("Internal error: fake-step failed to complete."); |
| 1920 | return 0; |
| 1921 | } |
| 1922 | } |
| 1923 | else if (candidate_tid != 0) |
| 1924 | { |
| 1925 | /* Found a candidate unhandled event. |
| 1926 | */ |
| 1927 | tstate = candidate_tstate; |
| 1928 | } |
| 1929 | else if (tid != 0) |
| 1930 | { |
| 1931 | warning ("Internal error in call of ttrace_wait."); |
| 1932 | return 0; |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | warning ("Internal error: no unhandled thread event to select"); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | copy_ttstate_t (tsp, &tstate); |
| 1941 | return 1; |
| 1942 | } /* End of select_stopped_thread_of_process */ |
| 1943 | |
| 1944 | #ifdef PARANOIA |
| 1945 | /* Check our internal thread data against the real thing. |
| 1946 | */ |
| 1947 | static void |
| 1948 | check_thread_consistency (pid_t real_pid) |
| 1949 | { |
| 1950 | int tid; /* really lwpid_t */ |
| 1951 | ttstate_t tstate; |
| 1952 | thread_info *p; |
| 1953 | |
| 1954 | /* Spin down the O/S list of threads, checking that they |
| 1955 | * match what we've got. |
| 1956 | */ |
| 1957 | for (tid = get_process_first_stopped_thread_id (real_pid, &tstate); |
| 1958 | tid != 0; |
| 1959 | tid = get_process_next_stopped_thread_id (real_pid, &tstate)) |
| 1960 | { |
| 1961 | |
| 1962 | p = find_thread_info (tid); |
| 1963 | |
| 1964 | if (NULL == p) |
| 1965 | { |
| 1966 | warning ("No internal thread data for thread %d.", tid); |
| 1967 | continue; |
| 1968 | } |
| 1969 | |
| 1970 | if (!p->seen) |
| 1971 | { |
| 1972 | warning ("Inconsistent internal thread data for thread %d.", tid); |
| 1973 | } |
| 1974 | |
| 1975 | if (p->terminated) |
| 1976 | { |
| 1977 | warning ("Thread %d is not terminated, internal error.", tid); |
| 1978 | continue; |
| 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | #define TT_COMPARE( fld ) \ |
| 1983 | tstate.fld != p->last_stop_state.fld |
| 1984 | |
| 1985 | if (p->have_state) |
| 1986 | { |
| 1987 | if (TT_COMPARE (tts_pid) |
| 1988 | || TT_COMPARE (tts_lwpid) |
| 1989 | || TT_COMPARE (tts_user_tid) |
| 1990 | || TT_COMPARE (tts_event) |
| 1991 | || TT_COMPARE (tts_flags) |
| 1992 | || TT_COMPARE (tts_scno) |
| 1993 | || TT_COMPARE (tts_scnargs)) |
| 1994 | { |
| 1995 | warning ("Internal thread data for thread %d is wrong.", tid); |
| 1996 | continue; |
| 1997 | } |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | #endif /* PARANOIA */ |
| 2002 | \f |
| 2003 | |
| 2004 | /* This function wraps calls to "call_real_ttrace_wait" so |
| 2005 | * that a actual wait is only done when all pending events |
| 2006 | * have been reported. |
| 2007 | * |
| 2008 | * Note that typically it is called with a pid of "0", i.e. |
| 2009 | * the "don't care" value. |
| 2010 | * |
| 2011 | * Return value is the status of the pseudo wait. |
| 2012 | */ |
| 2013 | static int |
| 2014 | call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size) |
| 2015 | { |
| 2016 | /* This holds the actual, for-real, true process ID. |
| 2017 | */ |
| 2018 | static int real_pid; |
| 2019 | |
| 2020 | /* As an argument to ttrace_wait, zero pid |
| 2021 | * means "Any process", and zero tid means |
| 2022 | * "Any thread of the specified process". |
| 2023 | */ |
| 2024 | int wait_pid = 0; |
| 2025 | lwpid_t wait_tid = 0; |
| 2026 | lwpid_t real_tid; |
| 2027 | |
| 2028 | int ttw_status = 0; /* To be returned */ |
| 2029 | |
| 2030 | thread_info *tinfo = NULL; |
| 2031 | |
| 2032 | if (pid != 0) |
| 2033 | { |
| 2034 | /* Unexpected case. |
| 2035 | */ |
| 2036 | #ifdef THREAD_DEBUG |
| 2037 | if (debug_on) |
| 2038 | printf ("TW: Pid to wait on is %d\n", pid); |
| 2039 | #endif |
| 2040 | |
| 2041 | if (!any_thread_records ()) |
| 2042 | error ("No thread records for ttrace call w. specific pid"); |
| 2043 | |
| 2044 | /* OK, now the task is to translate the incoming tid into |
| 2045 | * a pid/tid pair. |
| 2046 | */ |
| 2047 | real_tid = map_from_gdb_tid (pid); |
| 2048 | real_pid = get_pid_for (real_tid); |
| 2049 | #ifdef THREAD_DEBUG |
| 2050 | if (debug_on) |
| 2051 | printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid); |
| 2052 | #endif |
| 2053 | } |
| 2054 | |
| 2055 | |
| 2056 | /* Sanity checks and set-up. |
| 2057 | * Process State |
| 2058 | * |
| 2059 | * Stopped Running Fake-step (v)Fork |
| 2060 | * \________________________________________ |
| 2061 | * | |
| 2062 | * No buffered events | error wait wait wait |
| 2063 | * | |
| 2064 | * Buffered events | debuffer error wait debuffer (?) |
| 2065 | * |
| 2066 | */ |
| 2067 | if (more_events_left == 0) |
| 2068 | { |
| 2069 | |
| 2070 | if (process_state == RUNNING) |
| 2071 | { |
| 2072 | /* OK--normal call of ttrace_wait with no buffered events. |
| 2073 | */ |
| 2074 | ; |
| 2075 | } |
| 2076 | else if (process_state == FAKE_STEPPING) |
| 2077 | { |
| 2078 | /* Ok--call of ttrace_wait to support |
| 2079 | * fake stepping with no buffered events. |
| 2080 | * |
| 2081 | * But we better be fake-stepping! |
| 2082 | */ |
| 2083 | if (!doing_fake_step) |
| 2084 | { |
| 2085 | warning ("Inconsistent thread state."); |
| 2086 | } |
| 2087 | } |
| 2088 | else if ((process_state == FORKING) |
| 2089 | || (process_state == VFORKING)) |
| 2090 | { |
| 2091 | /* Ok--there are two processes, so waiting |
| 2092 | * for the second while the first is stopped |
| 2093 | * is ok. Handled bits stay as they were. |
| 2094 | */ |
| 2095 | ; |
| 2096 | } |
| 2097 | else if (process_state == STOPPED) |
| 2098 | { |
| 2099 | warning ("Process not running at wait call."); |
| 2100 | } |
| 2101 | else |
| 2102 | /* No known state. |
| 2103 | */ |
| 2104 | warning ("Inconsistent process state."); |
| 2105 | } |
| 2106 | |
| 2107 | else |
| 2108 | { |
| 2109 | /* More events left |
| 2110 | */ |
| 2111 | if (process_state == STOPPED) |
| 2112 | { |
| 2113 | /* OK--buffered events being unbuffered. |
| 2114 | */ |
| 2115 | ; |
| 2116 | } |
| 2117 | else if (process_state == RUNNING) |
| 2118 | { |
| 2119 | /* An error--shouldn't have buffered events |
| 2120 | * when running. |
| 2121 | */ |
| 2122 | warning ("Trying to continue with buffered events:"); |
| 2123 | } |
| 2124 | else if (process_state == FAKE_STEPPING) |
| 2125 | { |
| 2126 | /* |
| 2127 | * Better be fake-stepping! |
| 2128 | */ |
| 2129 | if (!doing_fake_step) |
| 2130 | { |
| 2131 | warning ("Losing buffered thread events!\n"); |
| 2132 | } |
| 2133 | } |
| 2134 | else if ((process_state == FORKING) |
| 2135 | || (process_state == VFORKING)) |
| 2136 | { |
| 2137 | /* Ok--there are two processes, so waiting |
| 2138 | * for the second while the first is stopped |
| 2139 | * is ok. Handled bits stay as they were. |
| 2140 | */ |
| 2141 | ; |
| 2142 | } |
| 2143 | else |
| 2144 | warning ("Process in unknown state with buffered events."); |
| 2145 | } |
| 2146 | |
| 2147 | /* Sometimes we have to wait for a particular thread |
| 2148 | * (if we're stepping over a bpt). In that case, we |
| 2149 | * _know_ it's going to complete the single-step we |
| 2150 | * asked for (because we're only doing the step under |
| 2151 | * certain very well-understood circumstances), so it |
| 2152 | * can't block. |
| 2153 | */ |
| 2154 | if (doing_fake_step) |
| 2155 | { |
| 2156 | wait_tid = fake_step_tid; |
| 2157 | wait_pid = get_pid_for (fake_step_tid); |
| 2158 | |
| 2159 | #ifdef WAIT_BUFFER_DEBUG |
| 2160 | if (debug_on) |
| 2161 | printf ("Doing a wait after a fake-step for %d, pid %d\n", |
| 2162 | wait_tid, wait_pid); |
| 2163 | #endif |
| 2164 | } |
| 2165 | |
| 2166 | if (more_events_left == 0 /* No buffered events, need real ones. */ |
| 2167 | || process_state != STOPPED) |
| 2168 | { |
| 2169 | /* If there are no buffered events, and so we need |
| 2170 | * real ones, or if we are FORKING, VFORKING, |
| 2171 | * FAKE_STEPPING or RUNNING, and thus have to do |
| 2172 | * a real wait, then do a real wait. |
| 2173 | */ |
| 2174 | |
| 2175 | #ifdef WAIT_BUFFER_DEBUG |
| 2176 | /* Normal case... */ |
| 2177 | if (debug_on) |
| 2178 | printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid); |
| 2179 | #endif |
| 2180 | |
| 2181 | /* The actual wait call. |
| 2182 | */ |
| 2183 | ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size); |
| 2184 | |
| 2185 | /* Note that the routines we'll call will be using "call_real_ttrace", |
| 2186 | * not "call_ttrace", and thus need the real pid rather than the pseudo-tid |
| 2187 | * the rest of the world uses (which is actually the tid). |
| 2188 | */ |
| 2189 | real_pid = tsp->tts_pid; |
| 2190 | |
| 2191 | /* For most events: Stop the world! |
| 2192 | |
| 2193 | * It's sometimes not safe to stop all threads of a process. |
| 2194 | * Sometimes it's not even safe to ask for the thread state |
| 2195 | * of a process! |
| 2196 | */ |
| 2197 | if (can_touch_threads_of_process (real_pid, tsp->tts_event)) |
| 2198 | { |
| 2199 | /* If we're really only stepping a single thread, then don't |
| 2200 | * try to stop all the others -- we only do this single-stepping |
| 2201 | * business when all others were already stopped...and the stop |
| 2202 | * would mess up other threads' events. |
| 2203 | * |
| 2204 | * Similiarly, if there are other threads with events, |
| 2205 | * don't do the stop. |
| 2206 | */ |
| 2207 | if (!doing_fake_step) |
| 2208 | { |
| 2209 | if (more_events_left > 0) |
| 2210 | warning ("Internal error in stopping process"); |
| 2211 | |
| 2212 | stop_all_threads_of_process (real_pid); |
| 2213 | |
| 2214 | /* At this point, we could scan and update_thread_list(), |
| 2215 | * and only use the local list for the rest of the |
| 2216 | * module! We'd get rid of the scans in the various |
| 2217 | * continue routines (adding one in attach). It'd |
| 2218 | * be great--UPGRADE ME! |
| 2219 | */ |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | #ifdef PARANOIA |
| 2224 | else if (debug_on) |
| 2225 | { |
| 2226 | if (more_events_left > 0) |
| 2227 | printf ("== Can't stop process; more events!\n"); |
| 2228 | else |
| 2229 | printf ("== Can't stop process!\n"); |
| 2230 | } |
| 2231 | #endif |
| 2232 | |
| 2233 | process_state = STOPPED; |
| 2234 | |
| 2235 | #ifdef WAIT_BUFFER_DEBUG |
| 2236 | if (debug_on) |
| 2237 | printf ("Process set to STOPPED\n"); |
| 2238 | #endif |
| 2239 | } |
| 2240 | |
| 2241 | else |
| 2242 | { |
| 2243 | /* Fake a call to ttrace_wait. The process must be |
| 2244 | * STOPPED, as we aren't going to do any wait. |
| 2245 | */ |
| 2246 | #ifdef WAIT_BUFFER_DEBUG |
| 2247 | if (debug_on) |
| 2248 | printf ("TW: fake it\n"); |
| 2249 | #endif |
| 2250 | |
| 2251 | if (process_state != STOPPED) |
| 2252 | { |
| 2253 | warning ("Process not stopped at wait call, in state '%s'.\n", |
| 2254 | get_printable_name_of_process_state (process_state)); |
| 2255 | } |
| 2256 | |
| 2257 | if (doing_fake_step) |
| 2258 | error ("Internal error in stepping over breakpoint"); |
| 2259 | |
| 2260 | ttw_status = 0; /* Faking it is always successful! */ |
| 2261 | } /* End of fake or not? if */ |
| 2262 | |
| 2263 | /* Pick an event to pass to our caller. Be paranoid. |
| 2264 | */ |
| 2265 | if (!select_stopped_thread_of_process (real_pid, tsp)) |
| 2266 | warning ("Can't find event, using previous event."); |
| 2267 | |
| 2268 | else if (tsp->tts_event == TTEVT_NONE) |
| 2269 | warning ("Internal error: no thread has a real event."); |
| 2270 | |
| 2271 | else if (doing_fake_step) |
| 2272 | { |
| 2273 | if (fake_step_tid != tsp->tts_lwpid) |
| 2274 | warning ("Internal error in stepping over breakpoint."); |
| 2275 | |
| 2276 | /* This wait clears the (current) fake-step if there was one. |
| 2277 | */ |
| 2278 | doing_fake_step = 0; |
| 2279 | fake_step_tid = 0; |
| 2280 | } |
| 2281 | |
| 2282 | /* We now have a correct tsp and ttw_status for the thread |
| 2283 | * which we want to report. So it's "handled"! This call |
| 2284 | * will add it to our list if it's not there already. |
| 2285 | */ |
| 2286 | set_handled (real_pid, tsp->tts_lwpid); |
| 2287 | |
| 2288 | /* Save a copy of the ttrace state of this thread, in our local |
| 2289 | thread descriptor. |
| 2290 | |
| 2291 | This caches the state. The implementation of queries like |
| 2292 | hpux_has_execd can then use this cached state, rather than |
| 2293 | be forced to make an explicit ttrace call to get it. |
| 2294 | |
| 2295 | (Guard against the condition that this is the first time we've |
| 2296 | waited on, i.e., seen this thread, and so haven't yet entered |
| 2297 | it into our list of threads.) |
| 2298 | */ |
| 2299 | tinfo = find_thread_info (tsp->tts_lwpid); |
| 2300 | if (tinfo != NULL) |
| 2301 | { |
| 2302 | copy_ttstate_t (&tinfo->last_stop_state, tsp); |
| 2303 | tinfo->have_state = 1; |
| 2304 | } |
| 2305 | |
| 2306 | return ttw_status; |
| 2307 | } /* call_ttrace_wait */ |
| 2308 | |
| 2309 | #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL) |
| 2310 | int |
| 2311 | child_reported_exec_events_per_exec_call (void) |
| 2312 | { |
| 2313 | return 1; /* ttrace reports the event once per call. */ |
| 2314 | } |
| 2315 | #endif |
| 2316 | \f |
| 2317 | |
| 2318 | |
| 2319 | /* Our implementation of hardware watchpoints involves making memory |
| 2320 | pages write-protected. We must remember a page's original permissions, |
| 2321 | and we must also know when it is appropriate to restore a page's |
| 2322 | permissions to its original state. |
| 2323 | |
| 2324 | We use a "dictionary" of hardware-watched pages to do this. Each |
| 2325 | hardware-watched page is recorded in the dictionary. Each page's |
| 2326 | dictionary entry contains the original permissions and a reference |
| 2327 | count. Pages are hashed into the dictionary by their start address. |
| 2328 | |
| 2329 | When hardware watchpoint is set on page X for the first time, page X |
| 2330 | is added to the dictionary with a reference count of 1. If other |
| 2331 | hardware watchpoints are subsequently set on page X, its reference |
| 2332 | count is incremented. When hardware watchpoints are removed from |
| 2333 | page X, its reference count is decremented. If a page's reference |
| 2334 | count drops to 0, it's permissions are restored and the page's entry |
| 2335 | is thrown out of the dictionary. |
| 2336 | */ |
| 2337 | typedef struct memory_page |
| 2338 | { |
| 2339 | CORE_ADDR page_start; |
| 2340 | int reference_count; |
| 2341 | int original_permissions; |
| 2342 | struct memory_page *next; |
| 2343 | struct memory_page *previous; |
| 2344 | } |
| 2345 | memory_page_t; |
| 2346 | |
| 2347 | #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128 |
| 2348 | |
| 2349 | static struct |
| 2350 | { |
| 2351 | LONGEST page_count; |
| 2352 | int page_size; |
| 2353 | int page_protections_allowed; |
| 2354 | /* These are just the heads of chains of actual page descriptors. */ |
| 2355 | memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT]; |
| 2356 | } |
| 2357 | memory_page_dictionary; |
| 2358 | |
| 2359 | |
| 2360 | static void |
| 2361 | require_memory_page_dictionary (void) |
| 2362 | { |
| 2363 | int i; |
| 2364 | |
| 2365 | /* Is the memory page dictionary ready for use? If so, we're done. */ |
| 2366 | if (memory_page_dictionary.page_count >= (LONGEST) 0) |
| 2367 | return; |
| 2368 | |
| 2369 | /* Else, initialize it. */ |
| 2370 | memory_page_dictionary.page_count = (LONGEST) 0; |
| 2371 | |
| 2372 | for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++) |
| 2373 | { |
| 2374 | memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0; |
| 2375 | memory_page_dictionary.buckets[i].reference_count = 0; |
| 2376 | memory_page_dictionary.buckets[i].next = NULL; |
| 2377 | memory_page_dictionary.buckets[i].previous = NULL; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | static void |
| 2383 | retire_memory_page_dictionary (void) |
| 2384 | { |
| 2385 | memory_page_dictionary.page_count = (LONGEST) - 1; |
| 2386 | } |
| 2387 | |
| 2388 | |
| 2389 | /* Write-protect the memory page that starts at this address. |
| 2390 | |
| 2391 | Returns the original permissions of the page. |
| 2392 | */ |
| 2393 | static int |
| 2394 | write_protect_page (int pid, CORE_ADDR page_start) |
| 2395 | { |
| 2396 | int tt_status; |
| 2397 | int original_permissions; |
| 2398 | int new_permissions; |
| 2399 | |
| 2400 | tt_status = call_ttrace (TT_PROC_GET_MPROTECT, |
| 2401 | pid, |
| 2402 | (TTRACE_ARG_TYPE) page_start, |
| 2403 | TT_NIL, |
| 2404 | (TTRACE_ARG_TYPE) & original_permissions); |
| 2405 | if (errno || (tt_status < 0)) |
| 2406 | { |
| 2407 | return 0; /* What else can we do? */ |
| 2408 | } |
| 2409 | |
| 2410 | /* We'll also write-protect the page now, if that's allowed. */ |
| 2411 | if (memory_page_dictionary.page_protections_allowed) |
| 2412 | { |
| 2413 | new_permissions = original_permissions & ~PROT_WRITE; |
| 2414 | tt_status = call_ttrace (TT_PROC_SET_MPROTECT, |
| 2415 | pid, |
| 2416 | (TTRACE_ARG_TYPE) page_start, |
| 2417 | (TTRACE_ARG_TYPE) memory_page_dictionary.page_size, |
| 2418 | (TTRACE_ARG_TYPE) new_permissions); |
| 2419 | if (errno || (tt_status < 0)) |
| 2420 | { |
| 2421 | return 0; /* What else can we do? */ |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | return original_permissions; |
| 2426 | } |
| 2427 | |
| 2428 | |
| 2429 | /* Unwrite-protect the memory page that starts at this address, restoring |
| 2430 | (what we must assume are) its original permissions. |
| 2431 | */ |
| 2432 | static void |
| 2433 | unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions) |
| 2434 | { |
| 2435 | int tt_status; |
| 2436 | |
| 2437 | tt_status = call_ttrace (TT_PROC_SET_MPROTECT, |
| 2438 | pid, |
| 2439 | (TTRACE_ARG_TYPE) page_start, |
| 2440 | (TTRACE_ARG_TYPE) memory_page_dictionary.page_size, |
| 2441 | (TTRACE_ARG_TYPE) original_permissions); |
| 2442 | if (errno || (tt_status < 0)) |
| 2443 | { |
| 2444 | return; /* What else can we do? */ |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | |
| 2449 | /* Memory page-protections are used to implement "hardware" watchpoints |
| 2450 | on HP-UX. |
| 2451 | |
| 2452 | For every memory page that is currently being watched (i.e., that |
| 2453 | presently should be write-protected), write-protect it. |
| 2454 | */ |
| 2455 | void |
| 2456 | hppa_enable_page_protection_events (int pid) |
| 2457 | { |
| 2458 | int bucket; |
| 2459 | |
| 2460 | memory_page_dictionary.page_protections_allowed = 1; |
| 2461 | |
| 2462 | for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++) |
| 2463 | { |
| 2464 | memory_page_t *page; |
| 2465 | |
| 2466 | page = memory_page_dictionary.buckets[bucket].next; |
| 2467 | while (page != NULL) |
| 2468 | { |
| 2469 | page->original_permissions = write_protect_page (pid, page->page_start); |
| 2470 | page = page->next; |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | /* Memory page-protections are used to implement "hardware" watchpoints |
| 2477 | on HP-UX. |
| 2478 | |
| 2479 | For every memory page that is currently being watched (i.e., that |
| 2480 | presently is or should be write-protected), un-write-protect it. |
| 2481 | */ |
| 2482 | void |
| 2483 | hppa_disable_page_protection_events (int pid) |
| 2484 | { |
| 2485 | int bucket; |
| 2486 | |
| 2487 | for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++) |
| 2488 | { |
| 2489 | memory_page_t *page; |
| 2490 | |
| 2491 | page = memory_page_dictionary.buckets[bucket].next; |
| 2492 | while (page != NULL) |
| 2493 | { |
| 2494 | unwrite_protect_page (pid, page->page_start, page->original_permissions); |
| 2495 | page = page->next; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | memory_page_dictionary.page_protections_allowed = 0; |
| 2500 | } |
| 2501 | |
| 2502 | /* Count the number of outstanding events. At this |
| 2503 | * point, we have selected one thread and its event |
| 2504 | * as the one to be "reported" upwards to core gdb. |
| 2505 | * That thread is already marked as "handled". |
| 2506 | * |
| 2507 | * Note: we could just scan our own thread list. FIXME! |
| 2508 | */ |
| 2509 | static int |
| 2510 | count_unhandled_events (int real_pid, lwpid_t real_tid) |
| 2511 | { |
| 2512 | ttstate_t tstate; |
| 2513 | lwpid_t ttid; |
| 2514 | int events_left; |
| 2515 | |
| 2516 | /* Ok, find out how many threads have real events to report. |
| 2517 | */ |
| 2518 | events_left = 0; |
| 2519 | ttid = get_process_first_stopped_thread_id (real_pid, &tstate); |
| 2520 | |
| 2521 | #ifdef THREAD_DEBUG |
| 2522 | if (debug_on) |
| 2523 | { |
| 2524 | if (ttid == 0) |
| 2525 | printf ("Process %d has no threads\n", real_pid); |
| 2526 | else |
| 2527 | printf ("Process %d has these threads:\n", real_pid); |
| 2528 | } |
| 2529 | #endif |
| 2530 | |
| 2531 | while (ttid > 0) |
| 2532 | { |
| 2533 | if (tstate.tts_event != TTEVT_NONE |
| 2534 | && !was_handled (ttid)) |
| 2535 | { |
| 2536 | /* TTEVT_NONE implies we just stopped it ourselves |
| 2537 | * because we're the stop-the-world guys, so it's |
| 2538 | * not an event from our point of view. |
| 2539 | * |
| 2540 | * If "was_handled" is true, this is an event we |
| 2541 | * already handled, so don't count it. |
| 2542 | * |
| 2543 | * Note that we don't count the thread with the |
| 2544 | * currently-reported event, as it's already marked |
| 2545 | * as handled. |
| 2546 | */ |
| 2547 | events_left++; |
| 2548 | } |
| 2549 | |
| 2550 | #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG ) |
| 2551 | if (debug_on) |
| 2552 | { |
| 2553 | if (ttid == real_tid) |
| 2554 | printf ("*"); /* Thread we're reporting */ |
| 2555 | else |
| 2556 | printf (" "); |
| 2557 | |
| 2558 | if (tstate.tts_event != TTEVT_NONE) |
| 2559 | printf ("+"); /* Thread with a real event */ |
| 2560 | else |
| 2561 | printf (" "); |
| 2562 | |
| 2563 | if (was_handled (ttid)) |
| 2564 | printf ("h"); /* Thread has been handled */ |
| 2565 | else |
| 2566 | printf (" "); |
| 2567 | |
| 2568 | printf (" %d, with event %s", ttid, |
| 2569 | get_printable_name_of_ttrace_event (tstate.tts_event)); |
| 2570 | |
| 2571 | if (tstate.tts_event == TTEVT_SIGNAL |
| 2572 | && 5 == tstate.tts_u.tts_signal.tts_signo) |
| 2573 | { |
| 2574 | CORE_ADDR pc_val; |
| 2575 | |
| 2576 | pc_val = get_raw_pc (ttid); |
| 2577 | |
| 2578 | if (pc_val > 0) |
| 2579 | printf (" breakpoint at 0x%x\n", pc_val); |
| 2580 | else |
| 2581 | printf (" bpt, can't fetch pc.\n"); |
| 2582 | } |
| 2583 | else |
| 2584 | printf ("\n"); |
| 2585 | } |
| 2586 | #endif |
| 2587 | |
| 2588 | ttid = get_process_next_stopped_thread_id (real_pid, &tstate); |
| 2589 | } |
| 2590 | |
| 2591 | #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG ) |
| 2592 | if (debug_on) |
| 2593 | if (events_left > 0) |
| 2594 | printf ("There are thus %d pending events\n", events_left); |
| 2595 | #endif |
| 2596 | |
| 2597 | return events_left; |
| 2598 | } |
| 2599 | |
| 2600 | /* This function is provided as a sop to clients that are calling |
| 2601 | * ptrace_wait to wait for a process to stop. (see the |
| 2602 | * implementation of child_wait.) Return value is the pid for |
| 2603 | * the event that ended the wait. |
| 2604 | * |
| 2605 | * Note: used by core gdb and so uses the pseudo-pid (really tid). |
| 2606 | */ |
| 2607 | int |
| 2608 | ptrace_wait (ptid_t ptid, int *status) |
| 2609 | { |
| 2610 | ttstate_t tsp; |
| 2611 | int ttwait_return; |
| 2612 | int real_pid; |
| 2613 | ttstate_t state; |
| 2614 | lwpid_t real_tid; |
| 2615 | int return_pid; |
| 2616 | |
| 2617 | /* The ptrace implementation of this also ignores pid. |
| 2618 | */ |
| 2619 | *status = 0; |
| 2620 | |
| 2621 | ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp)); |
| 2622 | if (ttwait_return < 0) |
| 2623 | { |
| 2624 | /* ??rehrauer: It appears that if our inferior exits and we |
| 2625 | haven't asked for exit events, that we're not getting any |
| 2626 | indication save a negative return from ttrace_wait and an |
| 2627 | errno set to ESRCH? |
| 2628 | */ |
| 2629 | if (errno == ESRCH) |
| 2630 | { |
| 2631 | *status = 0; /* WIFEXITED */ |
| 2632 | return PIDGET (inferior_ptid); |
| 2633 | } |
| 2634 | |
| 2635 | warning ("Call of ttrace_wait returned with errno %d.", |
| 2636 | errno); |
| 2637 | *status = ttwait_return; |
| 2638 | return PIDGET (inferior_ptid); |
| 2639 | } |
| 2640 | |
| 2641 | real_pid = tsp.tts_pid; |
| 2642 | real_tid = tsp.tts_lwpid; |
| 2643 | |
| 2644 | /* One complication is that the "tts_event" structure has |
| 2645 | * a set of flags, and more than one can be set. So we |
| 2646 | * either have to force an order (as we do here), or handle |
| 2647 | * more than one flag at a time. |
| 2648 | */ |
| 2649 | if (tsp.tts_event & TTEVT_LWP_CREATE) |
| 2650 | { |
| 2651 | |
| 2652 | /* Unlike what you might expect, this event is reported in |
| 2653 | * the _creating_ thread, and the _created_ thread (whose tid |
| 2654 | * we have) is still running. So we have to stop it. This |
| 2655 | * has already been done in "call_ttrace_wait", but should we |
| 2656 | * ever abandon the "stop-the-world" model, here's the command |
| 2657 | * to use: |
| 2658 | * |
| 2659 | * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL ); |
| 2660 | * |
| 2661 | * Note that this would depend on being called _after_ "add_tthread" |
| 2662 | * below for the tid-to-pid translation to be done in "call_ttrace". |
| 2663 | */ |
| 2664 | |
| 2665 | #ifdef THREAD_DEBUG |
| 2666 | if (debug_on) |
| 2667 | printf ("New thread: pid %d, tid %d, creator tid %d\n", |
| 2668 | real_pid, tsp.tts_u.tts_thread.tts_target_lwpid, |
| 2669 | real_tid); |
| 2670 | #endif |
| 2671 | |
| 2672 | /* Now we have to return the tid of the created thread, not |
| 2673 | * the creating thread, or "wait_for_inferior" won't know we |
| 2674 | * have a new "process" (thread). Plus we should record it |
| 2675 | * right, too. |
| 2676 | */ |
| 2677 | real_tid = tsp.tts_u.tts_thread.tts_target_lwpid; |
| 2678 | |
| 2679 | add_tthread (real_pid, real_tid); |
| 2680 | } |
| 2681 | |
| 2682 | else if ((tsp.tts_event & TTEVT_LWP_TERMINATE) |
| 2683 | || (tsp.tts_event & TTEVT_LWP_EXIT)) |
| 2684 | { |
| 2685 | |
| 2686 | #ifdef THREAD_DEBUG |
| 2687 | if (debug_on) |
| 2688 | printf ("Thread dies: %d\n", real_tid); |
| 2689 | #endif |
| 2690 | |
| 2691 | del_tthread (real_tid); |
| 2692 | } |
| 2693 | |
| 2694 | else if (tsp.tts_event & TTEVT_EXEC) |
| 2695 | { |
| 2696 | |
| 2697 | #ifdef THREAD_DEBUG |
| 2698 | if (debug_on) |
| 2699 | printf ("Pid %d has zero'th thread %d; inferior pid is %d\n", |
| 2700 | real_pid, real_tid, PIDGET (inferior_ptid)); |
| 2701 | #endif |
| 2702 | |
| 2703 | add_tthread (real_pid, real_tid); |
| 2704 | } |
| 2705 | |
| 2706 | #ifdef THREAD_DEBUG |
| 2707 | else if (debug_on) |
| 2708 | { |
| 2709 | printf ("Process-level event %s, using tid %d\n", |
| 2710 | get_printable_name_of_ttrace_event (tsp.tts_event), |
| 2711 | real_tid); |
| 2712 | |
| 2713 | /* OK to do this, as "add_tthread" won't add |
| 2714 | * duplicate entries. Also OK not to do it, |
| 2715 | * as this event isn't one which can change the |
| 2716 | * thread state. |
| 2717 | */ |
| 2718 | add_tthread (real_pid, real_tid); |
| 2719 | } |
| 2720 | #endif |
| 2721 | |
| 2722 | |
| 2723 | /* How many events are left to report later? |
| 2724 | * In a non-stop-the-world model, this isn't needed. |
| 2725 | * |
| 2726 | * Note that it's not always safe to query the thread state of a process, |
| 2727 | * which is what count_unhandled_events does. (If unsafe, we're left with |
| 2728 | * no other resort than to assume that no more events remain...) |
| 2729 | */ |
| 2730 | if (can_touch_threads_of_process (real_pid, tsp.tts_event)) |
| 2731 | more_events_left = count_unhandled_events (real_pid, real_tid); |
| 2732 | |
| 2733 | else |
| 2734 | { |
| 2735 | if (more_events_left > 0) |
| 2736 | warning ("Vfork or fork causing loss of %d buffered events.", |
| 2737 | more_events_left); |
| 2738 | |
| 2739 | more_events_left = 0; |
| 2740 | } |
| 2741 | |
| 2742 | /* Attempt to translate the ttrace_wait-returned status into the |
| 2743 | ptrace equivalent. |
| 2744 | |
| 2745 | ??rehrauer: This is somewhat fragile. We really ought to rewrite |
| 2746 | clients that expect to pick apart a ptrace wait status, to use |
| 2747 | something a little more abstract. |
| 2748 | */ |
| 2749 | if ((tsp.tts_event & TTEVT_EXEC) |
| 2750 | || (tsp.tts_event & TTEVT_FORK) |
| 2751 | || (tsp.tts_event & TTEVT_VFORK)) |
| 2752 | { |
| 2753 | /* Forks come in pairs (parent and child), so core gdb |
| 2754 | * will do two waits. Be ready to notice this. |
| 2755 | */ |
| 2756 | if (tsp.tts_event & TTEVT_FORK) |
| 2757 | { |
| 2758 | process_state = FORKING; |
| 2759 | |
| 2760 | #ifdef WAIT_BUFFER_DEBUG |
| 2761 | if (debug_on) |
| 2762 | printf ("Process set to FORKING\n"); |
| 2763 | #endif |
| 2764 | } |
| 2765 | else if (tsp.tts_event & TTEVT_VFORK) |
| 2766 | { |
| 2767 | process_state = VFORKING; |
| 2768 | |
| 2769 | #ifdef WAIT_BUFFER_DEBUG |
| 2770 | if (debug_on) |
| 2771 | printf ("Process set to VFORKING\n"); |
| 2772 | #endif |
| 2773 | } |
| 2774 | |
| 2775 | /* Make an exec or fork look like a breakpoint. Definitely a hack, |
| 2776 | but I don't think non HP-UX-specific clients really carefully |
| 2777 | inspect the first events they get after inferior startup, so |
| 2778 | it probably almost doesn't matter what we claim this is. |
| 2779 | */ |
| 2780 | |
| 2781 | #ifdef THREAD_DEBUG |
| 2782 | if (debug_on) |
| 2783 | printf ("..a process 'event'\n"); |
| 2784 | #endif |
| 2785 | |
| 2786 | /* Also make fork and exec events look like bpts, so they can be caught. |
| 2787 | */ |
| 2788 | *status = 0177 | (_SIGTRAP << 8); |
| 2789 | } |
| 2790 | |
| 2791 | /* Special-cases: We ask for syscall entry and exit events to implement |
| 2792 | "fast" (aka "hardware") watchpoints. |
| 2793 | |
| 2794 | When we get a syscall entry, we want to disable page-protections, |
| 2795 | and resume the inferior; this isn't an event we wish for |
| 2796 | wait_for_inferior to see. Note that we must resume ONLY the |
| 2797 | thread that reported the syscall entry; we don't want to allow |
| 2798 | other threads to run with the page protections off, as they might |
| 2799 | then be able to write to watch memory without it being caught. |
| 2800 | |
| 2801 | When we get a syscall exit, we want to reenable page-protections, |
| 2802 | but we don't want to resume the inferior; this is an event we wish |
| 2803 | wait_for_inferior to see. Make it look like the signal we normally |
| 2804 | get for a single-step completion. This should cause wait_for_inferior |
| 2805 | to evaluate whether any watchpoint triggered. |
| 2806 | |
| 2807 | Or rather, that's what we'd LIKE to do for syscall exit; we can't, |
| 2808 | due to some HP-UX "features". Some syscalls have problems with |
| 2809 | write-protections on some pages, and some syscalls seem to have |
| 2810 | pending writes to those pages at the time we're getting the return |
| 2811 | event. So, we'll single-step the inferior to get out of the syscall, |
| 2812 | and then reenable protections. |
| 2813 | |
| 2814 | Note that we're intentionally allowing the syscall exit case to |
| 2815 | fall through into the succeeding cases, as sometimes we single- |
| 2816 | step out of one syscall only to immediately enter another... |
| 2817 | */ |
| 2818 | else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY) |
| 2819 | || (tsp.tts_event & TTEVT_SYSCALL_RETURN)) |
| 2820 | { |
| 2821 | /* Make a syscall event look like a breakpoint. Same comments |
| 2822 | as for exec & fork events. |
| 2823 | */ |
| 2824 | #ifdef THREAD_DEBUG |
| 2825 | if (debug_on) |
| 2826 | printf ("..a syscall 'event'\n"); |
| 2827 | #endif |
| 2828 | |
| 2829 | /* Also make syscall events look like bpts, so they can be caught. |
| 2830 | */ |
| 2831 | *status = 0177 | (_SIGTRAP << 8); |
| 2832 | } |
| 2833 | |
| 2834 | else if ((tsp.tts_event & TTEVT_LWP_CREATE) |
| 2835 | || (tsp.tts_event & TTEVT_LWP_TERMINATE) |
| 2836 | || (tsp.tts_event & TTEVT_LWP_EXIT)) |
| 2837 | { |
| 2838 | /* Make a thread event look like a breakpoint. Same comments |
| 2839 | * as for exec & fork events. |
| 2840 | */ |
| 2841 | #ifdef THREAD_DEBUG |
| 2842 | if (debug_on) |
| 2843 | printf ("..a thread 'event'\n"); |
| 2844 | #endif |
| 2845 | |
| 2846 | /* Also make thread events look like bpts, so they can be caught. |
| 2847 | */ |
| 2848 | *status = 0177 | (_SIGTRAP << 8); |
| 2849 | } |
| 2850 | |
| 2851 | else if ((tsp.tts_event & TTEVT_EXIT)) |
| 2852 | { /* WIFEXITED */ |
| 2853 | |
| 2854 | #ifdef THREAD_DEBUG |
| 2855 | if (debug_on) |
| 2856 | printf ("..an exit\n"); |
| 2857 | #endif |
| 2858 | |
| 2859 | /* Prevent rest of gdb from thinking this is |
| 2860 | * a new thread if for some reason it's never |
| 2861 | * seen the main thread before. |
| 2862 | */ |
| 2863 | inferior_ptid = pid_to_ptid (map_to_gdb_tid (real_tid)); /* HACK, FIX */ |
| 2864 | |
| 2865 | *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode); |
| 2866 | } |
| 2867 | |
| 2868 | else if (tsp.tts_event & TTEVT_SIGNAL) |
| 2869 | { /* WIFSTOPPED */ |
| 2870 | #ifdef THREAD_DEBUG |
| 2871 | if (debug_on) |
| 2872 | printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo); |
| 2873 | #endif |
| 2874 | |
| 2875 | *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8); |
| 2876 | } |
| 2877 | |
| 2878 | else |
| 2879 | { /* !WIFSTOPPED */ |
| 2880 | |
| 2881 | /* This means the process or thread terminated. But we should've |
| 2882 | caught an explicit exit/termination above. So warn (this is |
| 2883 | really an internal error) and claim the process or thread |
| 2884 | terminated with a SIGTRAP. |
| 2885 | */ |
| 2886 | |
| 2887 | warning ("process_wait: unknown process state"); |
| 2888 | |
| 2889 | #ifdef THREAD_DEBUG |
| 2890 | if (debug_on) |
| 2891 | printf ("Process-level event %s, using tid %d\n", |
| 2892 | get_printable_name_of_ttrace_event (tsp.tts_event), |
| 2893 | real_tid); |
| 2894 | #endif |
| 2895 | |
| 2896 | *status = _SIGTRAP; |
| 2897 | } |
| 2898 | |
| 2899 | target_post_wait (pid_to_ptid (tsp.tts_pid), *status); |
| 2900 | |
| 2901 | |
| 2902 | #ifdef THREAD_DEBUG |
| 2903 | if (debug_on) |
| 2904 | printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid); |
| 2905 | #endif |
| 2906 | |
| 2907 | /* All code external to this module uses the tid, but calls |
| 2908 | * it "pid". There's some tweaking so that the outside sees |
| 2909 | * the first thread as having the same number as the starting |
| 2910 | * pid. |
| 2911 | */ |
| 2912 | return_pid = map_to_gdb_tid (real_tid); |
| 2913 | |
| 2914 | /* Remember this for later use in "hppa_prepare_to_proceed". |
| 2915 | */ |
| 2916 | old_gdb_pid = PIDGET (inferior_ptid); |
| 2917 | reported_pid = return_pid; |
| 2918 | reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo)); |
| 2919 | |
| 2920 | if (real_tid == 0 || return_pid == 0) |
| 2921 | { |
| 2922 | warning ("Internal error: process-wait failed."); |
| 2923 | } |
| 2924 | |
| 2925 | return return_pid; |
| 2926 | } |
| 2927 | \f |
| 2928 | |
| 2929 | /* This function causes the caller's process to be traced by its |
| 2930 | parent. This is intended to be called after GDB forks itself, |
| 2931 | and before the child execs the target. Despite the name, it |
| 2932 | is called by the child. |
| 2933 | |
| 2934 | Note that HP-UX ttrace is rather funky in how this is done. |
| 2935 | If the parent wants to get the initial exec event of a child, |
| 2936 | it must set the ttrace event mask of the child to include execs. |
| 2937 | (The child cannot do this itself.) This must be done after the |
| 2938 | child is forked, but before it execs. |
| 2939 | |
| 2940 | To coordinate the parent and child, we implement a semaphore using |
| 2941 | pipes. After SETTRC'ing itself, the child tells the parent that |
| 2942 | it is now traceable by the parent, and waits for the parent's |
| 2943 | acknowledgement. The parent can then set the child's event mask, |
| 2944 | and notify the child that it can now exec. |
| 2945 | |
| 2946 | (The acknowledgement by parent happens as a result of a call to |
| 2947 | child_acknowledge_created_inferior.) |
| 2948 | */ |
| 2949 | int |
| 2950 | parent_attach_all (void) |
| 2951 | { |
| 2952 | int tt_status; |
| 2953 | |
| 2954 | /* We need a memory home for a constant, to pass it to ttrace. |
| 2955 | The value of the constant is arbitrary, so long as both |
| 2956 | parent and child use the same value. Might as well use the |
| 2957 | "magic" constant provided by ttrace... |
| 2958 | */ |
| 2959 | uint64_t tc_magic_child = TT_VERSION; |
| 2960 | uint64_t tc_magic_parent = 0; |
| 2961 | |
| 2962 | tt_status = call_real_ttrace ( |
| 2963 | TT_PROC_SETTRC, |
| 2964 | (int) TT_NIL, |
| 2965 | (lwpid_t) TT_NIL, |
| 2966 | TT_NIL, |
| 2967 | (TTRACE_ARG_TYPE) TT_VERSION, |
| 2968 | TT_NIL); |
| 2969 | |
| 2970 | if (tt_status < 0) |
| 2971 | return tt_status; |
| 2972 | |
| 2973 | /* Notify the parent that we're potentially ready to exec(). */ |
| 2974 | write (startup_semaphore.child_channel[SEM_TALK], |
| 2975 | &tc_magic_child, |
| 2976 | sizeof (tc_magic_child)); |
| 2977 | |
| 2978 | /* Wait for acknowledgement from the parent. */ |
| 2979 | read (startup_semaphore.parent_channel[SEM_LISTEN], |
| 2980 | &tc_magic_parent, |
| 2981 | sizeof (tc_magic_parent)); |
| 2982 | |
| 2983 | if (tc_magic_child != tc_magic_parent) |
| 2984 | warning ("mismatched semaphore magic"); |
| 2985 | |
| 2986 | /* Discard our copy of the semaphore. */ |
| 2987 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); |
| 2988 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); |
| 2989 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); |
| 2990 | (void) close (startup_semaphore.child_channel[SEM_TALK]); |
| 2991 | |
| 2992 | return tt_status; |
| 2993 | } |
| 2994 | |
| 2995 | /* Despite being file-local, this routine is dealing with |
| 2996 | * actual process IDs, not thread ids. That's because it's |
| 2997 | * called before the first "wait" call, and there's no map |
| 2998 | * yet from tids to pids. |
| 2999 | * |
| 3000 | * When it is called, a forked child is running, but waiting on |
| 3001 | * the semaphore. If you stop the child and re-start it, |
| 3002 | * things get confused, so don't do that! An attached child is |
| 3003 | * stopped. |
| 3004 | * |
| 3005 | * Since this is called after either attach or run, we |
| 3006 | * have to be the common part of both. |
| 3007 | */ |
| 3008 | static void |
| 3009 | require_notification_of_events (int real_pid) |
| 3010 | { |
| 3011 | int tt_status; |
| 3012 | ttevent_t notifiable_events; |
| 3013 | |
| 3014 | lwpid_t tid; |
| 3015 | ttstate_t thread_state; |
| 3016 | |
| 3017 | #ifdef THREAD_DEBUG |
| 3018 | if (debug_on) |
| 3019 | printf ("Require notif, pid is %d\n", real_pid); |
| 3020 | #endif |
| 3021 | |
| 3022 | /* Temporary HACK: tell inftarg.c/child_wait to not |
| 3023 | * loop until pids are the same. |
| 3024 | */ |
| 3025 | not_same_real_pid = 0; |
| 3026 | |
| 3027 | sigemptyset (¬ifiable_events.tte_signals); |
| 3028 | notifiable_events.tte_opts = TTEO_NONE; |
| 3029 | |
| 3030 | /* This ensures that forked children inherit their parent's |
| 3031 | * event mask, which we're setting here. |
| 3032 | * |
| 3033 | * NOTE: if you debug gdb with itself, then the ultimate |
| 3034 | * debuggee gets flags set by the outermost gdb, as |
| 3035 | * a child of a child will still inherit. |
| 3036 | */ |
| 3037 | notifiable_events.tte_opts |= TTEO_PROC_INHERIT; |
| 3038 | |
| 3039 | notifiable_events.tte_events = TTEVT_DEFAULT; |
| 3040 | notifiable_events.tte_events |= TTEVT_SIGNAL; |
| 3041 | notifiable_events.tte_events |= TTEVT_EXEC; |
| 3042 | notifiable_events.tte_events |= TTEVT_EXIT; |
| 3043 | notifiable_events.tte_events |= TTEVT_FORK; |
| 3044 | notifiable_events.tte_events |= TTEVT_VFORK; |
| 3045 | notifiable_events.tte_events |= TTEVT_LWP_CREATE; |
| 3046 | notifiable_events.tte_events |= TTEVT_LWP_EXIT; |
| 3047 | notifiable_events.tte_events |= TTEVT_LWP_TERMINATE; |
| 3048 | |
| 3049 | tt_status = call_real_ttrace ( |
| 3050 | TT_PROC_SET_EVENT_MASK, |
| 3051 | real_pid, |
| 3052 | (lwpid_t) TT_NIL, |
| 3053 | (TTRACE_ARG_TYPE) & notifiable_events, |
| 3054 | (TTRACE_ARG_TYPE) sizeof (notifiable_events), |
| 3055 | TT_NIL); |
| 3056 | } |
| 3057 | |
| 3058 | static void |
| 3059 | require_notification_of_exec_events (int real_pid) |
| 3060 | { |
| 3061 | int tt_status; |
| 3062 | ttevent_t notifiable_events; |
| 3063 | |
| 3064 | lwpid_t tid; |
| 3065 | ttstate_t thread_state; |
| 3066 | |
| 3067 | #ifdef THREAD_DEBUG |
| 3068 | if (debug_on) |
| 3069 | printf ("Require notif, pid is %d\n", real_pid); |
| 3070 | #endif |
| 3071 | |
| 3072 | /* Temporary HACK: tell inftarg.c/child_wait to not |
| 3073 | * loop until pids are the same. |
| 3074 | */ |
| 3075 | not_same_real_pid = 0; |
| 3076 | |
| 3077 | sigemptyset (¬ifiable_events.tte_signals); |
| 3078 | notifiable_events.tte_opts = TTEO_NOSTRCCHLD; |
| 3079 | |
| 3080 | /* This ensures that forked children don't inherit their parent's |
| 3081 | * event mask, which we're setting here. |
| 3082 | */ |
| 3083 | notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT; |
| 3084 | |
| 3085 | notifiable_events.tte_events = TTEVT_DEFAULT; |
| 3086 | notifiable_events.tte_events |= TTEVT_EXEC; |
| 3087 | notifiable_events.tte_events |= TTEVT_EXIT; |
| 3088 | |
| 3089 | tt_status = call_real_ttrace ( |
| 3090 | TT_PROC_SET_EVENT_MASK, |
| 3091 | real_pid, |
| 3092 | (lwpid_t) TT_NIL, |
| 3093 | (TTRACE_ARG_TYPE) & notifiable_events, |
| 3094 | (TTRACE_ARG_TYPE) sizeof (notifiable_events), |
| 3095 | TT_NIL); |
| 3096 | } |
| 3097 | \f |
| 3098 | |
| 3099 | /* This function is called by the parent process, with pid being the |
| 3100 | * ID of the child process, after the debugger has forked. |
| 3101 | */ |
| 3102 | void |
| 3103 | child_acknowledge_created_inferior (int pid) |
| 3104 | { |
| 3105 | /* We need a memory home for a constant, to pass it to ttrace. |
| 3106 | The value of the constant is arbitrary, so long as both |
| 3107 | parent and child use the same value. Might as well use the |
| 3108 | "magic" constant provided by ttrace... |
| 3109 | */ |
| 3110 | uint64_t tc_magic_parent = TT_VERSION; |
| 3111 | uint64_t tc_magic_child = 0; |
| 3112 | |
| 3113 | /* Wait for the child to tell us that it has forked. */ |
| 3114 | read (startup_semaphore.child_channel[SEM_LISTEN], |
| 3115 | &tc_magic_child, |
| 3116 | sizeof (tc_magic_child)); |
| 3117 | |
| 3118 | /* Clear thread info now. We'd like to do this in |
| 3119 | * "require...", but that messes up attach. |
| 3120 | */ |
| 3121 | clear_thread_info (); |
| 3122 | |
| 3123 | /* Tell the "rest of gdb" that the initial thread exists. |
| 3124 | * This isn't really a hack. Other thread-based versions |
| 3125 | * of gdb (e.g. gnu-nat.c) seem to do the same thing. |
| 3126 | * |
| 3127 | * Q: Why don't we also add this thread to the local |
| 3128 | * list via "add_tthread"? |
| 3129 | * |
| 3130 | * A: Because we don't know the tid, and can't stop the |
| 3131 | * the process safely to ask what it is. Anyway, we'll |
| 3132 | * add it when it gets the EXEC event. |
| 3133 | */ |
| 3134 | add_thread (pid_to_ptid (pid)); /* in thread.c */ |
| 3135 | |
| 3136 | /* We can now set the child's ttrace event mask. |
| 3137 | */ |
| 3138 | require_notification_of_exec_events (pid); |
| 3139 | |
| 3140 | /* Tell ourselves that the process is running. |
| 3141 | */ |
| 3142 | process_state = RUNNING; |
| 3143 | |
| 3144 | /* Notify the child that it can exec. */ |
| 3145 | write (startup_semaphore.parent_channel[SEM_TALK], |
| 3146 | &tc_magic_parent, |
| 3147 | sizeof (tc_magic_parent)); |
| 3148 | |
| 3149 | /* Discard our copy of the semaphore. */ |
| 3150 | (void) close (startup_semaphore.parent_channel[SEM_LISTEN]); |
| 3151 | (void) close (startup_semaphore.parent_channel[SEM_TALK]); |
| 3152 | (void) close (startup_semaphore.child_channel[SEM_LISTEN]); |
| 3153 | (void) close (startup_semaphore.child_channel[SEM_TALK]); |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | /* |
| 3158 | * arrange for notification of all events by |
| 3159 | * calling require_notification_of_events. |
| 3160 | */ |
| 3161 | void |
| 3162 | child_post_startup_inferior (ptid_t ptid) |
| 3163 | { |
| 3164 | require_notification_of_events (PIDGET (ptid)); |
| 3165 | } |
| 3166 | |
| 3167 | /* From here on, we should expect tids rather than pids. |
| 3168 | */ |
| 3169 | static void |
| 3170 | hppa_enable_catch_fork (int tid) |
| 3171 | { |
| 3172 | int tt_status; |
| 3173 | ttevent_t ttrace_events; |
| 3174 | |
| 3175 | /* Get the set of events that are currently enabled. |
| 3176 | */ |
| 3177 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 3178 | tid, |
| 3179 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3180 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3181 | TT_NIL); |
| 3182 | if (errno) |
| 3183 | perror_with_name ("ttrace"); |
| 3184 | |
| 3185 | /* Add forks to that set. */ |
| 3186 | ttrace_events.tte_events |= TTEVT_FORK; |
| 3187 | |
| 3188 | #ifdef THREAD_DEBUG |
| 3189 | if (debug_on) |
| 3190 | printf ("enable fork, tid is %d\n", tid); |
| 3191 | #endif |
| 3192 | |
| 3193 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 3194 | tid, |
| 3195 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3196 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3197 | TT_NIL); |
| 3198 | if (errno) |
| 3199 | perror_with_name ("ttrace"); |
| 3200 | } |
| 3201 | |
| 3202 | |
| 3203 | static void |
| 3204 | hppa_disable_catch_fork (int tid) |
| 3205 | { |
| 3206 | int tt_status; |
| 3207 | ttevent_t ttrace_events; |
| 3208 | |
| 3209 | /* Get the set of events that are currently enabled. |
| 3210 | */ |
| 3211 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 3212 | tid, |
| 3213 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3214 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3215 | TT_NIL); |
| 3216 | |
| 3217 | if (errno) |
| 3218 | perror_with_name ("ttrace"); |
| 3219 | |
| 3220 | /* Remove forks from that set. */ |
| 3221 | ttrace_events.tte_events &= ~TTEVT_FORK; |
| 3222 | |
| 3223 | #ifdef THREAD_DEBUG |
| 3224 | if (debug_on) |
| 3225 | printf ("disable fork, tid is %d\n", tid); |
| 3226 | #endif |
| 3227 | |
| 3228 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 3229 | tid, |
| 3230 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3231 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3232 | TT_NIL); |
| 3233 | |
| 3234 | if (errno) |
| 3235 | perror_with_name ("ttrace"); |
| 3236 | } |
| 3237 | |
| 3238 | |
| 3239 | #if defined(CHILD_INSERT_FORK_CATCHPOINT) |
| 3240 | int |
| 3241 | child_insert_fork_catchpoint (int tid) |
| 3242 | { |
| 3243 | /* Enable reporting of fork events from the kernel. */ |
| 3244 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3245 | and just ignoring them if there's no catchpoint to catch them. |
| 3246 | */ |
| 3247 | return 0; |
| 3248 | } |
| 3249 | #endif |
| 3250 | |
| 3251 | |
| 3252 | #if defined(CHILD_REMOVE_FORK_CATCHPOINT) |
| 3253 | int |
| 3254 | child_remove_fork_catchpoint (int tid) |
| 3255 | { |
| 3256 | /* Disable reporting of fork events from the kernel. */ |
| 3257 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3258 | and just ignoring them if there's no catchpoint to catch them. |
| 3259 | */ |
| 3260 | return 0; |
| 3261 | } |
| 3262 | #endif |
| 3263 | |
| 3264 | |
| 3265 | static void |
| 3266 | hppa_enable_catch_vfork (int tid) |
| 3267 | { |
| 3268 | int tt_status; |
| 3269 | ttevent_t ttrace_events; |
| 3270 | |
| 3271 | /* Get the set of events that are currently enabled. |
| 3272 | */ |
| 3273 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 3274 | tid, |
| 3275 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3276 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3277 | TT_NIL); |
| 3278 | |
| 3279 | if (errno) |
| 3280 | perror_with_name ("ttrace"); |
| 3281 | |
| 3282 | /* Add vforks to that set. */ |
| 3283 | ttrace_events.tte_events |= TTEVT_VFORK; |
| 3284 | |
| 3285 | #ifdef THREAD_DEBUG |
| 3286 | if (debug_on) |
| 3287 | printf ("enable vfork, tid is %d\n", tid); |
| 3288 | #endif |
| 3289 | |
| 3290 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 3291 | tid, |
| 3292 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3293 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3294 | TT_NIL); |
| 3295 | |
| 3296 | if (errno) |
| 3297 | perror_with_name ("ttrace"); |
| 3298 | } |
| 3299 | |
| 3300 | |
| 3301 | static void |
| 3302 | hppa_disable_catch_vfork (int tid) |
| 3303 | { |
| 3304 | int tt_status; |
| 3305 | ttevent_t ttrace_events; |
| 3306 | |
| 3307 | /* Get the set of events that are currently enabled. */ |
| 3308 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 3309 | tid, |
| 3310 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3311 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3312 | TT_NIL); |
| 3313 | |
| 3314 | if (errno) |
| 3315 | perror_with_name ("ttrace"); |
| 3316 | |
| 3317 | /* Remove vforks from that set. */ |
| 3318 | ttrace_events.tte_events &= ~TTEVT_VFORK; |
| 3319 | |
| 3320 | #ifdef THREAD_DEBUG |
| 3321 | if (debug_on) |
| 3322 | printf ("disable vfork, tid is %d\n", tid); |
| 3323 | #endif |
| 3324 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 3325 | tid, |
| 3326 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 3327 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 3328 | TT_NIL); |
| 3329 | |
| 3330 | if (errno) |
| 3331 | perror_with_name ("ttrace"); |
| 3332 | } |
| 3333 | |
| 3334 | |
| 3335 | #if defined(CHILD_INSERT_VFORK_CATCHPOINT) |
| 3336 | int |
| 3337 | child_insert_vfork_catchpoint (int tid) |
| 3338 | { |
| 3339 | /* Enable reporting of vfork events from the kernel. */ |
| 3340 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3341 | and just ignoring them if there's no catchpoint to catch them. |
| 3342 | */ |
| 3343 | return 0; |
| 3344 | } |
| 3345 | #endif |
| 3346 | |
| 3347 | |
| 3348 | #if defined(CHILD_REMOVE_VFORK_CATCHPOINT) |
| 3349 | int |
| 3350 | child_remove_vfork_catchpoint (int tid) |
| 3351 | { |
| 3352 | /* Disable reporting of vfork events from the kernel. */ |
| 3353 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3354 | and just ignoring them if there's no catchpoint to catch them. |
| 3355 | */ |
| 3356 | return 0; |
| 3357 | } |
| 3358 | #endif |
| 3359 | |
| 3360 | /* Q: Do we need to map the returned process ID to a thread ID? |
| 3361 | |
| 3362 | * A: I don't think so--here we want a _real_ pid. Any later |
| 3363 | * operations will call "require_notification_of_events" and |
| 3364 | * start the mapping. |
| 3365 | */ |
| 3366 | int |
| 3367 | hpux_has_forked (int tid, int *childpid) |
| 3368 | { |
| 3369 | int tt_status; |
| 3370 | ttstate_t ttrace_state; |
| 3371 | thread_info *tinfo; |
| 3372 | |
| 3373 | /* Do we have cached thread state that we can consult? If so, use it. */ |
| 3374 | tinfo = find_thread_info (map_from_gdb_tid (tid)); |
| 3375 | if (tinfo != NULL) |
| 3376 | { |
| 3377 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); |
| 3378 | } |
| 3379 | |
| 3380 | /* Nope, must read the thread's current state */ |
| 3381 | else |
| 3382 | { |
| 3383 | tt_status = call_ttrace (TT_LWP_GET_STATE, |
| 3384 | tid, |
| 3385 | (TTRACE_ARG_TYPE) & ttrace_state, |
| 3386 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), |
| 3387 | TT_NIL); |
| 3388 | |
| 3389 | if (errno) |
| 3390 | perror_with_name ("ttrace"); |
| 3391 | |
| 3392 | if (tt_status < 0) |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | if (ttrace_state.tts_event & TTEVT_FORK) |
| 3397 | { |
| 3398 | *childpid = ttrace_state.tts_u.tts_fork.tts_fpid; |
| 3399 | return 1; |
| 3400 | } |
| 3401 | |
| 3402 | return 0; |
| 3403 | } |
| 3404 | |
| 3405 | /* See hpux_has_forked for pid discussion. |
| 3406 | */ |
| 3407 | int |
| 3408 | hpux_has_vforked (int tid, int *childpid) |
| 3409 | { |
| 3410 | int tt_status; |
| 3411 | ttstate_t ttrace_state; |
| 3412 | thread_info *tinfo; |
| 3413 | |
| 3414 | /* Do we have cached thread state that we can consult? If so, use it. */ |
| 3415 | tinfo = find_thread_info (map_from_gdb_tid (tid)); |
| 3416 | if (tinfo != NULL) |
| 3417 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); |
| 3418 | |
| 3419 | /* Nope, must read the thread's current state */ |
| 3420 | else |
| 3421 | { |
| 3422 | tt_status = call_ttrace (TT_LWP_GET_STATE, |
| 3423 | tid, |
| 3424 | (TTRACE_ARG_TYPE) & ttrace_state, |
| 3425 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), |
| 3426 | TT_NIL); |
| 3427 | |
| 3428 | if (errno) |
| 3429 | perror_with_name ("ttrace"); |
| 3430 | |
| 3431 | if (tt_status < 0) |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | if (ttrace_state.tts_event & TTEVT_VFORK) |
| 3436 | { |
| 3437 | *childpid = ttrace_state.tts_u.tts_fork.tts_fpid; |
| 3438 | return 1; |
| 3439 | } |
| 3440 | |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
| 3444 | |
| 3445 | #if defined(CHILD_INSERT_EXEC_CATCHPOINT) |
| 3446 | int |
| 3447 | child_insert_exec_catchpoint (int tid) |
| 3448 | { |
| 3449 | /* Enable reporting of exec events from the kernel. */ |
| 3450 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3451 | and just ignoring them if there's no catchpoint to catch them. |
| 3452 | */ |
| 3453 | return 0; |
| 3454 | } |
| 3455 | #endif |
| 3456 | |
| 3457 | |
| 3458 | #if defined(CHILD_REMOVE_EXEC_CATCHPOINT) |
| 3459 | int |
| 3460 | child_remove_exec_catchpoint (int tid) |
| 3461 | { |
| 3462 | /* Disable reporting of execevents from the kernel. */ |
| 3463 | /* ??rehrauer: For the moment, we're always enabling these events, |
| 3464 | and just ignoring them if there's no catchpoint to catch them. |
| 3465 | */ |
| 3466 | return 0; |
| 3467 | } |
| 3468 | #endif |
| 3469 | |
| 3470 | |
| 3471 | int |
| 3472 | hpux_has_execd (int tid, char **execd_pathname) |
| 3473 | { |
| 3474 | int tt_status; |
| 3475 | ttstate_t ttrace_state; |
| 3476 | thread_info *tinfo; |
| 3477 | |
| 3478 | /* Do we have cached thread state that we can consult? If so, use it. */ |
| 3479 | tinfo = find_thread_info (map_from_gdb_tid (tid)); |
| 3480 | if (tinfo != NULL) |
| 3481 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); |
| 3482 | |
| 3483 | /* Nope, must read the thread's current state */ |
| 3484 | else |
| 3485 | { |
| 3486 | tt_status = call_ttrace (TT_LWP_GET_STATE, |
| 3487 | tid, |
| 3488 | (TTRACE_ARG_TYPE) & ttrace_state, |
| 3489 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), |
| 3490 | TT_NIL); |
| 3491 | |
| 3492 | if (errno) |
| 3493 | perror_with_name ("ttrace"); |
| 3494 | |
| 3495 | if (tt_status < 0) |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
| 3499 | if (ttrace_state.tts_event & TTEVT_EXEC) |
| 3500 | { |
| 3501 | /* See child_pid_to_exec_file in this file: this is a macro. |
| 3502 | */ |
| 3503 | char *exec_file = target_pid_to_exec_file (tid); |
| 3504 | |
| 3505 | *execd_pathname = savestring (exec_file, strlen (exec_file)); |
| 3506 | return 1; |
| 3507 | } |
| 3508 | |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | int |
| 3514 | hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id) |
| 3515 | { |
| 3516 | int tt_status; |
| 3517 | ttstate_t ttrace_state; |
| 3518 | thread_info *tinfo; |
| 3519 | |
| 3520 | /* Do we have cached thread state that we can consult? If so, use it. */ |
| 3521 | tinfo = find_thread_info (map_from_gdb_tid (pid)); |
| 3522 | if (tinfo != NULL) |
| 3523 | copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state); |
| 3524 | |
| 3525 | /* Nope, must read the thread's current state */ |
| 3526 | else |
| 3527 | { |
| 3528 | tt_status = call_ttrace (TT_LWP_GET_STATE, |
| 3529 | pid, |
| 3530 | (TTRACE_ARG_TYPE) & ttrace_state, |
| 3531 | (TTRACE_ARG_TYPE) sizeof (ttrace_state), |
| 3532 | TT_NIL); |
| 3533 | |
| 3534 | if (errno) |
| 3535 | perror_with_name ("ttrace"); |
| 3536 | |
| 3537 | if (tt_status < 0) |
| 3538 | return 0; |
| 3539 | } |
| 3540 | |
| 3541 | *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */ |
| 3542 | *syscall_id = -1; |
| 3543 | |
| 3544 | if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY) |
| 3545 | *kind = TARGET_WAITKIND_SYSCALL_ENTRY; |
| 3546 | else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN) |
| 3547 | *kind = TARGET_WAITKIND_SYSCALL_RETURN; |
| 3548 | else |
| 3549 | return 0; |
| 3550 | |
| 3551 | *syscall_id = ttrace_state.tts_scno; |
| 3552 | return 1; |
| 3553 | } |
| 3554 | \f |
| 3555 | |
| 3556 | |
| 3557 | #if defined(CHILD_THREAD_ALIVE) |
| 3558 | |
| 3559 | /* Check to see if the given thread is alive. |
| 3560 | |
| 3561 | * We'll trust the thread list, as the more correct |
| 3562 | * approach of stopping the process and spinning down |
| 3563 | * the OS's thread list is _very_ expensive. |
| 3564 | * |
| 3565 | * May need a FIXME for that reason. |
| 3566 | */ |
| 3567 | int |
| 3568 | child_thread_alive (ptid_t ptid) |
| 3569 | { |
| 3570 | lwpid_t gdb_tid = PIDGET (ptid); |
| 3571 | lwpid_t tid; |
| 3572 | |
| 3573 | /* This spins down the lists twice. |
| 3574 | * Possible peformance improvement here! |
| 3575 | */ |
| 3576 | tid = map_from_gdb_tid (gdb_tid); |
| 3577 | return !is_terminated (tid); |
| 3578 | } |
| 3579 | |
| 3580 | #endif |
| 3581 | \f |
| 3582 | |
| 3583 | |
| 3584 | /* This function attempts to read the specified number of bytes from the |
| 3585 | save_state_t that is our view into the hardware registers, starting at |
| 3586 | ss_offset, and ending at ss_offset + sizeof_buf - 1 |
| 3587 | |
| 3588 | If this function succeeds, it deposits the fetched bytes into buf, |
| 3589 | and returns 0. |
| 3590 | |
| 3591 | If it fails, it returns a negative result. The contents of buf are |
| 3592 | undefined it this function fails. |
| 3593 | */ |
| 3594 | int |
| 3595 | read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf, |
| 3596 | int sizeof_buf) |
| 3597 | { |
| 3598 | int tt_status; |
| 3599 | register_value_t register_value = 0; |
| 3600 | |
| 3601 | tt_status = call_ttrace (TT_LWP_RUREGS, |
| 3602 | tid, |
| 3603 | ss_offset, |
| 3604 | (TTRACE_ARG_TYPE) sizeof_buf, |
| 3605 | (TTRACE_ARG_TYPE) buf); |
| 3606 | |
| 3607 | if (tt_status == 1) |
| 3608 | /* Map ttrace's version of success to our version. |
| 3609 | * Sometime ttrace returns 0, but that's ok here. |
| 3610 | */ |
| 3611 | return 0; |
| 3612 | |
| 3613 | return tt_status; |
| 3614 | } |
| 3615 | \f |
| 3616 | |
| 3617 | /* This function attempts to write the specified number of bytes to the |
| 3618 | save_state_t that is our view into the hardware registers, starting at |
| 3619 | ss_offset, and ending at ss_offset + sizeof_buf - 1 |
| 3620 | |
| 3621 | If this function succeeds, it deposits the bytes in buf, and returns 0. |
| 3622 | |
| 3623 | If it fails, it returns a negative result. The contents of the save_state_t |
| 3624 | are undefined it this function fails. |
| 3625 | */ |
| 3626 | int |
| 3627 | write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf, |
| 3628 | int sizeof_buf) |
| 3629 | { |
| 3630 | int tt_status; |
| 3631 | register_value_t register_value = 0; |
| 3632 | |
| 3633 | tt_status = call_ttrace (TT_LWP_WUREGS, |
| 3634 | tid, |
| 3635 | ss_offset, |
| 3636 | (TTRACE_ARG_TYPE) sizeof_buf, |
| 3637 | (TTRACE_ARG_TYPE) buf); |
| 3638 | return tt_status; |
| 3639 | } |
| 3640 | \f |
| 3641 | |
| 3642 | /* This function is a sop to the largeish number of direct calls |
| 3643 | to call_ptrace that exist in other files. Rather than create |
| 3644 | functions whose name abstracts away from ptrace, and change all |
| 3645 | the present callers of call_ptrace, we'll do the expedient (and |
| 3646 | perhaps only practical) thing. |
| 3647 | |
| 3648 | Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced |
| 3649 | process. Thus, we must translate all ptrace requests into their |
| 3650 | process-specific, ttrace equivalents. |
| 3651 | */ |
| 3652 | int |
| 3653 | call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data) |
| 3654 | { |
| 3655 | ttreq_t tt_request; |
| 3656 | TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr; |
| 3657 | TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data; |
| 3658 | TTRACE_ARG_TYPE tt_addr2 = TT_NIL; |
| 3659 | int tt_status; |
| 3660 | register_value_t register_value; |
| 3661 | int read_buf; |
| 3662 | |
| 3663 | /* Perform the necessary argument translation. Note that some |
| 3664 | cases are funky enough in the ttrace realm that we handle them |
| 3665 | very specially. |
| 3666 | */ |
| 3667 | switch (pt_request) |
| 3668 | { |
| 3669 | /* The following cases cannot conveniently be handled conveniently |
| 3670 | by merely adjusting the ptrace arguments and feeding into the |
| 3671 | generic call to ttrace at the bottom of this function. |
| 3672 | |
| 3673 | Note that because all branches of this switch end in "return", |
| 3674 | there's no need for any "break" statements. |
| 3675 | */ |
| 3676 | case PT_SETTRC: |
| 3677 | return parent_attach_all (); |
| 3678 | |
| 3679 | case PT_RUREGS: |
| 3680 | tt_status = read_from_register_save_state (gdb_tid, |
| 3681 | tt_addr, |
| 3682 | ®ister_value, |
| 3683 | sizeof (register_value)); |
| 3684 | if (tt_status < 0) |
| 3685 | return tt_status; |
| 3686 | return register_value; |
| 3687 | |
| 3688 | case PT_WUREGS: |
| 3689 | register_value = (int) tt_data; |
| 3690 | tt_status = write_to_register_save_state (gdb_tid, |
| 3691 | tt_addr, |
| 3692 | ®ister_value, |
| 3693 | sizeof (register_value)); |
| 3694 | return tt_status; |
| 3695 | break; |
| 3696 | |
| 3697 | case PT_READ_I: |
| 3698 | tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */ |
| 3699 | gdb_tid, |
| 3700 | tt_addr, |
| 3701 | (TTRACE_ARG_TYPE) 4, |
| 3702 | (TTRACE_ARG_TYPE) & read_buf); |
| 3703 | if (tt_status < 0) |
| 3704 | return tt_status; |
| 3705 | return read_buf; |
| 3706 | |
| 3707 | case PT_READ_D: |
| 3708 | tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */ |
| 3709 | gdb_tid, |
| 3710 | tt_addr, |
| 3711 | (TTRACE_ARG_TYPE) 4, |
| 3712 | (TTRACE_ARG_TYPE) & read_buf); |
| 3713 | if (tt_status < 0) |
| 3714 | return tt_status; |
| 3715 | return read_buf; |
| 3716 | |
| 3717 | case PT_ATTACH: |
| 3718 | tt_status = call_real_ttrace (TT_PROC_ATTACH, |
| 3719 | map_from_gdb_tid (gdb_tid), |
| 3720 | (lwpid_t) TT_NIL, |
| 3721 | tt_addr, |
| 3722 | (TTRACE_ARG_TYPE) TT_VERSION, |
| 3723 | tt_addr2); |
| 3724 | if (tt_status < 0) |
| 3725 | return tt_status; |
| 3726 | return tt_status; |
| 3727 | |
| 3728 | /* The following cases are handled by merely adjusting the ptrace |
| 3729 | arguments and feeding into the generic call to ttrace. |
| 3730 | */ |
| 3731 | case PT_DETACH: |
| 3732 | tt_request = TT_PROC_DETACH; |
| 3733 | break; |
| 3734 | |
| 3735 | case PT_WRITE_I: |
| 3736 | tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */ |
| 3737 | tt_data = 4; /* This many bytes. */ |
| 3738 | tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */ |
| 3739 | break; |
| 3740 | |
| 3741 | case PT_WRITE_D: |
| 3742 | tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */ |
| 3743 | tt_data = 4; /* This many bytes. */ |
| 3744 | tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */ |
| 3745 | break; |
| 3746 | |
| 3747 | case PT_RDTEXT: |
| 3748 | tt_request = TT_PROC_RDTEXT; |
| 3749 | break; |
| 3750 | |
| 3751 | case PT_RDDATA: |
| 3752 | tt_request = TT_PROC_RDDATA; |
| 3753 | break; |
| 3754 | |
| 3755 | case PT_WRTEXT: |
| 3756 | tt_request = TT_PROC_WRTEXT; |
| 3757 | break; |
| 3758 | |
| 3759 | case PT_WRDATA: |
| 3760 | tt_request = TT_PROC_WRDATA; |
| 3761 | break; |
| 3762 | |
| 3763 | case PT_CONTINUE: |
| 3764 | tt_request = TT_PROC_CONTINUE; |
| 3765 | break; |
| 3766 | |
| 3767 | case PT_STEP: |
| 3768 | tt_request = TT_LWP_SINGLE; /* Should not be making this request? */ |
| 3769 | break; |
| 3770 | |
| 3771 | case PT_KILL: |
| 3772 | tt_request = TT_PROC_EXIT; |
| 3773 | break; |
| 3774 | |
| 3775 | case PT_GET_PROCESS_PATHNAME: |
| 3776 | tt_request = TT_PROC_GET_PATHNAME; |
| 3777 | break; |
| 3778 | |
| 3779 | default: |
| 3780 | tt_request = pt_request; /* Let ttrace be the one to complain. */ |
| 3781 | break; |
| 3782 | } |
| 3783 | |
| 3784 | return call_ttrace (tt_request, |
| 3785 | gdb_tid, |
| 3786 | tt_addr, |
| 3787 | tt_data, |
| 3788 | tt_addr2); |
| 3789 | } |
| 3790 | |
| 3791 | /* Kill that pesky process! |
| 3792 | */ |
| 3793 | void |
| 3794 | kill_inferior (void) |
| 3795 | { |
| 3796 | int tid; |
| 3797 | int wait_status; |
| 3798 | thread_info *t; |
| 3799 | thread_info **paranoia; |
| 3800 | int para_count, i; |
| 3801 | |
| 3802 | if (PIDGET (inferior_ptid) == 0) |
| 3803 | return; |
| 3804 | |
| 3805 | /* Walk the list of "threads", some of which are "pseudo threads", |
| 3806 | aka "processes". For each that is NOT inferior_ptid, stop it, |
| 3807 | and detach it. |
| 3808 | |
| 3809 | You see, we may not have just a single process to kill. If we're |
| 3810 | restarting or quitting or detaching just after the inferior has |
| 3811 | forked, then we've actually two processes to clean up. |
| 3812 | |
| 3813 | But we can't just call target_mourn_inferior() for each, since that |
| 3814 | zaps the target vector. |
| 3815 | */ |
| 3816 | |
| 3817 | paranoia = (thread_info **) xmalloc (thread_head.count * |
| 3818 | sizeof (thread_info *)); |
| 3819 | para_count = 0; |
| 3820 | |
| 3821 | t = thread_head.head; |
| 3822 | while (t) |
| 3823 | { |
| 3824 | |
| 3825 | paranoia[para_count] = t; |
| 3826 | for (i = 0; i < para_count; i++) |
| 3827 | { |
| 3828 | if (t->next == paranoia[i]) |
| 3829 | { |
| 3830 | warning ("Bad data in gdb's thread data; repairing."); |
| 3831 | t->next = 0; |
| 3832 | } |
| 3833 | } |
| 3834 | para_count++; |
| 3835 | |
| 3836 | if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid))) |
| 3837 | { |
| 3838 | call_ttrace (TT_PROC_EXIT, |
| 3839 | t->pid, |
| 3840 | TT_NIL, |
| 3841 | TT_NIL, |
| 3842 | TT_NIL); |
| 3843 | } |
| 3844 | t = t->next; |
| 3845 | } |
| 3846 | |
| 3847 | xfree (paranoia); |
| 3848 | |
| 3849 | call_ttrace (TT_PROC_EXIT, |
| 3850 | PIDGET (inferior_ptid), |
| 3851 | TT_NIL, |
| 3852 | TT_NIL, |
| 3853 | TT_NIL); |
| 3854 | target_mourn_inferior (); |
| 3855 | clear_thread_info (); |
| 3856 | } |
| 3857 | |
| 3858 | |
| 3859 | #ifndef CHILD_RESUME |
| 3860 | |
| 3861 | /* Sanity check a thread about to be continued. |
| 3862 | */ |
| 3863 | static void |
| 3864 | thread_dropping_event_check (thread_info *p) |
| 3865 | { |
| 3866 | if (!p->handled) |
| 3867 | { |
| 3868 | /* |
| 3869 | * This seems to happen when we "next" over a |
| 3870 | * "fork()" while following the parent. If it's |
| 3871 | * the FORK event, that's ok. If it's a SIGNAL |
| 3872 | * in the unfollowed child, that's ok to--but |
| 3873 | * how can we know that's what's going on? |
| 3874 | * |
| 3875 | * FIXME! |
| 3876 | */ |
| 3877 | if (p->have_state) |
| 3878 | { |
| 3879 | if (p->last_stop_state.tts_event == TTEVT_FORK) |
| 3880 | { |
| 3881 | /* Ok */ |
| 3882 | ; |
| 3883 | } |
| 3884 | else if (p->last_stop_state.tts_event == TTEVT_SIGNAL) |
| 3885 | { |
| 3886 | /* Ok, close eyes and let it happen. |
| 3887 | */ |
| 3888 | ; |
| 3889 | } |
| 3890 | else |
| 3891 | { |
| 3892 | /* This shouldn't happen--we're dropping a |
| 3893 | * real event. |
| 3894 | */ |
| 3895 | warning ("About to continue process %d, thread %d with unhandled event %s.", |
| 3896 | p->pid, p->tid, |
| 3897 | get_printable_name_of_ttrace_event ( |
| 3898 | p->last_stop_state.tts_event)); |
| 3899 | |
| 3900 | #ifdef PARANOIA |
| 3901 | if (debug_on) |
| 3902 | print_tthread (p); |
| 3903 | #endif |
| 3904 | } |
| 3905 | } |
| 3906 | else |
| 3907 | { |
| 3908 | /* No saved state, have to assume it failed. |
| 3909 | */ |
| 3910 | warning ("About to continue process %d, thread %d with unhandled event.", |
| 3911 | p->pid, p->tid); |
| 3912 | #ifdef PARANOIA |
| 3913 | if (debug_on) |
| 3914 | print_tthread (p); |
| 3915 | #endif |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | } /* thread_dropping_event_check */ |
| 3920 | |
| 3921 | /* Use a loop over the threads to continue all the threads but |
| 3922 | * the one specified, which is to be stepped. |
| 3923 | */ |
| 3924 | static void |
| 3925 | threads_continue_all_but_one (lwpid_t gdb_tid, int signal) |
| 3926 | { |
| 3927 | thread_info *p; |
| 3928 | int thread_signal; |
| 3929 | lwpid_t real_tid; |
| 3930 | lwpid_t scan_tid; |
| 3931 | ttstate_t state; |
| 3932 | int real_pid; |
| 3933 | |
| 3934 | #ifdef THREAD_DEBUG |
| 3935 | if (debug_on) |
| 3936 | printf ("Using loop over threads to step/resume with signals\n"); |
| 3937 | #endif |
| 3938 | |
| 3939 | /* First update the thread list. |
| 3940 | */ |
| 3941 | set_all_unseen (); |
| 3942 | real_tid = map_from_gdb_tid (gdb_tid); |
| 3943 | real_pid = get_pid_for (real_tid); |
| 3944 | |
| 3945 | scan_tid = get_process_first_stopped_thread_id (real_pid, &state); |
| 3946 | while (0 != scan_tid) |
| 3947 | { |
| 3948 | |
| 3949 | #ifdef THREAD_DEBUG |
| 3950 | /* FIX: later should check state is stopped; |
| 3951 | * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED |
| 3952 | */ |
| 3953 | if (debug_on) |
| 3954 | if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED) |
| 3955 | printf ("About to continue non-stopped thread %d\n", scan_tid); |
| 3956 | #endif |
| 3957 | |
| 3958 | p = find_thread_info (scan_tid); |
| 3959 | if (NULL == p) |
| 3960 | { |
| 3961 | add_tthread (real_pid, scan_tid); |
| 3962 | p = find_thread_info (scan_tid); |
| 3963 | |
| 3964 | /* This is either a newly-created thread or the |
| 3965 | * result of a fork; in either case there's no |
| 3966 | * actual event to worry about. |
| 3967 | */ |
| 3968 | p->handled = 1; |
| 3969 | |
| 3970 | if (state.tts_event != TTEVT_NONE) |
| 3971 | { |
| 3972 | /* Oops, do need to worry! |
| 3973 | */ |
| 3974 | warning ("Unexpected thread with \"%s\" event.", |
| 3975 | get_printable_name_of_ttrace_event (state.tts_event)); |
| 3976 | } |
| 3977 | } |
| 3978 | else if (scan_tid != p->tid) |
| 3979 | error ("Bad data in thread database."); |
| 3980 | |
| 3981 | #ifdef THREAD_DEBUG |
| 3982 | if (debug_on) |
| 3983 | if (p->terminated) |
| 3984 | printf ("Why are we continuing a dead thread?\n"); |
| 3985 | #endif |
| 3986 | |
| 3987 | p->seen = 1; |
| 3988 | |
| 3989 | scan_tid = get_process_next_stopped_thread_id (real_pid, &state); |
| 3990 | } |
| 3991 | |
| 3992 | /* Remove unseen threads. |
| 3993 | */ |
| 3994 | update_thread_list (); |
| 3995 | |
| 3996 | /* Now run down the thread list and continue or step. |
| 3997 | */ |
| 3998 | for (p = thread_head.head; p; p = p->next) |
| 3999 | { |
| 4000 | |
| 4001 | /* Sanity check. |
| 4002 | */ |
| 4003 | thread_dropping_event_check (p); |
| 4004 | |
| 4005 | /* Pass the correct signals along. |
| 4006 | */ |
| 4007 | if (p->have_signal) |
| 4008 | { |
| 4009 | thread_signal = p->signal_value; |
| 4010 | p->have_signal = 0; |
| 4011 | } |
| 4012 | else |
| 4013 | thread_signal = 0; |
| 4014 | |
| 4015 | if (p->tid != real_tid) |
| 4016 | { |
| 4017 | /* |
| 4018 | * Not the thread of interest, so continue it |
| 4019 | * as the user expects. |
| 4020 | */ |
| 4021 | if (p->stepping_mode == DO_STEP) |
| 4022 | { |
| 4023 | /* Just step this thread. |
| 4024 | */ |
| 4025 | call_ttrace ( |
| 4026 | TT_LWP_SINGLE, |
| 4027 | p->tid, |
| 4028 | TT_USE_CURRENT_PC, |
| 4029 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), |
| 4030 | TT_NIL); |
| 4031 | } |
| 4032 | else |
| 4033 | { |
| 4034 | /* Regular continue (default case). |
| 4035 | */ |
| 4036 | call_ttrace ( |
| 4037 | TT_LWP_CONTINUE, |
| 4038 | p->tid, |
| 4039 | TT_USE_CURRENT_PC, |
| 4040 | (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal), |
| 4041 | TT_NIL); |
| 4042 | } |
| 4043 | } |
| 4044 | else |
| 4045 | { |
| 4046 | /* Step the thread of interest. |
| 4047 | */ |
| 4048 | call_ttrace ( |
| 4049 | TT_LWP_SINGLE, |
| 4050 | real_tid, |
| 4051 | TT_USE_CURRENT_PC, |
| 4052 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), |
| 4053 | TT_NIL); |
| 4054 | } |
| 4055 | } /* Loop over threads */ |
| 4056 | } /* End threads_continue_all_but_one */ |
| 4057 | |
| 4058 | /* Use a loop over the threads to continue all the threads. |
| 4059 | * This is done when a signal must be sent to any of the threads. |
| 4060 | */ |
| 4061 | static void |
| 4062 | threads_continue_all_with_signals (lwpid_t gdb_tid, int signal) |
| 4063 | { |
| 4064 | thread_info *p; |
| 4065 | int thread_signal; |
| 4066 | lwpid_t real_tid; |
| 4067 | lwpid_t scan_tid; |
| 4068 | ttstate_t state; |
| 4069 | int real_pid; |
| 4070 | |
| 4071 | #ifdef THREAD_DEBUG |
| 4072 | if (debug_on) |
| 4073 | printf ("Using loop over threads to resume with signals\n"); |
| 4074 | #endif |
| 4075 | |
| 4076 | /* Scan and update thread list. |
| 4077 | */ |
| 4078 | set_all_unseen (); |
| 4079 | real_tid = map_from_gdb_tid (gdb_tid); |
| 4080 | real_pid = get_pid_for (real_tid); |
| 4081 | |
| 4082 | scan_tid = get_process_first_stopped_thread_id (real_pid, &state); |
| 4083 | while (0 != scan_tid) |
| 4084 | { |
| 4085 | |
| 4086 | #ifdef THREAD_DEBUG |
| 4087 | if (debug_on) |
| 4088 | if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED) |
| 4089 | warning ("About to continue non-stopped thread %d\n", scan_tid); |
| 4090 | #endif |
| 4091 | |
| 4092 | p = find_thread_info (scan_tid); |
| 4093 | if (NULL == p) |
| 4094 | { |
| 4095 | add_tthread (real_pid, scan_tid); |
| 4096 | p = find_thread_info (scan_tid); |
| 4097 | |
| 4098 | /* This is either a newly-created thread or the |
| 4099 | * result of a fork; in either case there's no |
| 4100 | * actual event to worry about. |
| 4101 | */ |
| 4102 | p->handled = 1; |
| 4103 | |
| 4104 | if (state.tts_event != TTEVT_NONE) |
| 4105 | { |
| 4106 | /* Oops, do need to worry! |
| 4107 | */ |
| 4108 | warning ("Unexpected thread with \"%s\" event.", |
| 4109 | get_printable_name_of_ttrace_event (state.tts_event)); |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | #ifdef THREAD_DEBUG |
| 4114 | if (debug_on) |
| 4115 | if (p->terminated) |
| 4116 | printf ("Why are we continuing a dead thread? (1)\n"); |
| 4117 | #endif |
| 4118 | |
| 4119 | p->seen = 1; |
| 4120 | |
| 4121 | scan_tid = get_process_next_stopped_thread_id (real_pid, &state); |
| 4122 | } |
| 4123 | |
| 4124 | /* Remove unseen threads from our list. |
| 4125 | */ |
| 4126 | update_thread_list (); |
| 4127 | |
| 4128 | /* Continue the threads. |
| 4129 | */ |
| 4130 | for (p = thread_head.head; p; p = p->next) |
| 4131 | { |
| 4132 | |
| 4133 | /* Sanity check. |
| 4134 | */ |
| 4135 | thread_dropping_event_check (p); |
| 4136 | |
| 4137 | /* Pass the correct signals along. |
| 4138 | */ |
| 4139 | if (p->tid == real_tid) |
| 4140 | { |
| 4141 | thread_signal = signal; |
| 4142 | p->have_signal = 0; |
| 4143 | } |
| 4144 | else if (p->have_signal) |
| 4145 | { |
| 4146 | thread_signal = p->signal_value; |
| 4147 | p->have_signal = 0; |
| 4148 | } |
| 4149 | else |
| 4150 | thread_signal = 0; |
| 4151 | |
| 4152 | if (p->stepping_mode == DO_STEP) |
| 4153 | { |
| 4154 | call_ttrace ( |
| 4155 | TT_LWP_SINGLE, |
| 4156 | p->tid, |
| 4157 | TT_USE_CURRENT_PC, |
| 4158 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), |
| 4159 | TT_NIL); |
| 4160 | } |
| 4161 | else |
| 4162 | { |
| 4163 | /* Continue this thread (default case). |
| 4164 | */ |
| 4165 | call_ttrace ( |
| 4166 | TT_LWP_CONTINUE, |
| 4167 | p->tid, |
| 4168 | TT_USE_CURRENT_PC, |
| 4169 | (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal), |
| 4170 | TT_NIL); |
| 4171 | } |
| 4172 | } |
| 4173 | } /* End threads_continue_all_with_signals */ |
| 4174 | |
| 4175 | /* Step one thread only. |
| 4176 | */ |
| 4177 | static void |
| 4178 | thread_fake_step (lwpid_t tid, enum target_signal signal) |
| 4179 | { |
| 4180 | thread_info *p; |
| 4181 | |
| 4182 | #ifdef THREAD_DEBUG |
| 4183 | if (debug_on) |
| 4184 | { |
| 4185 | printf ("Doing a fake-step over a bpt, etc. for %d\n", tid); |
| 4186 | |
| 4187 | if (is_terminated (tid)) |
| 4188 | printf ("Why are we continuing a dead thread? (4)\n"); |
| 4189 | } |
| 4190 | #endif |
| 4191 | |
| 4192 | if (doing_fake_step) |
| 4193 | warning ("Step while step already in progress."); |
| 4194 | |
| 4195 | /* See if there's a saved signal value for this |
| 4196 | * thread to be passed on, but no current signal. |
| 4197 | */ |
| 4198 | p = find_thread_info (tid); |
| 4199 | if (p != NULL) |
| 4200 | { |
| 4201 | if (p->have_signal && signal == TARGET_SIGNAL_0) |
| 4202 | { |
| 4203 | /* Pass on a saved signal. |
| 4204 | */ |
| 4205 | signal = p->signal_value; |
| 4206 | } |
| 4207 | |
| 4208 | p->have_signal = 0; |
| 4209 | } |
| 4210 | |
| 4211 | if (!p->handled) |
| 4212 | warning ("Internal error: continuing unhandled thread."); |
| 4213 | |
| 4214 | call_ttrace (TT_LWP_SINGLE, |
| 4215 | tid, |
| 4216 | TT_USE_CURRENT_PC, |
| 4217 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), |
| 4218 | TT_NIL); |
| 4219 | |
| 4220 | /* Do bookkeeping so "call_ttrace_wait" knows it has to wait |
| 4221 | * for this thread only, and clear any saved signal info. |
| 4222 | */ |
| 4223 | doing_fake_step = 1; |
| 4224 | fake_step_tid = tid; |
| 4225 | |
| 4226 | } /* End thread_fake_step */ |
| 4227 | |
| 4228 | /* Continue one thread when a signal must be sent to it. |
| 4229 | */ |
| 4230 | static void |
| 4231 | threads_continue_one_with_signal (lwpid_t gdb_tid, int signal) |
| 4232 | { |
| 4233 | thread_info *p; |
| 4234 | lwpid_t real_tid; |
| 4235 | int real_pid; |
| 4236 | |
| 4237 | #ifdef THREAD_DEBUG |
| 4238 | if (debug_on) |
| 4239 | printf ("Continuing one thread with a signal\n"); |
| 4240 | #endif |
| 4241 | |
| 4242 | real_tid = map_from_gdb_tid (gdb_tid); |
| 4243 | real_pid = get_pid_for (real_tid); |
| 4244 | |
| 4245 | p = find_thread_info (real_tid); |
| 4246 | if (NULL == p) |
| 4247 | { |
| 4248 | add_tthread (real_pid, real_tid); |
| 4249 | } |
| 4250 | |
| 4251 | #ifdef THREAD_DEBUG |
| 4252 | if (debug_on) |
| 4253 | if (p->terminated) |
| 4254 | printf ("Why are we continuing a dead thread? (2)\n"); |
| 4255 | #endif |
| 4256 | |
| 4257 | if (!p->handled) |
| 4258 | warning ("Internal error: continuing unhandled thread."); |
| 4259 | |
| 4260 | p->have_signal = 0; |
| 4261 | |
| 4262 | call_ttrace (TT_LWP_CONTINUE, |
| 4263 | gdb_tid, |
| 4264 | TT_USE_CURRENT_PC, |
| 4265 | (TTRACE_ARG_TYPE) target_signal_to_host (signal), |
| 4266 | TT_NIL); |
| 4267 | } |
| 4268 | #endif |
| 4269 | |
| 4270 | #ifndef CHILD_RESUME |
| 4271 | |
| 4272 | /* Resume execution of the inferior process. |
| 4273 | |
| 4274 | * This routine is in charge of setting the "handled" bits. |
| 4275 | * |
| 4276 | * If STEP is zero, continue it. |
| 4277 | * If STEP is nonzero, single-step it. |
| 4278 | * |
| 4279 | * If SIGNAL is nonzero, give it that signal. |
| 4280 | * |
| 4281 | * If TID is -1, apply to all threads. |
| 4282 | * If TID is not -1, apply to specified thread. |
| 4283 | * |
| 4284 | * STEP |
| 4285 | * \ !0 0 |
| 4286 | * TID \________________________________________________ |
| 4287 | * | |
| 4288 | * -1 | Step current Continue all threads |
| 4289 | * | thread and (but which gets any |
| 4290 | * | continue others signal?--We look at |
| 4291 | * | "inferior_ptid") |
| 4292 | * | |
| 4293 | * N | Step _this_ thread Continue _this_ thread |
| 4294 | * | and leave others and leave others |
| 4295 | * | stopped; internally stopped; used only for |
| 4296 | * | used by gdb, never hardware watchpoints |
| 4297 | * | a user command. and attach, never a |
| 4298 | * | user command. |
| 4299 | */ |
| 4300 | void |
| 4301 | child_resume (ptid_t ptid, int step, enum target_signal signal) |
| 4302 | { |
| 4303 | int resume_all_threads; |
| 4304 | lwpid_t tid; |
| 4305 | process_state_t new_process_state; |
| 4306 | lwpid_t gdb_tid = PIDGET (ptid); |
| 4307 | |
| 4308 | resume_all_threads = |
| 4309 | (gdb_tid == INFTTRACE_ALL_THREADS) || |
| 4310 | (vfork_in_flight); |
| 4311 | |
| 4312 | if (resume_all_threads) |
| 4313 | { |
| 4314 | /* Resume all threads, but first pick a tid value |
| 4315 | * so we can get the pid when in call_ttrace doing |
| 4316 | * the map. |
| 4317 | */ |
| 4318 | if (vfork_in_flight) |
| 4319 | tid = vforking_child_pid; |
| 4320 | else |
| 4321 | tid = map_from_gdb_tid (PIDGET (inferior_ptid)); |
| 4322 | } |
| 4323 | else |
| 4324 | tid = map_from_gdb_tid (gdb_tid); |
| 4325 | |
| 4326 | #ifdef THREAD_DEBUG |
| 4327 | if (debug_on) |
| 4328 | { |
| 4329 | if (more_events_left) |
| 4330 | printf ("More events; "); |
| 4331 | |
| 4332 | if (signal != 0) |
| 4333 | printf ("Sending signal %d; ", signal); |
| 4334 | |
| 4335 | if (resume_all_threads) |
| 4336 | { |
| 4337 | if (step == 0) |
| 4338 | printf ("Continue process %d\n", tid); |
| 4339 | else |
| 4340 | printf ("Step/continue thread %d\n", tid); |
| 4341 | } |
| 4342 | else |
| 4343 | { |
| 4344 | if (step == 0) |
| 4345 | printf ("Continue thread %d\n", tid); |
| 4346 | else |
| 4347 | printf ("Step just thread %d\n", tid); |
| 4348 | } |
| 4349 | |
| 4350 | if (vfork_in_flight) |
| 4351 | printf ("Vfork in flight\n"); |
| 4352 | } |
| 4353 | #endif |
| 4354 | |
| 4355 | if (process_state == RUNNING) |
| 4356 | warning ("Internal error in resume logic; doing resume or step anyway."); |
| 4357 | |
| 4358 | if (!step /* Asked to continue... */ |
| 4359 | && resume_all_threads /* whole process.. */ |
| 4360 | && signal != 0 /* with a signal... */ |
| 4361 | && more_events_left > 0) |
| 4362 | { /* but we can't yet--save it! */ |
| 4363 | |
| 4364 | /* Continue with signal means we have to set the pending |
| 4365 | * signal value for this thread. |
| 4366 | */ |
| 4367 | thread_info *k; |
| 4368 | |
| 4369 | #ifdef THREAD_DEBUG |
| 4370 | if (debug_on) |
| 4371 | printf ("Saving signal %d for thread %d\n", signal, tid); |
| 4372 | #endif |
| 4373 | |
| 4374 | k = find_thread_info (tid); |
| 4375 | if (k != NULL) |
| 4376 | { |
| 4377 | k->have_signal = 1; |
| 4378 | k->signal_value = signal; |
| 4379 | |
| 4380 | #ifdef THREAD_DEBUG |
| 4381 | if (debug_on) |
| 4382 | if (k->terminated) |
| 4383 | printf ("Why are we continuing a dead thread? (3)\n"); |
| 4384 | #endif |
| 4385 | |
| 4386 | } |
| 4387 | |
| 4388 | #ifdef THREAD_DEBUG |
| 4389 | else if (debug_on) |
| 4390 | { |
| 4391 | printf ("No thread info for tid %d\n", tid); |
| 4392 | } |
| 4393 | #endif |
| 4394 | } |
| 4395 | |
| 4396 | /* Are we faking this "continue" or "step"? |
| 4397 | |
| 4398 | * We used to do steps by continuing all the threads for |
| 4399 | * which the events had been handled already. While |
| 4400 | * conceptually nicer (hides it all in a lower level), this |
| 4401 | * can lead to starvation and a hang (e.g. all but one thread |
| 4402 | * are unhandled at a breakpoint just before a "join" operation, |
| 4403 | * and one thread is in the join, and the user wants to step that |
| 4404 | * thread). |
| 4405 | */ |
| 4406 | if (resume_all_threads /* Whole process, therefore user command */ |
| 4407 | && more_events_left > 0) |
| 4408 | { /* But we can't do this yet--fake it! */ |
| 4409 | thread_info *p; |
| 4410 | |
| 4411 | if (!step) |
| 4412 | { |
| 4413 | /* No need to do any notes on a per-thread |
| 4414 | * basis--we're done! |
| 4415 | */ |
| 4416 | #ifdef WAIT_BUFFER_DEBUG |
| 4417 | if (debug_on) |
| 4418 | printf ("Faking a process resume.\n"); |
| 4419 | #endif |
| 4420 | |
| 4421 | return; |
| 4422 | } |
| 4423 | else |
| 4424 | { |
| 4425 | |
| 4426 | #ifdef WAIT_BUFFER_DEBUG |
| 4427 | if (debug_on) |
| 4428 | printf ("Faking a process step.\n"); |
| 4429 | #endif |
| 4430 | |
| 4431 | } |
| 4432 | |
| 4433 | p = find_thread_info (tid); |
| 4434 | if (p == NULL) |
| 4435 | { |
| 4436 | warning ("No thread information for tid %d, 'next' command ignored.\n", tid); |
| 4437 | return; |
| 4438 | } |
| 4439 | else |
| 4440 | { |
| 4441 | |
| 4442 | #ifdef THREAD_DEBUG |
| 4443 | if (debug_on) |
| 4444 | if (p->terminated) |
| 4445 | printf ("Why are we continuing a dead thread? (3.5)\n"); |
| 4446 | #endif |
| 4447 | |
| 4448 | if (p->stepping_mode != DO_DEFAULT) |
| 4449 | { |
| 4450 | warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored."); |
| 4451 | |
| 4452 | return; |
| 4453 | } |
| 4454 | |
| 4455 | if (step) |
| 4456 | p->stepping_mode = DO_STEP; |
| 4457 | else |
| 4458 | p->stepping_mode = DO_CONTINUE; |
| 4459 | |
| 4460 | return; |
| 4461 | } /* Have thread info */ |
| 4462 | } /* Must fake step or go */ |
| 4463 | |
| 4464 | /* Execept for fake-steps, from here on we know we are |
| 4465 | * going to wind up with a running process which will |
| 4466 | * need a real wait. |
| 4467 | */ |
| 4468 | new_process_state = RUNNING; |
| 4469 | |
| 4470 | /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where |
| 4471 | * it was. (If GDB wanted it to start some other way, we have already |
| 4472 | * written a new PC value to the child.) |
| 4473 | * |
| 4474 | * If this system does not support PT_STEP, a higher level function will |
| 4475 | * have called single_step() to transmute the step request into a |
| 4476 | * continue request (by setting breakpoints on all possible successor |
| 4477 | * instructions), so we don't have to worry about that here. |
| 4478 | */ |
| 4479 | if (step) |
| 4480 | { |
| 4481 | if (resume_all_threads) |
| 4482 | { |
| 4483 | /* |
| 4484 | * Regular user step: other threads get a "continue". |
| 4485 | */ |
| 4486 | threads_continue_all_but_one (tid, signal); |
| 4487 | clear_all_handled (); |
| 4488 | clear_all_stepping_mode (); |
| 4489 | } |
| 4490 | |
| 4491 | else |
| 4492 | { |
| 4493 | /* "Fake step": gdb is stepping one thread over a |
| 4494 | * breakpoint, watchpoint, or out of a library load |
| 4495 | * event, etc. The rest just stay where they are. |
| 4496 | * |
| 4497 | * Also used when there are pending events: we really |
| 4498 | * step the current thread, but leave the rest stopped. |
| 4499 | * Users can't request this, but "wait_for_inferior" |
| 4500 | * does--a lot! |
| 4501 | */ |
| 4502 | thread_fake_step (tid, signal); |
| 4503 | |
| 4504 | /* Clear the "handled" state of this thread, because |
| 4505 | * we'll soon get a new event for it. Other events |
| 4506 | * stay as they were. |
| 4507 | */ |
| 4508 | clear_handled (tid); |
| 4509 | clear_stepping_mode (tid); |
| 4510 | new_process_state = FAKE_STEPPING; |
| 4511 | } |
| 4512 | } |
| 4513 | |
| 4514 | else |
| 4515 | { |
| 4516 | /* TT_LWP_CONTINUE can pass signals to threads, TT_PROC_CONTINUE can't. |
| 4517 | Therefore, we really can't use TT_PROC_CONTINUE here. |
| 4518 | |
| 4519 | Consider a process which stopped due to signal which gdb decides |
| 4520 | to handle and not pass on to the inferior. In that case we must |
| 4521 | clear the pending signal by restarting the inferior using |
| 4522 | TT_LWP_CONTINUE and pass zero as the signal number. Else the |
| 4523 | pending signal will be passed to the inferior. interrupt.exp |
| 4524 | in the testsuite does this precise thing and fails due to the |
| 4525 | unwanted signal delivery to the inferior. */ |
| 4526 | /* drow/2002-12-05: However, note that we must use TT_PROC_CONTINUE |
| 4527 | if we are tracing a vfork. */ |
| 4528 | if (vfork_in_flight) |
| 4529 | { |
| 4530 | call_ttrace (TT_PROC_CONTINUE, tid, TT_NIL, TT_NIL, TT_NIL); |
| 4531 | clear_all_handled (); |
| 4532 | clear_all_stepping_mode (); |
| 4533 | } |
| 4534 | else if (resume_all_threads) |
| 4535 | { |
| 4536 | #ifdef THREAD_DEBUG |
| 4537 | if (debug_on) |
| 4538 | printf ("Doing a continue by loop of all threads\n"); |
| 4539 | #endif |
| 4540 | |
| 4541 | threads_continue_all_with_signals (tid, signal); |
| 4542 | |
| 4543 | clear_all_handled (); |
| 4544 | clear_all_stepping_mode (); |
| 4545 | } |
| 4546 | else |
| 4547 | { |
| 4548 | #ifdef THREAD_DEBUG |
| 4549 | printf ("Doing a continue w/signal of just thread %d\n", tid); |
| 4550 | #endif |
| 4551 | |
| 4552 | threads_continue_one_with_signal (tid, signal); |
| 4553 | |
| 4554 | /* Clear the "handled" state of this thread, because we |
| 4555 | will soon get a new event for it. Other events can |
| 4556 | stay as they were. */ |
| 4557 | clear_handled (tid); |
| 4558 | clear_stepping_mode (tid); |
| 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | process_state = new_process_state; |
| 4563 | |
| 4564 | #ifdef WAIT_BUFFER_DEBUG |
| 4565 | if (debug_on) |
| 4566 | printf ("Process set to %s\n", |
| 4567 | get_printable_name_of_process_state (process_state)); |
| 4568 | #endif |
| 4569 | |
| 4570 | } |
| 4571 | #endif /* CHILD_RESUME */ |
| 4572 | \f |
| 4573 | |
| 4574 | #ifdef ATTACH_DETACH |
| 4575 | /* |
| 4576 | * Like it says. |
| 4577 | * |
| 4578 | * One worry is that we may not be attaching to "inferior_ptid" |
| 4579 | * and thus may not want to clear out our data. FIXME? |
| 4580 | * |
| 4581 | */ |
| 4582 | static void |
| 4583 | update_thread_state_after_attach (int pid, attach_continue_t kind_of_go) |
| 4584 | { |
| 4585 | int tt_status; |
| 4586 | ttstate_t thread_state; |
| 4587 | lwpid_t a_thread; |
| 4588 | lwpid_t tid; |
| 4589 | |
| 4590 | /* The process better be stopped. |
| 4591 | */ |
| 4592 | if (process_state != STOPPED |
| 4593 | && process_state != VFORKING) |
| 4594 | warning ("Internal error attaching."); |
| 4595 | |
| 4596 | /* Clear out old tthread info and start over. This has the |
| 4597 | * side effect of ensuring that the TRAP is reported as being |
| 4598 | * in the right thread (re-mapped from tid to pid). |
| 4599 | * |
| 4600 | * It's because we need to add the tthread _now_ that we |
| 4601 | * need to call "clear_thread_info" _now_, and that's why |
| 4602 | * "require_notification_of_events" doesn't clear the thread |
| 4603 | * info (it's called later than this routine). |
| 4604 | */ |
| 4605 | clear_thread_info (); |
| 4606 | a_thread = 0; |
| 4607 | |
| 4608 | for (tid = get_process_first_stopped_thread_id (pid, &thread_state); |
| 4609 | tid != 0; |
| 4610 | tid = get_process_next_stopped_thread_id (pid, &thread_state)) |
| 4611 | { |
| 4612 | thread_info *p; |
| 4613 | |
| 4614 | if (a_thread == 0) |
| 4615 | { |
| 4616 | a_thread = tid; |
| 4617 | #ifdef THREAD_DEBUG |
| 4618 | if (debug_on) |
| 4619 | printf ("Attaching to process %d, thread %d\n", |
| 4620 | pid, a_thread); |
| 4621 | #endif |
| 4622 | } |
| 4623 | |
| 4624 | /* Tell ourselves and the "rest of gdb" that this thread |
| 4625 | * exists. |
| 4626 | * |
| 4627 | * This isn't really a hack. Other thread-based versions |
| 4628 | * of gdb (e.g. gnu-nat.c) seem to do the same thing. |
| 4629 | * |
| 4630 | * We don't need to do mapping here, as we know this |
| 4631 | * is the first thread and thus gets the real pid |
| 4632 | * (and is "inferior_ptid"). |
| 4633 | * |
| 4634 | * NOTE: it probably isn't the originating thread, |
| 4635 | * but that doesn't matter (we hope!). |
| 4636 | */ |
| 4637 | add_tthread (pid, tid); |
| 4638 | p = find_thread_info (tid); |
| 4639 | if (NULL == p) /* ?We just added it! */ |
| 4640 | error ("Internal error adding a thread on attach."); |
| 4641 | |
| 4642 | copy_ttstate_t (&p->last_stop_state, &thread_state); |
| 4643 | p->have_state = 1; |
| 4644 | |
| 4645 | if (DO_ATTACH_CONTINUE == kind_of_go) |
| 4646 | { |
| 4647 | /* |
| 4648 | * If we are going to CONTINUE afterwards, |
| 4649 | * raising a SIGTRAP, don't bother trying to |
| 4650 | * handle this event. But check first! |
| 4651 | */ |
| 4652 | switch (p->last_stop_state.tts_event) |
| 4653 | { |
| 4654 | |
| 4655 | case TTEVT_NONE: |
| 4656 | /* Ok to set this handled. |
| 4657 | */ |
| 4658 | break; |
| 4659 | |
| 4660 | default: |
| 4661 | warning ("Internal error; skipping event %s on process %d, thread %d.", |
| 4662 | get_printable_name_of_ttrace_event ( |
| 4663 | p->last_stop_state.tts_event), |
| 4664 | p->pid, p->tid); |
| 4665 | } |
| 4666 | |
| 4667 | set_handled (pid, tid); |
| 4668 | |
| 4669 | } |
| 4670 | else |
| 4671 | { |
| 4672 | /* There will be no "continue" opertion, so the |
| 4673 | * process remains stopped. Don't set any events |
| 4674 | * handled except the "gimmies". |
| 4675 | */ |
| 4676 | switch (p->last_stop_state.tts_event) |
| 4677 | { |
| 4678 | |
| 4679 | case TTEVT_NONE: |
| 4680 | /* Ok to ignore this. |
| 4681 | */ |
| 4682 | set_handled (pid, tid); |
| 4683 | break; |
| 4684 | |
| 4685 | case TTEVT_EXEC: |
| 4686 | case TTEVT_FORK: |
| 4687 | /* Expected "other" FORK or EXEC event from a |
| 4688 | * fork or vfork. |
| 4689 | */ |
| 4690 | break; |
| 4691 | |
| 4692 | default: |
| 4693 | printf ("Internal error: failed to handle event %s on process %d, thread %d.", |
| 4694 | get_printable_name_of_ttrace_event ( |
| 4695 | p->last_stop_state.tts_event), |
| 4696 | p->pid, p->tid); |
| 4697 | } |
| 4698 | } |
| 4699 | |
| 4700 | add_thread (pid_to_ptid (pid)); /* in thread.c */ |
| 4701 | } |
| 4702 | |
| 4703 | #ifdef PARANOIA |
| 4704 | if (debug_on) |
| 4705 | print_tthreads (); |
| 4706 | #endif |
| 4707 | |
| 4708 | /* One mustn't call ttrace_wait() after attaching via ttrace, |
| 4709 | 'cause the process is stopped already. |
| 4710 | |
| 4711 | However, the upper layers of gdb's execution control will |
| 4712 | want to wait after attaching (but not after forks, in |
| 4713 | which case they will be doing a "target_resume", anticipating |
| 4714 | a later TTEVT_EXEC or TTEVT_FORK event). |
| 4715 | |
| 4716 | To make this attach() implementation more compatible with |
| 4717 | others, we'll make the attached-to process raise a SIGTRAP. |
| 4718 | |
| 4719 | Issue: this continues only one thread. That could be |
| 4720 | dangerous if the thread is blocked--the process won't run |
| 4721 | and no trap will be raised. FIX! (check state.tts_flags? |
| 4722 | need one that's either TTS_WASRUNNING--but we've stopped |
| 4723 | it and made it TTS_WASSUSPENDED. Hum...FIXME!) |
| 4724 | */ |
| 4725 | if (DO_ATTACH_CONTINUE == kind_of_go) |
| 4726 | { |
| 4727 | tt_status = call_real_ttrace ( |
| 4728 | TT_LWP_CONTINUE, |
| 4729 | pid, |
| 4730 | a_thread, |
| 4731 | TT_USE_CURRENT_PC, |
| 4732 | (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP), |
| 4733 | TT_NIL); |
| 4734 | if (errno) |
| 4735 | perror_with_name ("ttrace"); |
| 4736 | |
| 4737 | clear_handled (a_thread); /* So TRAP will be reported. */ |
| 4738 | |
| 4739 | /* Now running. |
| 4740 | */ |
| 4741 | process_state = RUNNING; |
| 4742 | } |
| 4743 | |
| 4744 | attach_flag = 1; |
| 4745 | } |
| 4746 | #endif /* ATTACH_DETACH */ |
| 4747 | \f |
| 4748 | |
| 4749 | #ifdef ATTACH_DETACH |
| 4750 | /* Start debugging the process whose number is PID. |
| 4751 | * (A _real_ pid). |
| 4752 | */ |
| 4753 | int |
| 4754 | attach (int pid) |
| 4755 | { |
| 4756 | int tt_status; |
| 4757 | |
| 4758 | tt_status = call_real_ttrace ( |
| 4759 | TT_PROC_ATTACH, |
| 4760 | pid, |
| 4761 | (lwpid_t) TT_NIL, |
| 4762 | TT_NIL, |
| 4763 | (TTRACE_ARG_TYPE) TT_VERSION, |
| 4764 | TT_NIL); |
| 4765 | if (errno) |
| 4766 | perror_with_name ("ttrace attach"); |
| 4767 | |
| 4768 | /* If successful, the process is now stopped. |
| 4769 | */ |
| 4770 | process_state = STOPPED; |
| 4771 | |
| 4772 | /* Our caller ("attach_command" in "infcmd.c") |
| 4773 | * expects to do a "wait_for_inferior" after |
| 4774 | * the attach, so make sure the inferior is |
| 4775 | * running when we're done. |
| 4776 | */ |
| 4777 | update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE); |
| 4778 | |
| 4779 | return pid; |
| 4780 | } |
| 4781 | |
| 4782 | |
| 4783 | #if defined(CHILD_POST_ATTACH) |
| 4784 | void |
| 4785 | child_post_attach (int pid) |
| 4786 | { |
| 4787 | #ifdef THREAD_DEBUG |
| 4788 | if (debug_on) |
| 4789 | printf ("child-post-attach call\n"); |
| 4790 | #endif |
| 4791 | |
| 4792 | require_notification_of_events (pid); |
| 4793 | } |
| 4794 | #endif |
| 4795 | |
| 4796 | |
| 4797 | /* Stop debugging the process whose number is PID |
| 4798 | and continue it with signal number SIGNAL. |
| 4799 | SIGNAL = 0 means just continue it. |
| 4800 | */ |
| 4801 | void |
| 4802 | detach (int signal) |
| 4803 | { |
| 4804 | errno = 0; |
| 4805 | call_ttrace (TT_PROC_DETACH, |
| 4806 | PIDGET (inferior_ptid), |
| 4807 | TT_NIL, |
| 4808 | (TTRACE_ARG_TYPE) signal, |
| 4809 | TT_NIL); |
| 4810 | attach_flag = 0; |
| 4811 | |
| 4812 | clear_thread_info (); |
| 4813 | |
| 4814 | /* Process-state? */ |
| 4815 | } |
| 4816 | #endif /* ATTACH_DETACH */ |
| 4817 | \f |
| 4818 | |
| 4819 | /* Default the type of the ttrace transfer to int. */ |
| 4820 | #ifndef TTRACE_XFER_TYPE |
| 4821 | #define TTRACE_XFER_TYPE int |
| 4822 | #endif |
| 4823 | |
| 4824 | void |
| 4825 | _initialize_kernel_u_addr (void) |
| 4826 | { |
| 4827 | } |
| 4828 | |
| 4829 | #if !defined (CHILD_XFER_MEMORY) |
| 4830 | /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory |
| 4831 | in the NEW_SUN_TTRACE case. |
| 4832 | It ought to be straightforward. But it appears that writing did |
| 4833 | not write the data that I specified. I cannot understand where |
| 4834 | it got the data that it actually did write. */ |
| 4835 | |
| 4836 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR |
| 4837 | to debugger memory starting at MYADDR. Copy to inferior if |
| 4838 | WRITE is nonzero. TARGET is ignored. |
| 4839 | |
| 4840 | Returns the length copied, which is either the LEN argument or zero. |
| 4841 | This xfer function does not do partial moves, since child_ops |
| 4842 | doesn't allow memory operations to cross below us in the target stack |
| 4843 | anyway. */ |
| 4844 | |
| 4845 | int |
| 4846 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
| 4847 | struct mem_attrib *attrib, |
| 4848 | struct target_ops *target) |
| 4849 | { |
| 4850 | register int i; |
| 4851 | /* Round starting address down to longword boundary. */ |
| 4852 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (TTRACE_XFER_TYPE); |
| 4853 | /* Round ending address up; get number of longwords that makes. */ |
| 4854 | register int count |
| 4855 | = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1) |
| 4856 | / sizeof (TTRACE_XFER_TYPE); |
| 4857 | /* Allocate buffer of that many longwords. */ |
| 4858 | /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe |
| 4859 | because it uses alloca to allocate a buffer of arbitrary size. |
| 4860 | For very large xfers, this could crash GDB's stack. */ |
| 4861 | register TTRACE_XFER_TYPE *buffer |
| 4862 | = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE)); |
| 4863 | |
| 4864 | if (write) |
| 4865 | { |
| 4866 | /* Fill start and end extra bytes of buffer with existing memory data. */ |
| 4867 | |
| 4868 | if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE)) |
| 4869 | { |
| 4870 | /* Need part of initial word -- fetch it. */ |
| 4871 | buffer[0] = call_ttrace (TT_LWP_RDTEXT, |
| 4872 | PIDGET (inferior_ptid), |
| 4873 | (TTRACE_ARG_TYPE) addr, |
| 4874 | TT_NIL, |
| 4875 | TT_NIL); |
| 4876 | } |
| 4877 | |
| 4878 | if (count > 1) /* FIXME, avoid if even boundary */ |
| 4879 | { |
| 4880 | buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT, |
| 4881 | PIDGET (inferior_ptid), |
| 4882 | ((TTRACE_ARG_TYPE) |
| 4883 | (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))), |
| 4884 | TT_NIL, |
| 4885 | TT_NIL); |
| 4886 | } |
| 4887 | |
| 4888 | /* Copy data to be written over corresponding part of buffer */ |
| 4889 | |
| 4890 | memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)), |
| 4891 | myaddr, |
| 4892 | len); |
| 4893 | |
| 4894 | /* Write the entire buffer. */ |
| 4895 | |
| 4896 | for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE)) |
| 4897 | { |
| 4898 | errno = 0; |
| 4899 | call_ttrace (TT_LWP_WRDATA, |
| 4900 | PIDGET (inferior_ptid), |
| 4901 | (TTRACE_ARG_TYPE) addr, |
| 4902 | (TTRACE_ARG_TYPE) buffer[i], |
| 4903 | TT_NIL); |
| 4904 | if (errno) |
| 4905 | { |
| 4906 | /* Using the appropriate one (I or D) is necessary for |
| 4907 | Gould NP1, at least. */ |
| 4908 | errno = 0; |
| 4909 | call_ttrace (TT_LWP_WRTEXT, |
| 4910 | PIDGET (inferior_ptid), |
| 4911 | (TTRACE_ARG_TYPE) addr, |
| 4912 | (TTRACE_ARG_TYPE) buffer[i], |
| 4913 | TT_NIL); |
| 4914 | } |
| 4915 | if (errno) |
| 4916 | return 0; |
| 4917 | } |
| 4918 | } |
| 4919 | else |
| 4920 | { |
| 4921 | /* Read all the longwords */ |
| 4922 | for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE)) |
| 4923 | { |
| 4924 | errno = 0; |
| 4925 | buffer[i] = call_ttrace (TT_LWP_RDTEXT, |
| 4926 | PIDGET (inferior_ptid), |
| 4927 | (TTRACE_ARG_TYPE) addr, |
| 4928 | TT_NIL, |
| 4929 | TT_NIL); |
| 4930 | if (errno) |
| 4931 | return 0; |
| 4932 | QUIT; |
| 4933 | } |
| 4934 | |
| 4935 | /* Copy appropriate bytes out of the buffer. */ |
| 4936 | memcpy (myaddr, |
| 4937 | (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)), |
| 4938 | len); |
| 4939 | } |
| 4940 | return len; |
| 4941 | } |
| 4942 | \f |
| 4943 | |
| 4944 | static void |
| 4945 | udot_info (void) |
| 4946 | { |
| 4947 | int udot_off; /* Offset into user struct */ |
| 4948 | int udot_val; /* Value from user struct at udot_off */ |
| 4949 | char mess[128]; /* For messages */ |
| 4950 | |
| 4951 | if (!target_has_execution) |
| 4952 | { |
| 4953 | error ("The program is not being run."); |
| 4954 | } |
| 4955 | |
| 4956 | #if !defined (KERNEL_U_SIZE) |
| 4957 | |
| 4958 | /* Adding support for this command is easy. Typically you just add a |
| 4959 | routine, called "kernel_u_size" that returns the size of the user |
| 4960 | struct, to the appropriate *-nat.c file and then add to the native |
| 4961 | config file "#define KERNEL_U_SIZE kernel_u_size()" */ |
| 4962 | error ("Don't know how large ``struct user'' is in this version of gdb."); |
| 4963 | |
| 4964 | #else |
| 4965 | |
| 4966 | for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val)) |
| 4967 | { |
| 4968 | if ((udot_off % 24) == 0) |
| 4969 | { |
| 4970 | if (udot_off > 0) |
| 4971 | { |
| 4972 | printf_filtered ("\n"); |
| 4973 | } |
| 4974 | printf_filtered ("%04x:", udot_off); |
| 4975 | } |
| 4976 | udot_val = call_ttrace (TT_LWP_RUREGS, |
| 4977 | PIDGET (inferior_ptid), |
| 4978 | (TTRACE_ARG_TYPE) udot_off, |
| 4979 | TT_NIL, |
| 4980 | TT_NIL); |
| 4981 | if (errno != 0) |
| 4982 | { |
| 4983 | sprintf (mess, "\nreading user struct at offset 0x%x", udot_off); |
| 4984 | perror_with_name (mess); |
| 4985 | } |
| 4986 | /* Avoid using nonportable (?) "*" in print specs */ |
| 4987 | printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val); |
| 4988 | } |
| 4989 | printf_filtered ("\n"); |
| 4990 | |
| 4991 | #endif |
| 4992 | } |
| 4993 | #endif /* !defined (CHILD_XFER_MEMORY). */ |
| 4994 | |
| 4995 | |
| 4996 | /* TTrace version of "target_pid_to_exec_file" |
| 4997 | */ |
| 4998 | char * |
| 4999 | child_pid_to_exec_file (int tid) |
| 5000 | { |
| 5001 | int tt_status; |
| 5002 | static char exec_file_buffer[1024]; |
| 5003 | pid_t pid; |
| 5004 | static struct pst_status buf; |
| 5005 | |
| 5006 | /* On various versions of hpux11, this may fail due to a supposed |
| 5007 | kernel bug. We have alternate methods to get this information |
| 5008 | (ie pstat). */ |
| 5009 | tt_status = call_ttrace (TT_PROC_GET_PATHNAME, |
| 5010 | tid, |
| 5011 | (uint64_t) exec_file_buffer, |
| 5012 | sizeof (exec_file_buffer) - 1, |
| 5013 | 0); |
| 5014 | if (tt_status >= 0) |
| 5015 | return exec_file_buffer; |
| 5016 | |
| 5017 | /* Try to get process information via pstat and extract the filename |
| 5018 | from the pst_cmd field within the pst_status structure. */ |
| 5019 | if (pstat_getproc (&buf, sizeof (struct pst_status), 0, tid) != -1) |
| 5020 | { |
| 5021 | char *p = buf.pst_cmd; |
| 5022 | |
| 5023 | while (*p && *p != ' ') |
| 5024 | p++; |
| 5025 | *p = 0; |
| 5026 | |
| 5027 | return (buf.pst_cmd); |
| 5028 | } |
| 5029 | |
| 5030 | return (NULL); |
| 5031 | } |
| 5032 | |
| 5033 | void |
| 5034 | pre_fork_inferior (void) |
| 5035 | { |
| 5036 | int status; |
| 5037 | |
| 5038 | status = pipe (startup_semaphore.parent_channel); |
| 5039 | if (status < 0) |
| 5040 | { |
| 5041 | warning ("error getting parent pipe for startup semaphore"); |
| 5042 | return; |
| 5043 | } |
| 5044 | |
| 5045 | status = pipe (startup_semaphore.child_channel); |
| 5046 | if (status < 0) |
| 5047 | { |
| 5048 | warning ("error getting child pipe for startup semaphore"); |
| 5049 | return; |
| 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | /* Called from child_follow_fork in hppah-nat.c. |
| 5054 | * |
| 5055 | * This seems to be intended to attach after a fork or |
| 5056 | * vfork, while "attach" is used to attach to a pid |
| 5057 | * given by the user. The check for an existing attach |
| 5058 | * seems odd--it always fails in our test system. |
| 5059 | */ |
| 5060 | int |
| 5061 | hppa_require_attach (int pid) |
| 5062 | { |
| 5063 | int tt_status; |
| 5064 | CORE_ADDR pc; |
| 5065 | CORE_ADDR pc_addr; |
| 5066 | unsigned int regs_offset; |
| 5067 | process_state_t old_process_state = process_state; |
| 5068 | |
| 5069 | /* Are we already attached? There appears to be no explicit |
| 5070 | * way to answer this via ttrace, so we try something which |
| 5071 | * should be innocuous if we are attached. If that fails, |
| 5072 | * then we assume we're not attached, and so attempt to make |
| 5073 | * it so. |
| 5074 | */ |
| 5075 | errno = 0; |
| 5076 | tt_status = call_real_ttrace (TT_PROC_STOP, |
| 5077 | pid, |
| 5078 | (lwpid_t) TT_NIL, |
| 5079 | (TTRACE_ARG_TYPE) TT_NIL, |
| 5080 | (TTRACE_ARG_TYPE) TT_NIL, |
| 5081 | TT_NIL); |
| 5082 | |
| 5083 | if (errno) |
| 5084 | { |
| 5085 | /* No change to process-state! |
| 5086 | */ |
| 5087 | errno = 0; |
| 5088 | pid = attach (pid); |
| 5089 | } |
| 5090 | else |
| 5091 | { |
| 5092 | /* If successful, the process is now stopped. But if |
| 5093 | * we're VFORKING, the parent is still running, so don't |
| 5094 | * change the process state. |
| 5095 | */ |
| 5096 | if (process_state != VFORKING) |
| 5097 | process_state = STOPPED; |
| 5098 | |
| 5099 | /* If we were already attached, you'd think that we |
| 5100 | * would need to start going again--but you'd be wrong, |
| 5101 | * as the fork-following code is actually in the middle |
| 5102 | * of the "resume" routine in in "infrun.c" and so |
| 5103 | * will (almost) immediately do a resume. |
| 5104 | * |
| 5105 | * On the other hand, if we are VFORKING, which means |
| 5106 | * that the child and the parent share a process for a |
| 5107 | * while, we know that "resume" won't be resuming |
| 5108 | * until the child EXEC event is seen. But we still |
| 5109 | * don't want to continue, as the event is already |
| 5110 | * there waiting. |
| 5111 | */ |
| 5112 | update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE); |
| 5113 | } /* STOP succeeded */ |
| 5114 | |
| 5115 | return pid; |
| 5116 | } |
| 5117 | |
| 5118 | int |
| 5119 | hppa_require_detach (int pid, int signal) |
| 5120 | { |
| 5121 | int tt_status; |
| 5122 | |
| 5123 | /* If signal is non-zero, we must pass the signal on to the active |
| 5124 | thread prior to detaching. We do this by continuing the threads |
| 5125 | with the signal. |
| 5126 | */ |
| 5127 | if (signal != 0) |
| 5128 | { |
| 5129 | errno = 0; |
| 5130 | threads_continue_all_with_signals (pid, signal); |
| 5131 | } |
| 5132 | |
| 5133 | errno = 0; |
| 5134 | tt_status = call_ttrace (TT_PROC_DETACH, |
| 5135 | pid, |
| 5136 | TT_NIL, |
| 5137 | TT_NIL, |
| 5138 | TT_NIL); |
| 5139 | |
| 5140 | errno = 0; /* Ignore any errors. */ |
| 5141 | |
| 5142 | /* process_state? */ |
| 5143 | |
| 5144 | return pid; |
| 5145 | } |
| 5146 | |
| 5147 | /* Given the starting address of a memory page, hash it to a bucket in |
| 5148 | the memory page dictionary. |
| 5149 | */ |
| 5150 | static int |
| 5151 | get_dictionary_bucket_of_page (CORE_ADDR page_start) |
| 5152 | { |
| 5153 | int hash; |
| 5154 | |
| 5155 | hash = (page_start / memory_page_dictionary.page_size); |
| 5156 | hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; |
| 5157 | |
| 5158 | return hash; |
| 5159 | } |
| 5160 | |
| 5161 | |
| 5162 | /* Given a memory page's starting address, get (i.e., find an existing |
| 5163 | or create a new) dictionary entry for the page. The page will be |
| 5164 | write-protected when this function returns, but may have a reference |
| 5165 | count of 0 (if the page was newly-added to the dictionary). |
| 5166 | */ |
| 5167 | static memory_page_t * |
| 5168 | get_dictionary_entry_of_page (int pid, CORE_ADDR page_start) |
| 5169 | { |
| 5170 | int bucket; |
| 5171 | memory_page_t *page = NULL; |
| 5172 | memory_page_t *previous_page = NULL; |
| 5173 | |
| 5174 | /* We're going to be using the dictionary now, than-kew. */ |
| 5175 | require_memory_page_dictionary (); |
| 5176 | |
| 5177 | /* Try to find an existing dictionary entry for this page. Hash |
| 5178 | on the page's starting address. |
| 5179 | */ |
| 5180 | bucket = get_dictionary_bucket_of_page (page_start); |
| 5181 | page = &memory_page_dictionary.buckets[bucket]; |
| 5182 | while (page != NULL) |
| 5183 | { |
| 5184 | if (page->page_start == page_start) |
| 5185 | break; |
| 5186 | previous_page = page; |
| 5187 | page = page->next; |
| 5188 | } |
| 5189 | |
| 5190 | /* Did we find a dictionary entry for this page? If not, then |
| 5191 | add it to the dictionary now. |
| 5192 | */ |
| 5193 | if (page == NULL) |
| 5194 | { |
| 5195 | /* Create a new entry. */ |
| 5196 | page = (memory_page_t *) xmalloc (sizeof (memory_page_t)); |
| 5197 | page->page_start = page_start; |
| 5198 | page->reference_count = 0; |
| 5199 | page->next = NULL; |
| 5200 | page->previous = NULL; |
| 5201 | |
| 5202 | /* We'll write-protect the page now, if that's allowed. */ |
| 5203 | page->original_permissions = write_protect_page (pid, page_start); |
| 5204 | |
| 5205 | /* Add the new entry to the dictionary. */ |
| 5206 | page->previous = previous_page; |
| 5207 | previous_page->next = page; |
| 5208 | |
| 5209 | memory_page_dictionary.page_count++; |
| 5210 | } |
| 5211 | |
| 5212 | return page; |
| 5213 | } |
| 5214 | |
| 5215 | |
| 5216 | static void |
| 5217 | remove_dictionary_entry_of_page (int pid, memory_page_t *page) |
| 5218 | { |
| 5219 | /* Restore the page's original permissions. */ |
| 5220 | unwrite_protect_page (pid, page->page_start, page->original_permissions); |
| 5221 | |
| 5222 | /* Kick the page out of the dictionary. */ |
| 5223 | if (page->previous != NULL) |
| 5224 | page->previous->next = page->next; |
| 5225 | if (page->next != NULL) |
| 5226 | page->next->previous = page->previous; |
| 5227 | |
| 5228 | /* Just in case someone retains a handle to this after it's freed. */ |
| 5229 | page->page_start = (CORE_ADDR) 0; |
| 5230 | |
| 5231 | memory_page_dictionary.page_count--; |
| 5232 | |
| 5233 | xfree (page); |
| 5234 | } |
| 5235 | |
| 5236 | |
| 5237 | static void |
| 5238 | hppa_enable_syscall_events (int pid) |
| 5239 | { |
| 5240 | int tt_status; |
| 5241 | ttevent_t ttrace_events; |
| 5242 | |
| 5243 | /* Get the set of events that are currently enabled. */ |
| 5244 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 5245 | pid, |
| 5246 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 5247 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 5248 | TT_NIL); |
| 5249 | if (errno) |
| 5250 | perror_with_name ("ttrace"); |
| 5251 | |
| 5252 | /* Add syscall events to that set. */ |
| 5253 | ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY; |
| 5254 | ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN; |
| 5255 | |
| 5256 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 5257 | pid, |
| 5258 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 5259 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 5260 | TT_NIL); |
| 5261 | if (errno) |
| 5262 | perror_with_name ("ttrace"); |
| 5263 | } |
| 5264 | |
| 5265 | |
| 5266 | static void |
| 5267 | hppa_disable_syscall_events (int pid) |
| 5268 | { |
| 5269 | int tt_status; |
| 5270 | ttevent_t ttrace_events; |
| 5271 | |
| 5272 | /* Get the set of events that are currently enabled. */ |
| 5273 | tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK, |
| 5274 | pid, |
| 5275 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 5276 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 5277 | TT_NIL); |
| 5278 | if (errno) |
| 5279 | perror_with_name ("ttrace"); |
| 5280 | |
| 5281 | /* Remove syscall events from that set. */ |
| 5282 | ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY; |
| 5283 | ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN; |
| 5284 | |
| 5285 | tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK, |
| 5286 | pid, |
| 5287 | (TTRACE_ARG_TYPE) & ttrace_events, |
| 5288 | (TTRACE_ARG_TYPE) sizeof (ttrace_events), |
| 5289 | TT_NIL); |
| 5290 | if (errno) |
| 5291 | perror_with_name ("ttrace"); |
| 5292 | } |
| 5293 | |
| 5294 | |
| 5295 | /* The address range beginning with START and ending with START+LEN-1 |
| 5296 | (inclusive) is to be watched via page-protection by a new watchpoint. |
| 5297 | Set protection for all pages that overlap that range. |
| 5298 | |
| 5299 | Note that our caller sets TYPE to: |
| 5300 | 0 for a bp_hardware_watchpoint, |
| 5301 | 1 for a bp_read_watchpoint, |
| 5302 | 2 for a bp_access_watchpoint |
| 5303 | |
| 5304 | (Yes, this is intentionally (though lord only knows why) different |
| 5305 | from the TYPE that is passed to hppa_remove_hw_watchpoint.) |
| 5306 | */ |
| 5307 | int |
| 5308 | hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type) |
| 5309 | { |
| 5310 | CORE_ADDR page_start; |
| 5311 | int dictionary_was_empty; |
| 5312 | int page_size; |
| 5313 | int page_id; |
| 5314 | LONGEST range_size_in_pages; |
| 5315 | |
| 5316 | if (type != 0) |
| 5317 | error ("read or access hardware watchpoints not supported on HP-UX"); |
| 5318 | |
| 5319 | /* Examine all pages in the address range. */ |
| 5320 | require_memory_page_dictionary (); |
| 5321 | |
| 5322 | dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0); |
| 5323 | |
| 5324 | page_size = memory_page_dictionary.page_size; |
| 5325 | page_start = (start / page_size) * page_size; |
| 5326 | range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size; |
| 5327 | |
| 5328 | for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size) |
| 5329 | { |
| 5330 | memory_page_t *page; |
| 5331 | |
| 5332 | /* This gets the page entered into the dictionary if it was |
| 5333 | not already entered. |
| 5334 | */ |
| 5335 | page = get_dictionary_entry_of_page (pid, page_start); |
| 5336 | page->reference_count++; |
| 5337 | } |
| 5338 | |
| 5339 | /* Our implementation depends on seeing calls to kernel code, for the |
| 5340 | following reason. Here we ask to be notified of syscalls. |
| 5341 | |
| 5342 | When a protected page is accessed by user code, HP-UX raises a SIGBUS. |
| 5343 | Fine. |
| 5344 | |
| 5345 | But when kernel code accesses the page, it doesn't give a SIGBUS. |
| 5346 | Rather, the system call that touched the page fails, with errno=EFAULT. |
| 5347 | Not good for us. |
| 5348 | |
| 5349 | We could accomodate this "feature" by asking to be notified of syscall |
| 5350 | entries & exits; upon getting an entry event, disabling page-protections; |
| 5351 | upon getting an exit event, reenabling page-protections and then checking |
| 5352 | if any watchpoints triggered. |
| 5353 | |
| 5354 | However, this turns out to be a real performance loser. syscalls are |
| 5355 | usually a frequent occurrence. Having to unprotect-reprotect all watched |
| 5356 | pages, and also to then read all watched memory locations and compare for |
| 5357 | triggers, can be quite expensive. |
| 5358 | |
| 5359 | Instead, we'll only ask to be notified of syscall exits. When we get |
| 5360 | one, we'll check whether errno is set. If not, or if it's not EFAULT, |
| 5361 | we can just continue the inferior. |
| 5362 | |
| 5363 | If errno is set upon syscall exit to EFAULT, we must perform some fairly |
| 5364 | hackish stuff to determine whether the failure really was due to a |
| 5365 | page-protect trap on a watched location. |
| 5366 | */ |
| 5367 | if (dictionary_was_empty) |
| 5368 | hppa_enable_syscall_events (pid); |
| 5369 | |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | |
| 5374 | /* The address range beginning with START and ending with START+LEN-1 |
| 5375 | (inclusive) was being watched via page-protection by a watchpoint |
| 5376 | which has been removed. Remove protection for all pages that |
| 5377 | overlap that range, which are not also being watched by other |
| 5378 | watchpoints. |
| 5379 | */ |
| 5380 | int |
| 5381 | hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, |
| 5382 | enum bptype type) |
| 5383 | { |
| 5384 | CORE_ADDR page_start; |
| 5385 | int dictionary_is_empty; |
| 5386 | int page_size; |
| 5387 | int page_id; |
| 5388 | LONGEST range_size_in_pages; |
| 5389 | |
| 5390 | if (type != 0) |
| 5391 | error ("read or access hardware watchpoints not supported on HP-UX"); |
| 5392 | |
| 5393 | /* Examine all pages in the address range. */ |
| 5394 | require_memory_page_dictionary (); |
| 5395 | |
| 5396 | page_size = memory_page_dictionary.page_size; |
| 5397 | page_start = (start / page_size) * page_size; |
| 5398 | range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size; |
| 5399 | |
| 5400 | for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size) |
| 5401 | { |
| 5402 | memory_page_t *page; |
| 5403 | |
| 5404 | page = get_dictionary_entry_of_page (pid, page_start); |
| 5405 | page->reference_count--; |
| 5406 | |
| 5407 | /* Was this the last reference of this page? If so, then we |
| 5408 | must scrub the entry from the dictionary, and also restore |
| 5409 | the page's original permissions. |
| 5410 | */ |
| 5411 | if (page->reference_count == 0) |
| 5412 | remove_dictionary_entry_of_page (pid, page); |
| 5413 | } |
| 5414 | |
| 5415 | dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0); |
| 5416 | |
| 5417 | /* If write protections are currently disallowed, then that implies that |
| 5418 | wait_for_inferior believes that the inferior is within a system call. |
| 5419 | Since we want to see both syscall entry and return, it's clearly not |
| 5420 | good to disable syscall events in this state! |
| 5421 | |
| 5422 | ??rehrauer: Yeah, it'd be better if we had a specific flag that said, |
| 5423 | "inferior is between syscall events now". Oh well. |
| 5424 | */ |
| 5425 | if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed) |
| 5426 | hppa_disable_syscall_events (pid); |
| 5427 | |
| 5428 | return 1; |
| 5429 | } |
| 5430 | |
| 5431 | |
| 5432 | /* Could we implement a watchpoint of this type via our available |
| 5433 | hardware support? |
| 5434 | |
| 5435 | This query does not consider whether a particular address range |
| 5436 | could be so watched, but just whether support is generally available |
| 5437 | for such things. See hppa_range_profitable_for_hw_watchpoint for a |
| 5438 | query that answers whether a particular range should be watched via |
| 5439 | hardware support. |
| 5440 | */ |
| 5441 | int |
| 5442 | hppa_can_use_hw_watchpoint (enum bptype type, int cnt, enum bptype ot) |
| 5443 | { |
| 5444 | return (type == bp_hardware_watchpoint); |
| 5445 | } |
| 5446 | |
| 5447 | |
| 5448 | /* Assuming we could set a hardware watchpoint on this address, do |
| 5449 | we think it would be profitable ("a good idea") to do so? If not, |
| 5450 | we can always set a regular (aka single-step & test) watchpoint |
| 5451 | on the address... |
| 5452 | */ |
| 5453 | int |
| 5454 | hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len) |
| 5455 | { |
| 5456 | int range_is_stack_based; |
| 5457 | int range_is_accessible; |
| 5458 | CORE_ADDR page_start; |
| 5459 | int page_size; |
| 5460 | int page; |
| 5461 | LONGEST range_size_in_pages; |
| 5462 | |
| 5463 | /* ??rehrauer: For now, say that all addresses are potentially |
| 5464 | profitable. Possibly later we'll want to test the address |
| 5465 | for "stackness"? |
| 5466 | */ |
| 5467 | range_is_stack_based = 0; |
| 5468 | |
| 5469 | /* If any page in the range is inaccessible, then we cannot |
| 5470 | really use hardware watchpointing, even though our client |
| 5471 | thinks we can. In that case, it's actually an error to |
| 5472 | attempt to use hw watchpoints, so we'll tell our client |
| 5473 | that the range is "unprofitable", and hope that they listen... |
| 5474 | */ |
| 5475 | range_is_accessible = 1; /* Until proven otherwise. */ |
| 5476 | |
| 5477 | /* Examine all pages in the address range. */ |
| 5478 | errno = 0; |
| 5479 | page_size = sysconf (_SC_PAGE_SIZE); |
| 5480 | |
| 5481 | /* If we can't determine page size, we're hosed. Tell our |
| 5482 | client it's unprofitable to use hw watchpoints for this |
| 5483 | range. |
| 5484 | */ |
| 5485 | if (errno || (page_size <= 0)) |
| 5486 | { |
| 5487 | errno = 0; |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
| 5491 | page_start = (start / page_size) * page_size; |
| 5492 | range_size_in_pages = len / (LONGEST) page_size; |
| 5493 | |
| 5494 | for (page = 0; page < range_size_in_pages; page++, page_start += page_size) |
| 5495 | { |
| 5496 | int tt_status; |
| 5497 | int page_permissions; |
| 5498 | |
| 5499 | /* Is this page accessible? */ |
| 5500 | errno = 0; |
| 5501 | tt_status = call_ttrace (TT_PROC_GET_MPROTECT, |
| 5502 | pid, |
| 5503 | (TTRACE_ARG_TYPE) page_start, |
| 5504 | TT_NIL, |
| 5505 | (TTRACE_ARG_TYPE) & page_permissions); |
| 5506 | if (errno || (tt_status < 0)) |
| 5507 | { |
| 5508 | errno = 0; |
| 5509 | range_is_accessible = 0; |
| 5510 | break; |
| 5511 | } |
| 5512 | |
| 5513 | /* Yes, go for another... */ |
| 5514 | } |
| 5515 | |
| 5516 | return (!range_is_stack_based && range_is_accessible); |
| 5517 | } |
| 5518 | |
| 5519 | |
| 5520 | char * |
| 5521 | hppa_pid_or_tid_to_str (ptid_t ptid) |
| 5522 | { |
| 5523 | static char buf[100]; /* Static because address returned. */ |
| 5524 | pid_t id = PIDGET (ptid); |
| 5525 | |
| 5526 | /* Does this appear to be a process? If so, print it that way. */ |
| 5527 | if (is_process_id (id)) |
| 5528 | return child_pid_to_str (ptid); |
| 5529 | |
| 5530 | /* Else, print both the GDB thread number and the system thread id. */ |
| 5531 | sprintf (buf, "thread %d (", pid_to_thread_id (ptid)); |
| 5532 | strcat (buf, hppa_tid_to_str (ptid)); |
| 5533 | strcat (buf, ")\0"); |
| 5534 | |
| 5535 | return buf; |
| 5536 | } |
| 5537 | \f |
| 5538 | |
| 5539 | /* If the current pid is not the pid this module reported |
| 5540 | * from "ptrace_wait" with the most recent event, then the |
| 5541 | * user has switched threads. |
| 5542 | * |
| 5543 | * If the last reported event was a breakpoint, then return |
| 5544 | * the old thread id, else return 0. |
| 5545 | */ |
| 5546 | pid_t |
| 5547 | hppa_switched_threads (pid_t gdb_pid) |
| 5548 | { |
| 5549 | if (gdb_pid == old_gdb_pid) |
| 5550 | { |
| 5551 | /* |
| 5552 | * Core gdb is working with the same pid that it |
| 5553 | * was before we reported the last event. This |
| 5554 | * is ok: e.g. we reported hitting a thread-specific |
| 5555 | * breakpoint, but we were reporting the wrong |
| 5556 | * thread, so the core just ignored the event. |
| 5557 | * |
| 5558 | * No thread switch has happened. |
| 5559 | */ |
| 5560 | return (pid_t) 0; |
| 5561 | } |
| 5562 | else if (gdb_pid == reported_pid) |
| 5563 | { |
| 5564 | /* |
| 5565 | * Core gdb is working with the pid we reported, so |
| 5566 | * any continue or step will be able to figure out |
| 5567 | * that it needs to step over any hit breakpoints |
| 5568 | * without our (i.e. PREPARE_TO_PROCEED's) help. |
| 5569 | */ |
| 5570 | return (pid_t) 0; |
| 5571 | } |
| 5572 | else if (!reported_bpt) |
| 5573 | { |
| 5574 | /* |
| 5575 | * The core switched, but we didn't just report a |
| 5576 | * breakpoint, so there's no just-hit breakpoint |
| 5577 | * instruction at "reported_pid"'s PC, and thus there |
| 5578 | * is no need to step over it. |
| 5579 | */ |
| 5580 | return (pid_t) 0; |
| 5581 | } |
| 5582 | else |
| 5583 | { |
| 5584 | /* There's been a real switch, and we reported |
| 5585 | * a hit breakpoint. Let "hppa_prepare_to_proceed" |
| 5586 | * know, so it can see whether the breakpoint is |
| 5587 | * still active. |
| 5588 | */ |
| 5589 | return reported_pid; |
| 5590 | } |
| 5591 | |
| 5592 | /* Keep compiler happy with an obvious return at the end. |
| 5593 | */ |
| 5594 | return (pid_t) 0; |
| 5595 | } |
| 5596 | |
| 5597 | void |
| 5598 | hppa_ensure_vforking_parent_remains_stopped (int pid) |
| 5599 | { |
| 5600 | /* Nothing to do when using ttrace. Only the ptrace-based implementation |
| 5601 | must do real work. |
| 5602 | */ |
| 5603 | } |
| 5604 | |
| 5605 | |
| 5606 | int |
| 5607 | hppa_resume_execd_vforking_child_to_get_parent_vfork (void) |
| 5608 | { |
| 5609 | return 0; /* No, the parent vfork is available now. */ |
| 5610 | } |
| 5611 | \f |
| 5612 | |
| 5613 | /* Write a register as a 64bit value. This may be necessary if the |
| 5614 | native OS is too braindamaged to allow some (or all) registers to |
| 5615 | be written in 32bit hunks such as hpux11 and the PC queue registers. |
| 5616 | |
| 5617 | This is horribly gross and disgusting. */ |
| 5618 | |
| 5619 | int |
| 5620 | ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr) |
| 5621 | { |
| 5622 | pid_t pid; |
| 5623 | lwpid_t tid; |
| 5624 | int tt_status; |
| 5625 | |
| 5626 | tid = map_from_gdb_tid (gdb_tid); |
| 5627 | pid = get_pid_for (tid); |
| 5628 | |
| 5629 | errno = 0; |
| 5630 | tt_status = ttrace (TT_LWP_WUREGS, |
| 5631 | pid, |
| 5632 | tid, |
| 5633 | (TTRACE_ARG_TYPE) dest_addr, |
| 5634 | 8, |
| 5635 | (TTRACE_ARG_TYPE) src_addr ); |
| 5636 | |
| 5637 | #ifdef THREAD_DEBUG |
| 5638 | if (errno) |
| 5639 | { |
| 5640 | /* Don't bother for a known benign error: if you ask for the |
| 5641 | first thread state, but there is only one thread and it's |
| 5642 | not stopped, ttrace complains. |
| 5643 | |
| 5644 | We have this inside the #ifdef because our caller will do |
| 5645 | this check for real. */ |
| 5646 | if( request != TT_PROC_GET_FIRST_LWP_STATE |
| 5647 | || errno != EPROTO ) |
| 5648 | { |
| 5649 | if( debug_on ) |
| 5650 | printf( "TT fail for %s, with pid %d, tid %d, status %d \n", |
| 5651 | get_printable_name_of_ttrace_request (TT_LWP_WUREGS), |
| 5652 | pid, tid, tt_status ); |
| 5653 | } |
| 5654 | } |
| 5655 | #endif |
| 5656 | |
| 5657 | return tt_status; |
| 5658 | } |
| 5659 | |
| 5660 | void |
| 5661 | _initialize_infttrace (void) |
| 5662 | { |
| 5663 | /* Initialize the ttrace-based hardware watchpoint implementation. */ |
| 5664 | memory_page_dictionary.page_count = (LONGEST) - 1; |
| 5665 | memory_page_dictionary.page_protections_allowed = 1; |
| 5666 | |
| 5667 | errno = 0; |
| 5668 | memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE); |
| 5669 | |
| 5670 | /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure |
| 5671 | this is okay. */ |
| 5672 | if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *)) |
| 5673 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
| 5674 | |
| 5675 | if (errno || (memory_page_dictionary.page_size <= 0)) |
| 5676 | perror_with_name ("sysconf"); |
| 5677 | } |