| 1 | /* Thread management interface, for the remote server for GDB. |
| 2 | Copyright (C) 2002-2017 Free Software Foundation, Inc. |
| 3 | |
| 4 | Contributed by MontaVista Software. |
| 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "server.h" |
| 22 | |
| 23 | #include "linux-low.h" |
| 24 | |
| 25 | extern int debug_threads; |
| 26 | |
| 27 | #include "gdb_proc_service.h" |
| 28 | #include "nat/gdb_thread_db.h" |
| 29 | #include "gdb_vecs.h" |
| 30 | #include "nat/linux-procfs.h" |
| 31 | #include "common/scoped_restore.h" |
| 32 | |
| 33 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
| 34 | #include <dlfcn.h> |
| 35 | #endif |
| 36 | #include <limits.h> |
| 37 | #include <ctype.h> |
| 38 | |
| 39 | struct thread_db |
| 40 | { |
| 41 | /* Structure that identifies the child process for the |
| 42 | <proc_service.h> interface. */ |
| 43 | struct ps_prochandle proc_handle; |
| 44 | |
| 45 | /* Connection to the libthread_db library. */ |
| 46 | td_thragent_t *thread_agent; |
| 47 | |
| 48 | /* If this flag has been set, we've already asked GDB for all |
| 49 | symbols we might need; assume symbol cache misses are |
| 50 | failures. */ |
| 51 | int all_symbols_looked_up; |
| 52 | |
| 53 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
| 54 | /* Handle of the libthread_db from dlopen. */ |
| 55 | void *handle; |
| 56 | #endif |
| 57 | |
| 58 | /* Addresses of libthread_db functions. */ |
| 59 | td_ta_new_ftype *td_ta_new_p; |
| 60 | td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p; |
| 61 | td_thr_get_info_ftype *td_thr_get_info_p; |
| 62 | td_ta_thr_iter_ftype *td_ta_thr_iter_p; |
| 63 | td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p; |
| 64 | td_thr_tlsbase_ftype *td_thr_tlsbase_p; |
| 65 | td_symbol_list_ftype *td_symbol_list_p; |
| 66 | }; |
| 67 | |
| 68 | static char *libthread_db_search_path; |
| 69 | |
| 70 | static int find_one_thread (ptid_t); |
| 71 | static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data); |
| 72 | |
| 73 | static const char * |
| 74 | thread_db_err_str (td_err_e err) |
| 75 | { |
| 76 | static char buf[64]; |
| 77 | |
| 78 | switch (err) |
| 79 | { |
| 80 | case TD_OK: |
| 81 | return "generic 'call succeeded'"; |
| 82 | case TD_ERR: |
| 83 | return "generic error"; |
| 84 | case TD_NOTHR: |
| 85 | return "no thread to satisfy query"; |
| 86 | case TD_NOSV: |
| 87 | return "no sync handle to satisfy query"; |
| 88 | case TD_NOLWP: |
| 89 | return "no LWP to satisfy query"; |
| 90 | case TD_BADPH: |
| 91 | return "invalid process handle"; |
| 92 | case TD_BADTH: |
| 93 | return "invalid thread handle"; |
| 94 | case TD_BADSH: |
| 95 | return "invalid synchronization handle"; |
| 96 | case TD_BADTA: |
| 97 | return "invalid thread agent"; |
| 98 | case TD_BADKEY: |
| 99 | return "invalid key"; |
| 100 | case TD_NOMSG: |
| 101 | return "no event message for getmsg"; |
| 102 | case TD_NOFPREGS: |
| 103 | return "FPU register set not available"; |
| 104 | case TD_NOLIBTHREAD: |
| 105 | return "application not linked with libthread"; |
| 106 | case TD_NOEVENT: |
| 107 | return "requested event is not supported"; |
| 108 | case TD_NOCAPAB: |
| 109 | return "capability not available"; |
| 110 | case TD_DBERR: |
| 111 | return "debugger service failed"; |
| 112 | case TD_NOAPLIC: |
| 113 | return "operation not applicable to"; |
| 114 | case TD_NOTSD: |
| 115 | return "no thread-specific data for this thread"; |
| 116 | case TD_MALLOC: |
| 117 | return "malloc failed"; |
| 118 | case TD_PARTIALREG: |
| 119 | return "only part of register set was written/read"; |
| 120 | case TD_NOXREGS: |
| 121 | return "X register set not available for this thread"; |
| 122 | #ifdef HAVE_TD_VERSION |
| 123 | case TD_VERSION: |
| 124 | return "version mismatch between libthread_db and libpthread"; |
| 125 | #endif |
| 126 | default: |
| 127 | xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); |
| 128 | return buf; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #if 0 |
| 133 | static char * |
| 134 | thread_db_state_str (td_thr_state_e state) |
| 135 | { |
| 136 | static char buf[64]; |
| 137 | |
| 138 | switch (state) |
| 139 | { |
| 140 | case TD_THR_STOPPED: |
| 141 | return "stopped by debugger"; |
| 142 | case TD_THR_RUN: |
| 143 | return "runnable"; |
| 144 | case TD_THR_ACTIVE: |
| 145 | return "active"; |
| 146 | case TD_THR_ZOMBIE: |
| 147 | return "zombie"; |
| 148 | case TD_THR_SLEEP: |
| 149 | return "sleeping"; |
| 150 | case TD_THR_STOPPED_ASLEEP: |
| 151 | return "stopped by debugger AND blocked"; |
| 152 | default: |
| 153 | xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state); |
| 154 | return buf; |
| 155 | } |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | /* Get thread info about PTID, accessing memory via the current |
| 160 | thread. */ |
| 161 | |
| 162 | static int |
| 163 | find_one_thread (ptid_t ptid) |
| 164 | { |
| 165 | td_thrhandle_t th; |
| 166 | td_thrinfo_t ti; |
| 167 | td_err_e err; |
| 168 | struct thread_info *inferior; |
| 169 | struct lwp_info *lwp; |
| 170 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
| 171 | int lwpid = ptid_get_lwp (ptid); |
| 172 | |
| 173 | inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid); |
| 174 | lwp = get_thread_lwp (inferior); |
| 175 | if (lwp->thread_known) |
| 176 | return 1; |
| 177 | |
| 178 | /* Get information about this thread. */ |
| 179 | err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th); |
| 180 | if (err != TD_OK) |
| 181 | error ("Cannot get thread handle for LWP %d: %s", |
| 182 | lwpid, thread_db_err_str (err)); |
| 183 | |
| 184 | err = thread_db->td_thr_get_info_p (&th, &ti); |
| 185 | if (err != TD_OK) |
| 186 | error ("Cannot get thread info for LWP %d: %s", |
| 187 | lwpid, thread_db_err_str (err)); |
| 188 | |
| 189 | if (debug_threads) |
| 190 | debug_printf ("Found thread %ld (LWP %d)\n", |
| 191 | (unsigned long) ti.ti_tid, ti.ti_lid); |
| 192 | |
| 193 | if (lwpid != ti.ti_lid) |
| 194 | { |
| 195 | warning ("PID mismatch! Expected %ld, got %ld", |
| 196 | (long) lwpid, (long) ti.ti_lid); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* If the new thread ID is zero, a final thread ID will be available |
| 201 | later. Do not enable thread debugging yet. */ |
| 202 | if (ti.ti_tid == 0) |
| 203 | return 0; |
| 204 | |
| 205 | lwp->thread_known = 1; |
| 206 | lwp->th = th; |
| 207 | lwp->thread_handle = ti.ti_tid; |
| 208 | |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | /* Attach a thread. Return true on success. */ |
| 213 | |
| 214 | static int |
| 215 | attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) |
| 216 | { |
| 217 | struct process_info *proc = current_process (); |
| 218 | int pid = pid_of (proc); |
| 219 | ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0); |
| 220 | struct lwp_info *lwp; |
| 221 | int err; |
| 222 | |
| 223 | if (debug_threads) |
| 224 | debug_printf ("Attaching to thread %ld (LWP %d)\n", |
| 225 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid); |
| 226 | err = linux_attach_lwp (ptid); |
| 227 | if (err != 0) |
| 228 | { |
| 229 | warning ("Could not attach to thread %ld (LWP %d): %s\n", |
| 230 | (unsigned long) ti_p->ti_tid, ti_p->ti_lid, |
| 231 | linux_ptrace_attach_fail_reason_string (ptid, err)); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | lwp = find_lwp_pid (ptid); |
| 236 | gdb_assert (lwp != NULL); |
| 237 | lwp->thread_known = 1; |
| 238 | lwp->th = *th_p; |
| 239 | lwp->thread_handle = ti_p->ti_tid; |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | /* Attach thread if we haven't seen it yet. |
| 245 | Increment *COUNTER if we have attached a new thread. |
| 246 | Return false on failure. */ |
| 247 | |
| 248 | static int |
| 249 | maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, |
| 250 | int *counter) |
| 251 | { |
| 252 | struct lwp_info *lwp; |
| 253 | |
| 254 | lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid)); |
| 255 | if (lwp != NULL) |
| 256 | return 1; |
| 257 | |
| 258 | if (!attach_thread (th_p, ti_p)) |
| 259 | return 0; |
| 260 | |
| 261 | if (counter != NULL) |
| 262 | *counter += 1; |
| 263 | |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | static int |
| 268 | find_new_threads_callback (const td_thrhandle_t *th_p, void *data) |
| 269 | { |
| 270 | td_thrinfo_t ti; |
| 271 | td_err_e err; |
| 272 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
| 273 | |
| 274 | err = thread_db->td_thr_get_info_p (th_p, &ti); |
| 275 | if (err != TD_OK) |
| 276 | error ("Cannot get thread info: %s", thread_db_err_str (err)); |
| 277 | |
| 278 | if (ti.ti_lid == -1) |
| 279 | { |
| 280 | /* A thread with kernel thread ID -1 is either a thread that |
| 281 | exited and was joined, or a thread that is being created but |
| 282 | hasn't started yet, and that is reusing the tcb/stack of a |
| 283 | thread that previously exited and was joined. (glibc marks |
| 284 | terminated and joined threads with kernel thread ID -1. See |
| 285 | glibc PR17707. */ |
| 286 | if (debug_threads) |
| 287 | debug_printf ("thread_db: skipping exited and " |
| 288 | "joined thread (0x%lx)\n", |
| 289 | (unsigned long) ti.ti_tid); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* Check for zombies. */ |
| 294 | if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) |
| 295 | return 0; |
| 296 | |
| 297 | if (!maybe_attach_thread (th_p, &ti, (int *) data)) |
| 298 | { |
| 299 | /* Terminate iteration early: we might be looking at stale data in |
| 300 | the inferior. The thread_db_find_new_threads will retry. */ |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | thread_db_find_new_threads (void) |
| 309 | { |
| 310 | td_err_e err; |
| 311 | ptid_t ptid = current_ptid; |
| 312 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
| 313 | int loop, iteration; |
| 314 | |
| 315 | /* This function is only called when we first initialize thread_db. |
| 316 | First locate the initial thread. If it is not ready for |
| 317 | debugging yet, then stop. */ |
| 318 | if (find_one_thread (ptid) == 0) |
| 319 | return; |
| 320 | |
| 321 | /* Require 4 successive iterations which do not find any new threads. |
| 322 | The 4 is a heuristic: there is an inherent race here, and I have |
| 323 | seen that 2 iterations in a row are not always sufficient to |
| 324 | "capture" all threads. */ |
| 325 | for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) |
| 326 | { |
| 327 | int new_thread_count = 0; |
| 328 | |
| 329 | /* Iterate over all user-space threads to discover new threads. */ |
| 330 | err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, |
| 331 | find_new_threads_callback, |
| 332 | &new_thread_count, |
| 333 | TD_THR_ANY_STATE, |
| 334 | TD_THR_LOWEST_PRIORITY, |
| 335 | TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
| 336 | if (debug_threads) |
| 337 | debug_printf ("Found %d threads in iteration %d.\n", |
| 338 | new_thread_count, iteration); |
| 339 | |
| 340 | if (new_thread_count != 0) |
| 341 | { |
| 342 | /* Found new threads. Restart iteration from beginning. */ |
| 343 | loop = -1; |
| 344 | } |
| 345 | } |
| 346 | if (err != TD_OK) |
| 347 | error ("Cannot find new threads: %s", thread_db_err_str (err)); |
| 348 | } |
| 349 | |
| 350 | /* Cache all future symbols that thread_db might request. We can not |
| 351 | request symbols at arbitrary states in the remote protocol, only |
| 352 | when the client tells us that new symbols are available. So when |
| 353 | we load the thread library, make sure to check the entire list. */ |
| 354 | |
| 355 | static void |
| 356 | thread_db_look_up_symbols (void) |
| 357 | { |
| 358 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
| 359 | const char **sym_list; |
| 360 | CORE_ADDR unused; |
| 361 | |
| 362 | for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) |
| 363 | look_up_one_symbol (*sym_list, &unused, 1); |
| 364 | |
| 365 | /* We're not interested in any other libraries loaded after this |
| 366 | point, only in symbols in libpthread.so. */ |
| 367 | thread_db->all_symbols_looked_up = 1; |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) |
| 372 | { |
| 373 | struct thread_db *thread_db = current_process ()->priv->thread_db; |
| 374 | int may_ask_gdb = !thread_db->all_symbols_looked_up; |
| 375 | |
| 376 | /* If we've passed the call to thread_db_look_up_symbols, then |
| 377 | anything not in the cache must not exist; we're not interested |
| 378 | in any libraries loaded after that point, only in symbols in |
| 379 | libpthread.so. It might not be an appropriate time to look |
| 380 | up a symbol, e.g. while we're trying to fetch registers. */ |
| 381 | return look_up_one_symbol (name, addrp, may_ask_gdb); |
| 382 | } |
| 383 | |
| 384 | int |
| 385 | thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, |
| 386 | CORE_ADDR load_module, CORE_ADDR *address) |
| 387 | { |
| 388 | psaddr_t addr; |
| 389 | td_err_e err; |
| 390 | struct lwp_info *lwp; |
| 391 | struct thread_info *saved_thread; |
| 392 | struct process_info *proc; |
| 393 | struct thread_db *thread_db; |
| 394 | |
| 395 | proc = get_thread_process (thread); |
| 396 | thread_db = proc->priv->thread_db; |
| 397 | |
| 398 | /* If the thread layer is not (yet) initialized, fail. */ |
| 399 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) |
| 400 | return TD_ERR; |
| 401 | |
| 402 | /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase |
| 403 | could work. */ |
| 404 | if (thread_db->td_thr_tls_get_addr_p == NULL |
| 405 | || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) |
| 406 | return -1; |
| 407 | |
| 408 | lwp = get_thread_lwp (thread); |
| 409 | if (!lwp->thread_known) |
| 410 | find_one_thread (thread->id); |
| 411 | if (!lwp->thread_known) |
| 412 | return TD_NOTHR; |
| 413 | |
| 414 | saved_thread = current_thread; |
| 415 | current_thread = thread; |
| 416 | |
| 417 | if (load_module != 0) |
| 418 | { |
| 419 | /* Note the cast through uintptr_t: this interface only works if |
| 420 | a target address fits in a psaddr_t, which is a host pointer. |
| 421 | So a 32-bit debugger can not access 64-bit TLS through this. */ |
| 422 | err = thread_db->td_thr_tls_get_addr_p (&lwp->th, |
| 423 | (psaddr_t) (uintptr_t) load_module, |
| 424 | offset, &addr); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | /* This code path handles the case of -static -pthread executables: |
| 429 | https://sourceware.org/ml/libc-help/2014-03/msg00024.html |
| 430 | For older GNU libc r_debug.r_map is NULL. For GNU libc after |
| 431 | PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. |
| 432 | The constant number 1 depends on GNU __libc_setup_tls |
| 433 | initialization of l_tls_modid to 1. */ |
| 434 | err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); |
| 435 | addr = (char *) addr + offset; |
| 436 | } |
| 437 | |
| 438 | current_thread = saved_thread; |
| 439 | if (err == TD_OK) |
| 440 | { |
| 441 | *address = (CORE_ADDR) (uintptr_t) addr; |
| 442 | return 0; |
| 443 | } |
| 444 | else |
| 445 | return err; |
| 446 | } |
| 447 | |
| 448 | /* See linux-low.h. */ |
| 449 | |
| 450 | bool |
| 451 | thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len) |
| 452 | { |
| 453 | struct thread_db *thread_db; |
| 454 | struct lwp_info *lwp; |
| 455 | struct thread_info *thread |
| 456 | = (struct thread_info *) find_inferior_id (&all_threads, ptid); |
| 457 | |
| 458 | if (thread == NULL) |
| 459 | return false; |
| 460 | |
| 461 | thread_db = get_thread_process (thread)->priv->thread_db; |
| 462 | |
| 463 | if (thread_db == NULL) |
| 464 | return false; |
| 465 | |
| 466 | lwp = get_thread_lwp (thread); |
| 467 | |
| 468 | if (!lwp->thread_known && !find_one_thread (thread->id)) |
| 469 | return false; |
| 470 | |
| 471 | gdb_assert (lwp->thread_known); |
| 472 | |
| 473 | *handle = (gdb_byte *) &lwp->thread_handle; |
| 474 | *handle_len = sizeof (lwp->thread_handle); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | #ifdef USE_LIBTHREAD_DB_DIRECTLY |
| 479 | |
| 480 | static int |
| 481 | thread_db_load_search (void) |
| 482 | { |
| 483 | td_err_e err; |
| 484 | struct thread_db *tdb; |
| 485 | struct process_info *proc = current_process (); |
| 486 | |
| 487 | gdb_assert (proc->priv->thread_db == NULL); |
| 488 | |
| 489 | tdb = XCNEW (struct thread_db); |
| 490 | proc->priv->thread_db = tdb; |
| 491 | |
| 492 | tdb->td_ta_new_p = &td_ta_new; |
| 493 | |
| 494 | /* Attempt to open a connection to the thread library. */ |
| 495 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
| 496 | if (err != TD_OK) |
| 497 | { |
| 498 | if (debug_threads) |
| 499 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
| 500 | free (tdb); |
| 501 | proc->priv->thread_db = NULL; |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; |
| 506 | tdb->td_thr_get_info_p = &td_thr_get_info; |
| 507 | tdb->td_ta_thr_iter_p = &td_ta_thr_iter; |
| 508 | tdb->td_symbol_list_p = &td_symbol_list; |
| 509 | |
| 510 | /* These are not essential. */ |
| 511 | tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; |
| 512 | tdb->td_thr_tlsbase_p = &td_thr_tlsbase; |
| 513 | |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | #else |
| 518 | |
| 519 | static int |
| 520 | try_thread_db_load_1 (void *handle) |
| 521 | { |
| 522 | td_err_e err; |
| 523 | struct thread_db *tdb; |
| 524 | struct process_info *proc = current_process (); |
| 525 | |
| 526 | gdb_assert (proc->priv->thread_db == NULL); |
| 527 | |
| 528 | tdb = XCNEW (struct thread_db); |
| 529 | proc->priv->thread_db = tdb; |
| 530 | |
| 531 | tdb->handle = handle; |
| 532 | |
| 533 | /* Initialize pointers to the dynamic library functions we will use. |
| 534 | Essential functions first. */ |
| 535 | |
| 536 | #define CHK(required, a) \ |
| 537 | do \ |
| 538 | { \ |
| 539 | if ((a) == NULL) \ |
| 540 | { \ |
| 541 | if (debug_threads) \ |
| 542 | debug_printf ("dlsym: %s\n", dlerror ()); \ |
| 543 | if (required) \ |
| 544 | { \ |
| 545 | free (tdb); \ |
| 546 | proc->priv->thread_db = NULL; \ |
| 547 | return 0; \ |
| 548 | } \ |
| 549 | } \ |
| 550 | } \ |
| 551 | while (0) |
| 552 | |
| 553 | #define TDB_DLSYM(tdb, func) \ |
| 554 | tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func) |
| 555 | |
| 556 | CHK (1, TDB_DLSYM (tdb, td_ta_new)); |
| 557 | |
| 558 | /* Attempt to open a connection to the thread library. */ |
| 559 | err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); |
| 560 | if (err != TD_OK) |
| 561 | { |
| 562 | if (debug_threads) |
| 563 | debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err)); |
| 564 | free (tdb); |
| 565 | proc->priv->thread_db = NULL; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr)); |
| 570 | CHK (1, TDB_DLSYM (tdb, td_thr_get_info)); |
| 571 | CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter)); |
| 572 | CHK (1, TDB_DLSYM (tdb, td_symbol_list)); |
| 573 | |
| 574 | /* These are not essential. */ |
| 575 | CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr)); |
| 576 | CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase)); |
| 577 | |
| 578 | #undef CHK |
| 579 | #undef TDB_DLSYM |
| 580 | |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | #ifdef HAVE_DLADDR |
| 585 | |
| 586 | /* Lookup a library in which given symbol resides. |
| 587 | Note: this is looking in the GDBSERVER process, not in the inferior. |
| 588 | Returns library name, or NULL. */ |
| 589 | |
| 590 | static const char * |
| 591 | dladdr_to_soname (const void *addr) |
| 592 | { |
| 593 | Dl_info info; |
| 594 | |
| 595 | if (dladdr (addr, &info) != 0) |
| 596 | return info.dli_fname; |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | #endif |
| 601 | |
| 602 | static int |
| 603 | try_thread_db_load (const char *library) |
| 604 | { |
| 605 | void *handle; |
| 606 | |
| 607 | if (debug_threads) |
| 608 | debug_printf ("Trying host libthread_db library: %s.\n", |
| 609 | library); |
| 610 | handle = dlopen (library, RTLD_NOW); |
| 611 | if (handle == NULL) |
| 612 | { |
| 613 | if (debug_threads) |
| 614 | debug_printf ("dlopen failed: %s.\n", dlerror ()); |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | #ifdef HAVE_DLADDR |
| 619 | if (debug_threads && strchr (library, '/') == NULL) |
| 620 | { |
| 621 | void *td_init; |
| 622 | |
| 623 | td_init = dlsym (handle, "td_init"); |
| 624 | if (td_init != NULL) |
| 625 | { |
| 626 | const char *const libpath = dladdr_to_soname (td_init); |
| 627 | |
| 628 | if (libpath != NULL) |
| 629 | debug_printf ("Host %s resolved to: %s.\n", library, libpath); |
| 630 | } |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | if (try_thread_db_load_1 (handle)) |
| 635 | return 1; |
| 636 | |
| 637 | /* This library "refused" to work on current inferior. */ |
| 638 | dlclose (handle); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /* Handle $sdir in libthread-db-search-path. |
| 643 | Look for libthread_db in the system dirs, or wherever a plain |
| 644 | dlopen(file_without_path) will look. |
| 645 | The result is true for success. */ |
| 646 | |
| 647 | static int |
| 648 | try_thread_db_load_from_sdir (void) |
| 649 | { |
| 650 | return try_thread_db_load (LIBTHREAD_DB_SO); |
| 651 | } |
| 652 | |
| 653 | /* Try to load libthread_db from directory DIR of length DIR_LEN. |
| 654 | The result is true for success. */ |
| 655 | |
| 656 | static int |
| 657 | try_thread_db_load_from_dir (const char *dir, size_t dir_len) |
| 658 | { |
| 659 | char path[PATH_MAX]; |
| 660 | |
| 661 | if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) |
| 662 | { |
| 663 | char *cp = (char *) xmalloc (dir_len + 1); |
| 664 | |
| 665 | memcpy (cp, dir, dir_len); |
| 666 | cp[dir_len] = '\0'; |
| 667 | warning (_("libthread-db-search-path component too long," |
| 668 | " ignored: %s."), cp); |
| 669 | free (cp); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | memcpy (path, dir, dir_len); |
| 674 | path[dir_len] = '/'; |
| 675 | strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); |
| 676 | return try_thread_db_load (path); |
| 677 | } |
| 678 | |
| 679 | /* Search libthread_db_search_path for libthread_db which "agrees" |
| 680 | to work on current inferior. |
| 681 | The result is true for success. */ |
| 682 | |
| 683 | static int |
| 684 | thread_db_load_search (void) |
| 685 | { |
| 686 | VEC (char_ptr) *dir_vec; |
| 687 | char *this_dir; |
| 688 | int i, rc = 0; |
| 689 | |
| 690 | if (libthread_db_search_path == NULL) |
| 691 | libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); |
| 692 | |
| 693 | dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path); |
| 694 | |
| 695 | for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i) |
| 696 | { |
| 697 | const int pdir_len = sizeof ("$pdir") - 1; |
| 698 | size_t this_dir_len; |
| 699 | |
| 700 | this_dir_len = strlen (this_dir); |
| 701 | |
| 702 | if (strncmp (this_dir, "$pdir", pdir_len) == 0 |
| 703 | && (this_dir[pdir_len] == '\0' |
| 704 | || this_dir[pdir_len] == '/')) |
| 705 | { |
| 706 | /* We don't maintain a list of loaded libraries so we don't know |
| 707 | where libpthread lives. We *could* fetch the info, but we don't |
| 708 | do that yet. Ignore it. */ |
| 709 | } |
| 710 | else if (strcmp (this_dir, "$sdir") == 0) |
| 711 | { |
| 712 | if (try_thread_db_load_from_sdir ()) |
| 713 | { |
| 714 | rc = 1; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | if (try_thread_db_load_from_dir (this_dir, this_dir_len)) |
| 721 | { |
| 722 | rc = 1; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | free_char_ptr_vec (dir_vec); |
| 729 | if (debug_threads) |
| 730 | debug_printf ("thread_db_load_search returning %d\n", rc); |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
| 735 | |
| 736 | int |
| 737 | thread_db_init (void) |
| 738 | { |
| 739 | struct process_info *proc = current_process (); |
| 740 | |
| 741 | /* FIXME drow/2004-10-16: This is the "overall process ID", which |
| 742 | GNU/Linux calls tgid, "thread group ID". When we support |
| 743 | attaching to threads, the original thread may not be the correct |
| 744 | thread. We would have to get the process ID from /proc for NPTL. |
| 745 | |
| 746 | This isn't the only place in gdbserver that assumes that the first |
| 747 | process in the list is the thread group leader. */ |
| 748 | |
| 749 | if (thread_db_load_search ()) |
| 750 | { |
| 751 | /* It's best to avoid td_ta_thr_iter if possible. That walks |
| 752 | data structures in the inferior's address space that may be |
| 753 | corrupted, or, if the target is running, the list may change |
| 754 | while we walk it. In the latter case, it's possible that a |
| 755 | thread exits just at the exact time that causes GDBserver to |
| 756 | get stuck in an infinite loop. As the kernel supports clone |
| 757 | events and /proc/PID/task/ exists, then we already know about |
| 758 | all threads in the process. When we need info out of |
| 759 | thread_db on a given thread (e.g., for TLS), we'll use |
| 760 | find_one_thread then. That uses thread_db entry points that |
| 761 | do not walk libpthread's thread list, so should be safe, as |
| 762 | well as more efficient. */ |
| 763 | if (!linux_proc_task_list_dir_exists (pid_of (proc))) |
| 764 | thread_db_find_new_threads (); |
| 765 | thread_db_look_up_symbols (); |
| 766 | return 1; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static void |
| 773 | switch_to_process (struct process_info *proc) |
| 774 | { |
| 775 | int pid = pid_of (proc); |
| 776 | |
| 777 | current_thread = find_any_thread_of_pid (pid); |
| 778 | } |
| 779 | |
| 780 | /* Disconnect from libthread_db and free resources. */ |
| 781 | |
| 782 | static void |
| 783 | disable_thread_event_reporting (struct process_info *proc) |
| 784 | { |
| 785 | struct thread_db *thread_db = proc->priv->thread_db; |
| 786 | if (thread_db) |
| 787 | { |
| 788 | td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, |
| 789 | td_thr_events_t *event); |
| 790 | |
| 791 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
| 792 | td_ta_clear_event_p |
| 793 | = (td_ta_clear_event_ftype *) dlsym (thread_db->handle, |
| 794 | "td_ta_clear_event"); |
| 795 | #else |
| 796 | td_ta_clear_event_p = &td_ta_clear_event; |
| 797 | #endif |
| 798 | |
| 799 | if (td_ta_clear_event_p != NULL) |
| 800 | { |
| 801 | struct thread_info *saved_thread = current_thread; |
| 802 | td_thr_events_t events; |
| 803 | |
| 804 | switch_to_process (proc); |
| 805 | |
| 806 | /* Set the process wide mask saying we aren't interested |
| 807 | in any events anymore. */ |
| 808 | td_event_fillset (&events); |
| 809 | (*td_ta_clear_event_p) (thread_db->thread_agent, &events); |
| 810 | |
| 811 | current_thread = saved_thread; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | thread_db_detach (struct process_info *proc) |
| 818 | { |
| 819 | struct thread_db *thread_db = proc->priv->thread_db; |
| 820 | |
| 821 | if (thread_db) |
| 822 | { |
| 823 | disable_thread_event_reporting (proc); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* Disconnect from libthread_db and free resources. */ |
| 828 | |
| 829 | void |
| 830 | thread_db_mourn (struct process_info *proc) |
| 831 | { |
| 832 | struct thread_db *thread_db = proc->priv->thread_db; |
| 833 | if (thread_db) |
| 834 | { |
| 835 | td_ta_delete_ftype *td_ta_delete_p; |
| 836 | |
| 837 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
| 838 | td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete"); |
| 839 | #else |
| 840 | td_ta_delete_p = &td_ta_delete; |
| 841 | #endif |
| 842 | |
| 843 | if (td_ta_delete_p != NULL) |
| 844 | (*td_ta_delete_p) (thread_db->thread_agent); |
| 845 | |
| 846 | #ifndef USE_LIBTHREAD_DB_DIRECTLY |
| 847 | dlclose (thread_db->handle); |
| 848 | #endif /* USE_LIBTHREAD_DB_DIRECTLY */ |
| 849 | |
| 850 | free (thread_db); |
| 851 | proc->priv->thread_db = NULL; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* Handle "set libthread-db-search-path" monitor command and return 1. |
| 856 | For any other command, return 0. */ |
| 857 | |
| 858 | int |
| 859 | thread_db_handle_monitor_command (char *mon) |
| 860 | { |
| 861 | const char *cmd = "set libthread-db-search-path"; |
| 862 | size_t cmd_len = strlen (cmd); |
| 863 | |
| 864 | if (strncmp (mon, cmd, cmd_len) == 0 |
| 865 | && (mon[cmd_len] == '\0' |
| 866 | || mon[cmd_len] == ' ')) |
| 867 | { |
| 868 | const char *cp = mon + cmd_len; |
| 869 | |
| 870 | if (libthread_db_search_path != NULL) |
| 871 | free (libthread_db_search_path); |
| 872 | |
| 873 | /* Skip leading space (if any). */ |
| 874 | while (isspace (*cp)) |
| 875 | ++cp; |
| 876 | |
| 877 | if (*cp == '\0') |
| 878 | cp = LIBTHREAD_DB_SEARCH_PATH; |
| 879 | libthread_db_search_path = xstrdup (cp); |
| 880 | |
| 881 | monitor_output ("libthread-db-search-path set to `"); |
| 882 | monitor_output (libthread_db_search_path); |
| 883 | monitor_output ("'\n"); |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | /* Tell server.c to perform default processing. */ |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /* See linux-low.h. */ |
| 892 | |
| 893 | void |
| 894 | thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid) |
| 895 | { |
| 896 | process_info *parent_proc = get_thread_process (parent_thr); |
| 897 | struct thread_db *thread_db = parent_proc->priv->thread_db; |
| 898 | |
| 899 | /* If the thread layer isn't initialized, return. It may just |
| 900 | be that the program uses clone, but does not use libthread_db. */ |
| 901 | if (thread_db == NULL || !thread_db->all_symbols_looked_up) |
| 902 | return; |
| 903 | |
| 904 | /* find_one_thread calls into libthread_db which accesses memory via |
| 905 | the current thread. Temporarily switch to a thread we know is |
| 906 | stopped. */ |
| 907 | scoped_restore restore_current_thread |
| 908 | = make_scoped_restore (¤t_thread, parent_thr); |
| 909 | |
| 910 | if (!find_one_thread (child_ptid)) |
| 911 | warning ("Cannot find thread after clone.\n"); |
| 912 | } |