Most allocation functions (if not all) return a void* pointing to the allocated memory. In C++, we need to add an explicit cast when assigning the result to a pointer to another type (which is the case more often than not). The content of this patch is taken from Pedro's branch, from commit "(mostly) auto-generated patch to insert casts needed for C++". I validated that the changes make sense and manually reflowed the code to make it respect the coding style. I also found multiple places where I could use XNEW/XNEWVEC/XRESIZEVEC/etc. Thanks a lot to whoever did that automated script to insert casts, doing it completely by hand would have taken a ridiculous amount of time. Only files built on x86 with --enable-targets=all are modified. This means that all other -nat.c files are untouched and will have to be dealt with later by using appropiate compilers. Or maybe we can try to build them with a regular g++ just to know where to add casts, I don't know. I built-tested this with --enable-targets=all and reg-tested. Here's the changelog entry, which was not too bad to make despite the size, thanks to David Malcom's script. I fixed some bits by hand, but there might be some wrong parts left (hopefully not). gdb/ChangeLog: * aarch64-linux-tdep.c (aarch64_stap_parse_special_token): Add cast to allocation result assignment. * ada-exp.y (write_object_renaming): Likewise. (write_ambiguous_var): Likewise. (ada_nget_field_index): Likewise. (write_var_or_type): Likewise. * ada-lang.c (ada_decode_symbol): Likewise. (ada_value_assign): Likewise. (value_pointer): Likewise. (cache_symbol): Likewise. (add_nonlocal_symbols): Likewise. (ada_name_for_lookup): Likewise. (symbol_completion_add): Likewise. (ada_to_fixed_type_1): Likewise. (ada_get_next_arg): Likewise. (defns_collected): Likewise. * ada-lex.l (processId): Likewise. (processString): Likewise. * ada-tasks.c (read_known_tasks_array): Likewise. (read_known_tasks_list): Likewise. * ada-typeprint.c (decoded_type_name): Likewise. * addrmap.c (addrmap_mutable_create_fixed): Likewise. * amd64-tdep.c (amd64_push_arguments): Likewise. (amd64_displaced_step_copy_insn): Likewise. (amd64_classify_insn_at): Likewise. (amd64_relocate_instruction): Likewise. * amd64obsd-tdep.c (amd64obsd_sigtramp_p): Likewise. * arch-utils.c (simple_displaced_step_copy_insn): Likewise. (initialize_current_architecture): Likewise. * arm-linux-tdep.c (arm_stap_parse_special_token): Likewise. * arm-symbian-tdep.c (arm_symbian_osabi_sniffer): Likewise. * arm-tdep.c (arm_exidx_new_objfile): Likewise. (arm_push_dummy_call): Likewise. (extend_buffer_earlier): Likewise. (arm_adjust_breakpoint_address): Likewise. (arm_skip_stub): Likewise. * auto-load.c (filename_is_in_pattern): Likewise. (maybe_add_script_file): Likewise. (maybe_add_script_text): Likewise. (auto_load_objfile_script_1): Likewise. * auxv.c (ld_so_xfer_auxv): Likewise. * ax-general.c (new_agent_expr): Likewise. (grow_expr): Likewise. (ax_reg_mask): Likewise. * bcache.c (bcache_full): Likewise. * breakpoint.c (program_breakpoint_here_p): Likewise. * btrace.c (parse_xml_raw): Likewise. * build-id.c (build_id_to_debug_bfd): Likewise. * buildsym.c (end_symtab_with_blockvector): Likewise. * c-exp.y (string_exp): Likewise. (qualified_name): Likewise. (write_destructor_name): Likewise. (operator_stoken): Likewise. (parse_number): Likewise. (scan_macro_expansion): Likewise. (yylex): Likewise. (c_print_token): Likewise. * c-lang.c (c_get_string): Likewise. (emit_numeric_character): Likewise. * charset.c (wchar_iterate): Likewise. * cli/cli-cmds.c (complete_command): Likewise. (make_command): Likewise. * cli/cli-dump.c (restore_section_callback): Likewise. (restore_binary_file): Likewise. * cli/cli-interp.c (cli_interpreter_exec): Likewise. * cli/cli-script.c (execute_control_command): Likewise. * cli/cli-setshow.c (do_set_command): Likewise. * coff-pe-read.c (add_pe_forwarded_sym): Likewise. (read_pe_exported_syms): Likewise. * coffread.c (coff_read_struct_type): Likewise. (coff_read_enum_type): Likewise. * common/btrace-common.c (btrace_data_append): Likewise. * common/buffer.c (buffer_grow): Likewise. * common/filestuff.c (gdb_fopen_cloexec): Likewise. * common/format.c (parse_format_string): Likewise. * common/gdb_vecs.c (delim_string_to_char_ptr_vec_append): Likewise. * common/xml-utils.c (xml_escape_text): Likewise. * compile/compile-object-load.c (copy_sections): Likewise. (compile_object_load): Likewise. * compile/compile-object-run.c (compile_object_run): Likewise. * completer.c (filename_completer): Likewise. * corefile.c (read_memory_typed_address): Likewise. (write_memory_unsigned_integer): Likewise. (write_memory_signed_integer): Likewise. (complete_set_gnutarget): Likewise. * corelow.c (get_core_register_section): Likewise. * cp-name-parser.y (d_grab): Likewise. (allocate_info): Likewise. (cp_new_demangle_parse_info): Likewise. * cp-namespace.c (cp_scan_for_anonymous_namespaces): Likewise. (cp_lookup_symbol_in_namespace): Likewise. (lookup_namespace_scope): Likewise. (find_symbol_in_baseclass): Likewise. (cp_lookup_nested_symbol): Likewise. (cp_lookup_transparent_type_loop): Likewise. * cp-support.c (copy_string_to_obstack): Likewise. (make_symbol_overload_list): Likewise. (make_symbol_overload_list_namespace): Likewise. (make_symbol_overload_list_adl_namespace): Likewise. (first_component_command): Likewise. * cp-valprint.c (cp_print_value): Likewise. * ctf.c (ctf_xfer_partial): Likewise. * d-exp.y (StringExp): Likewise. * d-namespace.c (d_lookup_symbol_in_module): Likewise. (lookup_module_scope): Likewise. (find_symbol_in_baseclass): Likewise. (d_lookup_nested_symbol): Likewise. * dbxread.c (find_stab_function_addr): Likewise. (read_dbx_symtab): Likewise. (dbx_end_psymtab): Likewise. (cp_set_block_scope): Likewise. * dcache.c (dcache_alloc): Likewise. * demangle.c (_initialize_demangler): Likewise. * dicos-tdep.c (dicos_load_module_p): Likewise. * dictionary.c (dict_create_hashed_expandable): Likewise. (dict_create_linear_expandable): Likewise. (expand_hashtable): Likewise. (add_symbol_linear_expandable): Likewise. * dwarf2-frame.c (add_cie): Likewise. (add_fde): Likewise. (dwarf2_build_frame_info): Likewise. * dwarf2expr.c (dwarf_expr_grow_stack): Likewise. (dwarf_expr_fetch_address): Likewise. (add_piece): Likewise. (execute_stack_op): Likewise. * dwarf2loc.c (chain_candidate): Likewise. (dwarf_entry_parameter_to_value): Likewise. (read_pieced_value): Likewise. (write_pieced_value): Likewise. * dwarf2read.c (dwarf2_read_section): Likewise. (add_type_unit): Likewise. (read_comp_units_from_section): Likewise. (fixup_go_packaging): Likewise. (dwarf2_compute_name): Likewise. (dwarf2_physname): Likewise. (create_dwo_unit_in_dwp_v1): Likewise. (create_dwo_unit_in_dwp_v2): Likewise. (read_func_scope): Likewise. (read_call_site_scope): Likewise. (dwarf2_attach_fields_to_type): Likewise. (process_structure_scope): Likewise. (mark_common_block_symbol_computed): Likewise. (read_common_block): Likewise. (abbrev_table_read_table): Likewise. (guess_partial_die_structure_name): Likewise. (fixup_partial_die): Likewise. (add_file_name): Likewise. (dwarf2_const_value_data): Likewise. (dwarf2_const_value_attr): Likewise. (build_error_marker_type): Likewise. (guess_full_die_structure_name): Likewise. (anonymous_struct_prefix): Likewise. (typename_concat): Likewise. (dwarf2_canonicalize_name): Likewise. (dwarf2_name): Likewise. (write_constant_as_bytes): Likewise. (dwarf2_fetch_constant_bytes): Likewise. (copy_string): Likewise. (parse_macro_definition): Likewise. * elfread.c (elf_symfile_segments): Likewise. (elf_rel_plt_read): Likewise. (elf_gnu_ifunc_resolve_by_cache): Likewise. (elf_gnu_ifunc_resolve_by_got): Likewise. (elf_read_minimal_symbols): Likewise. (elf_gnu_ifunc_record_cache): Likewise. * event-top.c (top_level_prompt): Likewise. (command_line_handler): Likewise. * exec.c (resize_section_table): Likewise. * expprint.c (print_subexp_standard): Likewise. * fbsd-tdep.c (fbsd_collect_regset_section_cb): Likewise. * findcmd.c (parse_find_args): Likewise. * findvar.c (address_from_register): Likewise. * frame.c (get_prev_frame_always): Likewise. * gdb_bfd.c (gdb_bfd_ref): Likewise. (get_section_descriptor): Likewise. * gdb_obstack.c (obconcat): Likewise. (obstack_strdup): Likewise. * gdbtypes.c (lookup_function_type_with_arguments): Likewise. (create_set_type): Likewise. (lookup_unsigned_typename): Likewise. (lookup_signed_typename): Likewise. (resolve_dynamic_union): Likewise. (resolve_dynamic_struct): Likewise. (add_dyn_prop): Likewise. (copy_dynamic_prop_list): Likewise. (arch_flags_type): Likewise. (append_composite_type_field_raw): Likewise. * gdbtypes.h (INIT_FUNC_SPECIFIC): Likewise. * gnu-v3-abi.c (gnuv3_rtti_type): Likewise. * go-exp.y (string_exp): Likewise. * go-lang.c (go_demangle): Likewise. * guile/guile.c (compute_scheme_string): Likewise. * guile/scm-cmd.c (gdbscm_parse_command_name): Likewise. (gdbscm_canonicalize_command_name): Likewise. * guile/scm-ports.c (ioscm_init_stdio_buffers): Likewise. (ioscm_init_memory_port): Likewise. (ioscm_reinit_memory_port): Likewise. * guile/scm-utils.c (gdbscm_gc_xstrdup): Likewise. (gdbscm_gc_dup_argv): Likewise. * h8300-tdep.c (h8300_push_dummy_call): Likewise. * hppa-tdep.c (internalize_unwinds): Likewise. (read_unwind_info): Likewise. * i386-cygwin-tdep.c (core_process_module_section): Likewise. (windows_core_xfer_shared_libraries): Likewise. * i386-tdep.c (i386_displaced_step_copy_insn): Likewise. (i386_stap_parse_special_token_triplet): Likewise. (i386_stap_parse_special_token_three_arg_disp): Likewise. * i386obsd-tdep.c (i386obsd_sigtramp_p): Likewise. * inf-child.c (inf_child_fileio_readlink): Likewise. * inf-ptrace.c (inf_ptrace_fetch_register): Likewise. (inf_ptrace_store_register): Likewise. * infrun.c (follow_exec): Likewise. (displaced_step_prepare_throw): Likewise. (save_stop_context): Likewise. (save_infcall_suspend_state): Likewise. * jit.c (jit_read_descriptor): Likewise. (jit_read_code_entry): Likewise. (jit_symtab_line_mapping_add_impl): Likewise. (finalize_symtab): Likewise. (jit_unwind_reg_get_impl): Likewise. * jv-exp.y (QualifiedName): Likewise. * jv-lang.c (get_java_utf8_name): Likewise. (type_from_class): Likewise. (java_demangle_type_signature): Likewise. (java_class_name_from_physname): Likewise. * jv-typeprint.c (java_type_print_base): Likewise. * jv-valprint.c (java_value_print): Likewise. * language.c (add_language): Likewise. * linespec.c (add_sal_to_sals_basic): Likewise. (add_sal_to_sals): Likewise. (decode_objc): Likewise. (find_linespec_symbols): Likewise. * linux-fork.c (fork_save_infrun_state): Likewise. * linux-nat.c (linux_nat_detach): Likewise. (linux_nat_fileio_readlink): Likewise. * linux-record.c (record_linux_sockaddr): Likewise. (record_linux_msghdr): Likewise. (Do): Likewise. * linux-tdep.c (linux_core_info_proc_mappings): Likewise. (linux_collect_regset_section_cb): Likewise. (linux_get_siginfo_data): Likewise. * linux-thread-db.c (try_thread_db_load_from_pdir_1): Likewise. (try_thread_db_load_from_dir): Likewise. (thread_db_load_search): Likewise. (info_auto_load_libthread_db): Likewise. * m32c-tdep.c (m32c_m16c_address_to_pointer): Likewise. (m32c_m16c_pointer_to_address): Likewise. * m68hc11-tdep.c (m68hc11_pseudo_register_write): Likewise. * m68k-tdep.c (m68k_get_longjmp_target): Likewise. * machoread.c (macho_check_dsym): Likewise. * macroexp.c (resize_buffer): Likewise. (gather_arguments): Likewise. (maybe_expand): Likewise. * macrotab.c (new_macro_key): Likewise. (new_source_file): Likewise. (new_macro_definition): Likewise. * mdebugread.c (parse_symbol): Likewise. (parse_type): Likewise. (parse_partial_symbols): Likewise. (psymtab_to_symtab_1): Likewise. * mem-break.c (default_memory_insert_breakpoint): Likewise. * mi/mi-cmd-break.c (mi_argv_to_format): Likewise. * mi/mi-main.c (mi_cmd_data_read_memory): Likewise. (mi_cmd_data_read_memory_bytes): Likewise. (mi_cmd_data_write_memory_bytes): Likewise. (mi_cmd_trace_frame_collected): Likewise. * mi/mi-parse.c (mi_parse_argv): Likewise. (mi_parse): Likewise. * minidebug.c (lzma_open): Likewise. (lzma_pread): Likewise. * mips-tdep.c (mips_read_fp_register_single): Likewise. (mips_print_fp_register): Likewise. * mipsnbsd-tdep.c (mipsnbsd_get_longjmp_target): Likewise. * mipsread.c (read_alphacoff_dynamic_symtab): Likewise. * mt-tdep.c (mt_register_name): Likewise. (mt_registers_info): Likewise. (mt_push_dummy_call): Likewise. * namespace.c (add_using_directive): Likewise. * nat/linux-btrace.c (perf_event_read): Likewise. (linux_enable_bts): Likewise. * nat/linux-osdata.c (linux_common_core_of_thread): Likewise. * nat/linux-ptrace.c (linux_ptrace_test_ret_to_nx): Likewise. * nto-tdep.c (nto_find_and_open_solib): Likewise. (nto_parse_redirection): Likewise. * objc-lang.c (objc_demangle): Likewise. (find_methods): Likewise. * objfiles.c (get_objfile_bfd_data): Likewise. (set_objfile_main_name): Likewise. (allocate_objfile): Likewise. (objfile_relocate): Likewise. (update_section_map): Likewise. * osabi.c (generic_elf_osabi_sniff_abi_tag_sections): Likewise. * p-exp.y (exp): Likewise. (yylex): Likewise. * p-valprint.c (pascal_object_print_value): Likewise. * parse.c (initialize_expout): Likewise. (mark_completion_tag): Likewise. (copy_name): Likewise. (parse_float): Likewise. (type_stack_reserve): Likewise. * ppc-linux-tdep.c (ppc_stap_parse_special_token): Likewise. (ppu2spu_prev_register): Likewise. * ppc-ravenscar-thread.c (supply_register_at_address): Likewise. * printcmd.c (printf_wide_c_string): Likewise. (printf_pointer): Likewise. * probe.c (parse_probes): Likewise. * python/py-cmd.c (gdbpy_parse_command_name): Likewise. (cmdpy_init): Likewise. * python/py-gdb-readline.c (gdbpy_readline_wrapper): Likewise. * python/py-symtab.c (set_sal): Likewise. * python/py-unwind.c (pyuw_sniffer): Likewise. * python/python.c (python_interactive_command): Likewise. (compute_python_string): Likewise. * ravenscar-thread.c (get_running_thread_id): Likewise. * record-full.c (record_full_exec_insn): Likewise. (record_full_core_open_1): Likewise. * regcache.c (regcache_raw_read_signed): Likewise. (regcache_raw_read_unsigned): Likewise. (regcache_cooked_read_signed): Likewise. (regcache_cooked_read_unsigned): Likewise. * remote-fileio.c (remote_fileio_func_open): Likewise. (remote_fileio_func_rename): Likewise. (remote_fileio_func_unlink): Likewise. (remote_fileio_func_stat): Likewise. (remote_fileio_func_system): Likewise. * remote-mips.c (mips_xfer_memory): Likewise. (mips_load_srec): Likewise. (pmon_end_download): Likewise. * remote.c (new_remote_state): Likewise. (map_regcache_remote_table): Likewise. (remote_register_number_and_offset): Likewise. (init_remote_state): Likewise. (get_memory_packet_size): Likewise. (remote_pass_signals): Likewise. (remote_program_signals): Likewise. (remote_start_remote): Likewise. (remote_check_symbols): Likewise. (remote_query_supported): Likewise. (extended_remote_attach): Likewise. (process_g_packet): Likewise. (store_registers_using_G): Likewise. (putpkt_binary): Likewise. (read_frame): Likewise. (compare_sections_command): Likewise. (remote_hostio_pread): Likewise. (remote_hostio_readlink): Likewise. (remote_file_put): Likewise. (remote_file_get): Likewise. (remote_pid_to_exec_file): Likewise. (_initialize_remote): Likewise. * rs6000-aix-tdep.c (rs6000_aix_ld_info_to_xml): Likewise. (rs6000_aix_core_xfer_shared_libraries_aix): Likewise. * rs6000-tdep.c (ppc_displaced_step_copy_insn): Likewise. (bfd_uses_spe_extensions): Likewise. * s390-linux-tdep.c (s390_displaced_step_copy_insn): Likewise. * score-tdep.c (score7_malloc_and_get_memblock): Likewise. * solib-dsbt.c (decode_loadmap): Likewise. (fetch_loadmap): Likewise. (scan_dyntag): Likewise. (enable_break): Likewise. (dsbt_relocate_main_executable): Likewise. * solib-frv.c (fetch_loadmap): Likewise. (enable_break2): Likewise. (frv_relocate_main_executable): Likewise. * solib-spu.c (spu_relocate_main_executable): Likewise. (spu_bfd_open): Likewise. * solib-svr4.c (lm_info_read): Likewise. (read_program_header): Likewise. (find_program_interpreter): Likewise. (scan_dyntag): Likewise. (elf_locate_base): Likewise. (open_symbol_file_object): Likewise. (read_program_headers_from_bfd): Likewise. (svr4_relocate_main_executable): Likewise. * solib-target.c (solib_target_relocate_section_addresses): Likewise. * solib.c (solib_find_1): Likewise. (exec_file_find): Likewise. (solib_find): Likewise. * source.c (openp): Likewise. (print_source_lines_base): Likewise. (forward_search_command): Likewise. * sparc-ravenscar-thread.c (supply_register_at_address): Likewise. * spu-tdep.c (spu2ppu_prev_register): Likewise. (spu_get_overlay_table): Likewise. * stabsread.c (patch_block_stabs): Likewise. (define_symbol): Likewise. (again:): Likewise. (read_member_functions): Likewise. (read_one_struct_field): Likewise. (read_enum_type): Likewise. (common_block_start): Likewise. * stack.c (read_frame_arg): Likewise. (backtrace_command): Likewise. * stap-probe.c (stap_parse_register_operand): Likewise. * symfile.c (syms_from_objfile_1): Likewise. (find_separate_debug_file): Likewise. (load_command): Likewise. (load_progress): Likewise. (load_section_callback): Likewise. (reread_symbols): Likewise. (add_filename_language): Likewise. (allocate_compunit_symtab): Likewise. (read_target_long_array): Likewise. (simple_read_overlay_table): Likewise. * symtab.c (symbol_set_names): Likewise. (resize_symbol_cache): Likewise. (rbreak_command): Likewise. (completion_list_add_name): Likewise. (completion_list_objc_symbol): Likewise. (add_filename_to_list): Likewise. * target-descriptions.c (maint_print_c_tdesc_cmd): Likewise. * target-memory.c (target_write_memory_blocks): Likewise. * target.c (target_read_string): Likewise. (read_whatever_is_readable): Likewise. (target_read_alloc_1): Likewise. (simple_search_memory): Likewise. (target_fileio_read_alloc_1): Likewise. * tilegx-tdep.c (tilegx_push_dummy_call): Likewise. * top.c (command_line_input): Likewise. * tracefile-tfile.c (tfile_fetch_registers): Likewise. * tracefile.c (tracefile_fetch_registers): Likewise. * tracepoint.c (add_memrange): Likewise. (init_collection_list): Likewise. (add_aexpr): Likewise. (trace_dump_actions): Likewise. (parse_trace_status): Likewise. (parse_tracepoint_definition): Likewise. (parse_tsv_definition): Likewise. (parse_static_tracepoint_marker_definition): Likewise. * tui/tui-file.c (tui_sfileopen): Likewise. (tui_file_adjust_strbuf): Likewise. * tui/tui-io.c (tui_expand_tabs): Likewise. * tui/tui-source.c (tui_set_source_content): Likewise. * typeprint.c (find_global_typedef): Likewise. * ui-file.c (do_ui_file_xstrdup): Likewise. (ui_file_obsavestring): Likewise. (mem_file_write): Likewise. * utils.c (make_hex_string): Likewise. (get_regcomp_error): Likewise. (puts_filtered_tabular): Likewise. (gdb_realpath_keepfile): Likewise. (ldirname): Likewise. (gdb_bfd_errmsg): Likewise. (substitute_path_component): Likewise. * valops.c (search_struct_method): Likewise. (find_oload_champ_namespace_loop): Likewise. * valprint.c (print_decimal_chars): Likewise. (read_string): Likewise. (generic_emit_char): Likewise. * varobj.c (varobj_delete): Likewise. (varobj_value_get_print_value): Likewise. * vaxobsd-tdep.c (vaxobsd_sigtramp_sniffer): Likewise. * windows-tdep.c (display_one_tib): Likewise. * xcoffread.c (read_xcoff_symtab): Likewise. (process_xcoff_symbol): Likewise. (swap_sym): Likewise. (scan_xcoff_symtab): Likewise. (xcoff_initial_scan): Likewise. * xml-support.c (gdb_xml_end_element): Likewise. (xml_process_xincludes): Likewise. (xml_fetch_content_from_file): Likewise. * xml-syscall.c (xml_list_of_syscalls): Likewise. * xstormy16-tdep.c (xstormy16_push_dummy_call): Likewise. gdb/gdbserver/ChangeLog: * ax.c (gdb_parse_agent_expr): Add cast to allocation result assignment. (gdb_unparse_agent_expr): Likewise. * hostio.c (require_data): Likewise. (handle_pread): Likewise. * linux-low.c (disable_regset): Likewise. (fetch_register): Likewise. (store_register): Likewise. (get_dynamic): Likewise. (linux_qxfer_libraries_svr4): Likewise. * mem-break.c (delete_fast_tracepoint_jump): Likewise. (set_fast_tracepoint_jump): Likewise. (uninsert_fast_tracepoint_jumps_at): Likewise. (reinsert_fast_tracepoint_jumps_at): Likewise. (validate_inserted_breakpoint): Likewise. (clone_agent_expr): Likewise. * regcache.c (init_register_cache): Likewise. * remote-utils.c (putpkt_binary_1): Likewise. (decode_M_packet): Likewise. (decode_X_packet): Likewise. (look_up_one_symbol): Likewise. (relocate_instruction): Likewise. (monitor_output): Likewise. * server.c (handle_search_memory): Likewise. (handle_qxfer_exec_file): Likewise. (handle_qxfer_libraries): Likewise. (handle_qxfer): Likewise. (handle_query): Likewise. (handle_v_cont): Likewise. (handle_v_run): Likewise. (captured_main): Likewise. * target.c (write_inferior_memory): Likewise. * thread-db.c (try_thread_db_load_from_dir): Likewise. * tracepoint.c (init_trace_buffer): Likewise. (add_tracepoint_action): Likewise. (add_traceframe): Likewise. (add_traceframe_block): Likewise. (cmd_qtdpsrc): Likewise. (cmd_qtdv): Likewise. (cmd_qtstatus): Likewise. (response_source): Likewise. (response_tsv): Likewise. (cmd_qtnotes): Likewise. (gdb_collect): Likewise. (initialize_tracepoint): Likewise.
1024 lines
27 KiB
C
1024 lines
27 KiB
C
/* Thread management interface, for the remote server for GDB.
|
|
Copyright (C) 2002-2015 Free Software Foundation, Inc.
|
|
|
|
Contributed by MontaVista Software.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#include "server.h"
|
|
|
|
#include "linux-low.h"
|
|
|
|
extern int debug_threads;
|
|
|
|
static int thread_db_use_events;
|
|
|
|
#include "gdb_proc_service.h"
|
|
#include "nat/gdb_thread_db.h"
|
|
#include "gdb_vecs.h"
|
|
#include "nat/linux-procfs.h"
|
|
|
|
#ifndef USE_LIBTHREAD_DB_DIRECTLY
|
|
#include <dlfcn.h>
|
|
#endif
|
|
#include <limits.h>
|
|
#include <ctype.h>
|
|
|
|
struct thread_db
|
|
{
|
|
/* Structure that identifies the child process for the
|
|
<proc_service.h> interface. */
|
|
struct ps_prochandle proc_handle;
|
|
|
|
/* Connection to the libthread_db library. */
|
|
td_thragent_t *thread_agent;
|
|
|
|
/* If this flag has been set, we've already asked GDB for all
|
|
symbols we might need; assume symbol cache misses are
|
|
failures. */
|
|
int all_symbols_looked_up;
|
|
|
|
#ifndef USE_LIBTHREAD_DB_DIRECTLY
|
|
/* Handle of the libthread_db from dlopen. */
|
|
void *handle;
|
|
#endif
|
|
|
|
/* Thread creation event breakpoint. The code at this location in
|
|
the child process will be called by the pthread library whenever
|
|
a new thread is created. By setting a special breakpoint at this
|
|
location, GDB can detect when a new thread is created. We obtain
|
|
this location via the td_ta_event_addr call. Note that if the
|
|
running kernel supports tracing clones, then we don't need to use
|
|
(and in fact don't use) this magic thread event breakpoint to
|
|
learn about threads. */
|
|
struct breakpoint *td_create_bp;
|
|
|
|
/* Addresses of libthread_db functions. */
|
|
td_ta_new_ftype *td_ta_new_p;
|
|
td_ta_event_getmsg_ftype * td_ta_event_getmsg_p;
|
|
td_ta_set_event_ftype *td_ta_set_event_p;
|
|
td_ta_event_addr_ftype *td_ta_event_addr_p;
|
|
td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
|
|
td_thr_get_info_ftype *td_thr_get_info_p;
|
|
td_thr_event_enable_ftype *td_thr_event_enable_p;
|
|
td_ta_thr_iter_ftype *td_ta_thr_iter_p;
|
|
td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
|
|
td_thr_tlsbase_ftype *td_thr_tlsbase_p;
|
|
td_symbol_list_ftype *td_symbol_list_p;
|
|
};
|
|
|
|
static char *libthread_db_search_path;
|
|
|
|
static int find_one_thread (ptid_t);
|
|
static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
|
|
|
|
static const char *
|
|
thread_db_err_str (td_err_e err)
|
|
{
|
|
static char buf[64];
|
|
|
|
switch (err)
|
|
{
|
|
case TD_OK:
|
|
return "generic 'call succeeded'";
|
|
case TD_ERR:
|
|
return "generic error";
|
|
case TD_NOTHR:
|
|
return "no thread to satisfy query";
|
|
case TD_NOSV:
|
|
return "no sync handle to satisfy query";
|
|
case TD_NOLWP:
|
|
return "no LWP to satisfy query";
|
|
case TD_BADPH:
|
|
return "invalid process handle";
|
|
case TD_BADTH:
|
|
return "invalid thread handle";
|
|
case TD_BADSH:
|
|
return "invalid synchronization handle";
|
|
case TD_BADTA:
|
|
return "invalid thread agent";
|
|
case TD_BADKEY:
|
|
return "invalid key";
|
|
case TD_NOMSG:
|
|
return "no event message for getmsg";
|
|
case TD_NOFPREGS:
|
|
return "FPU register set not available";
|
|
case TD_NOLIBTHREAD:
|
|
return "application not linked with libthread";
|
|
case TD_NOEVENT:
|
|
return "requested event is not supported";
|
|
case TD_NOCAPAB:
|
|
return "capability not available";
|
|
case TD_DBERR:
|
|
return "debugger service failed";
|
|
case TD_NOAPLIC:
|
|
return "operation not applicable to";
|
|
case TD_NOTSD:
|
|
return "no thread-specific data for this thread";
|
|
case TD_MALLOC:
|
|
return "malloc failed";
|
|
case TD_PARTIALREG:
|
|
return "only part of register set was written/read";
|
|
case TD_NOXREGS:
|
|
return "X register set not available for this thread";
|
|
#ifdef HAVE_TD_VERSION
|
|
case TD_VERSION:
|
|
return "version mismatch between libthread_db and libpthread";
|
|
#endif
|
|
default:
|
|
xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
|
|
return buf;
|
|
}
|
|
}
|
|
|
|
#if 0
|
|
static char *
|
|
thread_db_state_str (td_thr_state_e state)
|
|
{
|
|
static char buf[64];
|
|
|
|
switch (state)
|
|
{
|
|
case TD_THR_STOPPED:
|
|
return "stopped by debugger";
|
|
case TD_THR_RUN:
|
|
return "runnable";
|
|
case TD_THR_ACTIVE:
|
|
return "active";
|
|
case TD_THR_ZOMBIE:
|
|
return "zombie";
|
|
case TD_THR_SLEEP:
|
|
return "sleeping";
|
|
case TD_THR_STOPPED_ASLEEP:
|
|
return "stopped by debugger AND blocked";
|
|
default:
|
|
xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
|
|
return buf;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
static int
|
|
thread_db_create_event (CORE_ADDR where)
|
|
{
|
|
td_event_msg_t msg;
|
|
td_err_e err;
|
|
struct lwp_info *lwp;
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
|
|
gdb_assert (thread_db->td_ta_event_getmsg_p != NULL);
|
|
|
|
if (debug_threads)
|
|
debug_printf ("Thread creation event.\n");
|
|
|
|
/* FIXME: This assumes we don't get another event.
|
|
In the LinuxThreads implementation, this is safe,
|
|
because all events come from the manager thread
|
|
(except for its own creation, of course). */
|
|
err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
|
|
if (err != TD_OK)
|
|
fprintf (stderr, "thread getmsg err: %s\n",
|
|
thread_db_err_str (err));
|
|
|
|
/* If we do not know about the main thread yet, this would be a good time to
|
|
find it. We need to do this to pick up the main thread before any newly
|
|
created threads. */
|
|
lwp = get_thread_lwp (current_thread);
|
|
if (lwp->thread_known == 0)
|
|
find_one_thread (current_thread->entry.id);
|
|
|
|
/* msg.event == TD_EVENT_CREATE */
|
|
|
|
find_new_threads_callback (msg.th_p, NULL);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
thread_db_enable_reporting (void)
|
|
{
|
|
td_thr_events_t events;
|
|
td_notify_t notify;
|
|
td_err_e err;
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
|
|
if (thread_db->td_ta_set_event_p == NULL
|
|
|| thread_db->td_ta_event_addr_p == NULL
|
|
|| thread_db->td_ta_event_getmsg_p == NULL)
|
|
/* This libthread_db is missing required support. */
|
|
return 0;
|
|
|
|
/* Set the process wide mask saying which events we're interested in. */
|
|
td_event_emptyset (&events);
|
|
td_event_addset (&events, TD_CREATE);
|
|
|
|
err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
|
|
if (err != TD_OK)
|
|
{
|
|
warning ("Unable to set global thread event mask: %s",
|
|
thread_db_err_str (err));
|
|
return 0;
|
|
}
|
|
|
|
/* Get address for thread creation breakpoint. */
|
|
err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
|
|
¬ify);
|
|
if (err != TD_OK)
|
|
{
|
|
warning ("Unable to get location for thread creation breakpoint: %s",
|
|
thread_db_err_str (err));
|
|
return 0;
|
|
}
|
|
thread_db->td_create_bp
|
|
= set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
|
|
thread_db_create_event);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
find_one_thread (ptid_t ptid)
|
|
{
|
|
td_thrhandle_t th;
|
|
td_thrinfo_t ti;
|
|
td_err_e err;
|
|
struct thread_info *inferior;
|
|
struct lwp_info *lwp;
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
int lwpid = ptid_get_lwp (ptid);
|
|
|
|
inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
|
|
lwp = get_thread_lwp (inferior);
|
|
if (lwp->thread_known)
|
|
return 1;
|
|
|
|
/* Get information about this thread. */
|
|
err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
|
|
if (err != TD_OK)
|
|
error ("Cannot get thread handle for LWP %d: %s",
|
|
lwpid, thread_db_err_str (err));
|
|
|
|
err = thread_db->td_thr_get_info_p (&th, &ti);
|
|
if (err != TD_OK)
|
|
error ("Cannot get thread info for LWP %d: %s",
|
|
lwpid, thread_db_err_str (err));
|
|
|
|
if (debug_threads)
|
|
debug_printf ("Found thread %ld (LWP %d)\n",
|
|
ti.ti_tid, ti.ti_lid);
|
|
|
|
if (lwpid != ti.ti_lid)
|
|
{
|
|
warning ("PID mismatch! Expected %ld, got %ld",
|
|
(long) lwpid, (long) ti.ti_lid);
|
|
return 0;
|
|
}
|
|
|
|
if (thread_db_use_events)
|
|
{
|
|
err = thread_db->td_thr_event_enable_p (&th, 1);
|
|
if (err != TD_OK)
|
|
error ("Cannot enable thread event reporting for %d: %s",
|
|
ti.ti_lid, thread_db_err_str (err));
|
|
}
|
|
|
|
/* If the new thread ID is zero, a final thread ID will be available
|
|
later. Do not enable thread debugging yet. */
|
|
if (ti.ti_tid == 0)
|
|
return 0;
|
|
|
|
lwp->thread_known = 1;
|
|
lwp->th = th;
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* Attach a thread. Return true on success. */
|
|
|
|
static int
|
|
attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
|
|
{
|
|
struct process_info *proc = current_process ();
|
|
int pid = pid_of (proc);
|
|
ptid_t ptid = ptid_build (pid, ti_p->ti_lid, 0);
|
|
struct lwp_info *lwp;
|
|
int err;
|
|
|
|
if (debug_threads)
|
|
debug_printf ("Attaching to thread %ld (LWP %d)\n",
|
|
ti_p->ti_tid, ti_p->ti_lid);
|
|
err = linux_attach_lwp (ptid);
|
|
if (err != 0)
|
|
{
|
|
warning ("Could not attach to thread %ld (LWP %d): %s\n",
|
|
ti_p->ti_tid, ti_p->ti_lid,
|
|
linux_ptrace_attach_fail_reason_string (ptid, err));
|
|
return 0;
|
|
}
|
|
|
|
lwp = find_lwp_pid (ptid);
|
|
gdb_assert (lwp != NULL);
|
|
lwp->thread_known = 1;
|
|
lwp->th = *th_p;
|
|
|
|
if (thread_db_use_events)
|
|
{
|
|
td_err_e err;
|
|
struct thread_db *thread_db = proc->priv->thread_db;
|
|
|
|
err = thread_db->td_thr_event_enable_p (th_p, 1);
|
|
if (err != TD_OK)
|
|
error ("Cannot enable thread event reporting for %d: %s",
|
|
ti_p->ti_lid, thread_db_err_str (err));
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* Attach thread if we haven't seen it yet.
|
|
Increment *COUNTER if we have attached a new thread.
|
|
Return false on failure. */
|
|
|
|
static int
|
|
maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
|
|
int *counter)
|
|
{
|
|
struct lwp_info *lwp;
|
|
|
|
lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
|
|
if (lwp != NULL)
|
|
return 1;
|
|
|
|
if (!attach_thread (th_p, ti_p))
|
|
return 0;
|
|
|
|
if (counter != NULL)
|
|
*counter += 1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
|
|
{
|
|
td_thrinfo_t ti;
|
|
td_err_e err;
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
|
|
err = thread_db->td_thr_get_info_p (th_p, &ti);
|
|
if (err != TD_OK)
|
|
error ("Cannot get thread info: %s", thread_db_err_str (err));
|
|
|
|
if (ti.ti_lid == -1)
|
|
{
|
|
/* A thread with kernel thread ID -1 is either a thread that
|
|
exited and was joined, or a thread that is being created but
|
|
hasn't started yet, and that is reusing the tcb/stack of a
|
|
thread that previously exited and was joined. (glibc marks
|
|
terminated and joined threads with kernel thread ID -1. See
|
|
glibc PR17707. */
|
|
if (debug_threads)
|
|
debug_printf ("thread_db: skipping exited and "
|
|
"joined thread (0x%lx)\n", ti.ti_tid);
|
|
return 0;
|
|
}
|
|
|
|
/* Check for zombies. */
|
|
if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
|
|
return 0;
|
|
|
|
if (!maybe_attach_thread (th_p, &ti, (int *) data))
|
|
{
|
|
/* Terminate iteration early: we might be looking at stale data in
|
|
the inferior. The thread_db_find_new_threads will retry. */
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
thread_db_find_new_threads (void)
|
|
{
|
|
td_err_e err;
|
|
ptid_t ptid = current_ptid;
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
int loop, iteration;
|
|
|
|
/* This function is only called when we first initialize thread_db.
|
|
First locate the initial thread. If it is not ready for
|
|
debugging yet, then stop. */
|
|
if (find_one_thread (ptid) == 0)
|
|
return;
|
|
|
|
/* Require 4 successive iterations which do not find any new threads.
|
|
The 4 is a heuristic: there is an inherent race here, and I have
|
|
seen that 2 iterations in a row are not always sufficient to
|
|
"capture" all threads. */
|
|
for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
|
|
{
|
|
int new_thread_count = 0;
|
|
|
|
/* Iterate over all user-space threads to discover new threads. */
|
|
err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
|
|
find_new_threads_callback,
|
|
&new_thread_count,
|
|
TD_THR_ANY_STATE,
|
|
TD_THR_LOWEST_PRIORITY,
|
|
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
|
|
if (debug_threads)
|
|
debug_printf ("Found %d threads in iteration %d.\n",
|
|
new_thread_count, iteration);
|
|
|
|
if (new_thread_count != 0)
|
|
{
|
|
/* Found new threads. Restart iteration from beginning. */
|
|
loop = -1;
|
|
}
|
|
}
|
|
if (err != TD_OK)
|
|
error ("Cannot find new threads: %s", thread_db_err_str (err));
|
|
}
|
|
|
|
/* Cache all future symbols that thread_db might request. We can not
|
|
request symbols at arbitrary states in the remote protocol, only
|
|
when the client tells us that new symbols are available. So when
|
|
we load the thread library, make sure to check the entire list. */
|
|
|
|
static void
|
|
thread_db_look_up_symbols (void)
|
|
{
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
const char **sym_list;
|
|
CORE_ADDR unused;
|
|
|
|
for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
|
|
look_up_one_symbol (*sym_list, &unused, 1);
|
|
|
|
/* We're not interested in any other libraries loaded after this
|
|
point, only in symbols in libpthread.so. */
|
|
thread_db->all_symbols_looked_up = 1;
|
|
}
|
|
|
|
int
|
|
thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
|
|
{
|
|
struct thread_db *thread_db = current_process ()->priv->thread_db;
|
|
int may_ask_gdb = !thread_db->all_symbols_looked_up;
|
|
|
|
/* If we've passed the call to thread_db_look_up_symbols, then
|
|
anything not in the cache must not exist; we're not interested
|
|
in any libraries loaded after that point, only in symbols in
|
|
libpthread.so. It might not be an appropriate time to look
|
|
up a symbol, e.g. while we're trying to fetch registers. */
|
|
return look_up_one_symbol (name, addrp, may_ask_gdb);
|
|
}
|
|
|
|
int
|
|
thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
|
|
CORE_ADDR load_module, CORE_ADDR *address)
|
|
{
|
|
psaddr_t addr;
|
|
td_err_e err;
|
|
struct lwp_info *lwp;
|
|
struct thread_info *saved_thread;
|
|
struct process_info *proc;
|
|
struct thread_db *thread_db;
|
|
|
|
proc = get_thread_process (thread);
|
|
thread_db = proc->priv->thread_db;
|
|
|
|
/* If the thread layer is not (yet) initialized, fail. */
|
|
if (thread_db == NULL || !thread_db->all_symbols_looked_up)
|
|
return TD_ERR;
|
|
|
|
/* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
|
|
could work. */
|
|
if (thread_db->td_thr_tls_get_addr_p == NULL
|
|
|| (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
|
|
return -1;
|
|
|
|
lwp = get_thread_lwp (thread);
|
|
if (!lwp->thread_known)
|
|
find_one_thread (thread->entry.id);
|
|
if (!lwp->thread_known)
|
|
return TD_NOTHR;
|
|
|
|
saved_thread = current_thread;
|
|
current_thread = thread;
|
|
|
|
if (load_module != 0)
|
|
{
|
|
/* Note the cast through uintptr_t: this interface only works if
|
|
a target address fits in a psaddr_t, which is a host pointer.
|
|
So a 32-bit debugger can not access 64-bit TLS through this. */
|
|
err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
|
|
(psaddr_t) (uintptr_t) load_module,
|
|
offset, &addr);
|
|
}
|
|
else
|
|
{
|
|
/* This code path handles the case of -static -pthread executables:
|
|
https://sourceware.org/ml/libc-help/2014-03/msg00024.html
|
|
For older GNU libc r_debug.r_map is NULL. For GNU libc after
|
|
PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
|
|
The constant number 1 depends on GNU __libc_setup_tls
|
|
initialization of l_tls_modid to 1. */
|
|
err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
|
|
addr = (char *) addr + offset;
|
|
}
|
|
|
|
current_thread = saved_thread;
|
|
if (err == TD_OK)
|
|
{
|
|
*address = (CORE_ADDR) (uintptr_t) addr;
|
|
return 0;
|
|
}
|
|
else
|
|
return err;
|
|
}
|
|
|
|
#ifdef USE_LIBTHREAD_DB_DIRECTLY
|
|
|
|
static int
|
|
thread_db_load_search (void)
|
|
{
|
|
td_err_e err;
|
|
struct thread_db *tdb;
|
|
struct process_info *proc = current_process ();
|
|
|
|
gdb_assert (proc->priv->thread_db == NULL);
|
|
|
|
tdb = XCNEW (struct thread_db);
|
|
proc->priv->thread_db = tdb;
|
|
|
|
tdb->td_ta_new_p = &td_ta_new;
|
|
|
|
/* Attempt to open a connection to the thread library. */
|
|
err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
|
|
if (err != TD_OK)
|
|
{
|
|
if (debug_threads)
|
|
debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
|
|
free (tdb);
|
|
proc->priv->thread_db = NULL;
|
|
return 0;
|
|
}
|
|
|
|
tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
|
|
tdb->td_thr_get_info_p = &td_thr_get_info;
|
|
tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
|
|
tdb->td_symbol_list_p = &td_symbol_list;
|
|
|
|
/* This is required only when thread_db_use_events is on. */
|
|
tdb->td_thr_event_enable_p = &td_thr_event_enable;
|
|
|
|
/* These are not essential. */
|
|
tdb->td_ta_event_addr_p = &td_ta_event_addr;
|
|
tdb->td_ta_set_event_p = &td_ta_set_event;
|
|
tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
|
|
tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
|
|
tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
|
|
|
|
return 1;
|
|
}
|
|
|
|
#else
|
|
|
|
static int
|
|
try_thread_db_load_1 (void *handle)
|
|
{
|
|
td_err_e err;
|
|
struct thread_db *tdb;
|
|
struct process_info *proc = current_process ();
|
|
|
|
gdb_assert (proc->priv->thread_db == NULL);
|
|
|
|
tdb = XCNEW (struct thread_db);
|
|
proc->priv->thread_db = tdb;
|
|
|
|
tdb->handle = handle;
|
|
|
|
/* Initialize pointers to the dynamic library functions we will use.
|
|
Essential functions first. */
|
|
|
|
#define CHK(required, a) \
|
|
do \
|
|
{ \
|
|
if ((a) == NULL) \
|
|
{ \
|
|
if (debug_threads) \
|
|
debug_printf ("dlsym: %s\n", dlerror ()); \
|
|
if (required) \
|
|
{ \
|
|
free (tdb); \
|
|
proc->priv->thread_db = NULL; \
|
|
return 0; \
|
|
} \
|
|
} \
|
|
} \
|
|
while (0)
|
|
|
|
#define TDB_DLSYM(tdb, func) \
|
|
tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
|
|
|
|
CHK (1, TDB_DLSYM (tdb, td_ta_new));
|
|
|
|
/* Attempt to open a connection to the thread library. */
|
|
err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
|
|
if (err != TD_OK)
|
|
{
|
|
if (debug_threads)
|
|
debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
|
|
free (tdb);
|
|
proc->priv->thread_db = NULL;
|
|
return 0;
|
|
}
|
|
|
|
CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
|
|
CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
|
|
CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
|
|
CHK (1, TDB_DLSYM (tdb, td_symbol_list));
|
|
|
|
/* This is required only when thread_db_use_events is on. */
|
|
CHK (thread_db_use_events, TDB_DLSYM (tdb, td_thr_event_enable));
|
|
|
|
/* These are not essential. */
|
|
CHK (0, TDB_DLSYM (tdb, td_ta_event_addr));
|
|
CHK (0, TDB_DLSYM (tdb, td_ta_set_event));
|
|
CHK (0, TDB_DLSYM (tdb, td_ta_event_getmsg));
|
|
CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
|
|
CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
|
|
|
|
#undef CHK
|
|
#undef TDB_DLSYM
|
|
|
|
return 1;
|
|
}
|
|
|
|
#ifdef HAVE_DLADDR
|
|
|
|
/* Lookup a library in which given symbol resides.
|
|
Note: this is looking in the GDBSERVER process, not in the inferior.
|
|
Returns library name, or NULL. */
|
|
|
|
static const char *
|
|
dladdr_to_soname (const void *addr)
|
|
{
|
|
Dl_info info;
|
|
|
|
if (dladdr (addr, &info) != 0)
|
|
return info.dli_fname;
|
|
return NULL;
|
|
}
|
|
|
|
#endif
|
|
|
|
static int
|
|
try_thread_db_load (const char *library)
|
|
{
|
|
void *handle;
|
|
|
|
if (debug_threads)
|
|
debug_printf ("Trying host libthread_db library: %s.\n",
|
|
library);
|
|
handle = dlopen (library, RTLD_NOW);
|
|
if (handle == NULL)
|
|
{
|
|
if (debug_threads)
|
|
debug_printf ("dlopen failed: %s.\n", dlerror ());
|
|
return 0;
|
|
}
|
|
|
|
#ifdef HAVE_DLADDR
|
|
if (debug_threads && strchr (library, '/') == NULL)
|
|
{
|
|
void *td_init;
|
|
|
|
td_init = dlsym (handle, "td_init");
|
|
if (td_init != NULL)
|
|
{
|
|
const char *const libpath = dladdr_to_soname (td_init);
|
|
|
|
if (libpath != NULL)
|
|
fprintf (stderr, "Host %s resolved to: %s.\n",
|
|
library, libpath);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
if (try_thread_db_load_1 (handle))
|
|
return 1;
|
|
|
|
/* This library "refused" to work on current inferior. */
|
|
dlclose (handle);
|
|
return 0;
|
|
}
|
|
|
|
/* Handle $sdir in libthread-db-search-path.
|
|
Look for libthread_db in the system dirs, or wherever a plain
|
|
dlopen(file_without_path) will look.
|
|
The result is true for success. */
|
|
|
|
static int
|
|
try_thread_db_load_from_sdir (void)
|
|
{
|
|
return try_thread_db_load (LIBTHREAD_DB_SO);
|
|
}
|
|
|
|
/* Try to load libthread_db from directory DIR of length DIR_LEN.
|
|
The result is true for success. */
|
|
|
|
static int
|
|
try_thread_db_load_from_dir (const char *dir, size_t dir_len)
|
|
{
|
|
char path[PATH_MAX];
|
|
|
|
if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
|
|
{
|
|
char *cp = (char *) xmalloc (dir_len + 1);
|
|
|
|
memcpy (cp, dir, dir_len);
|
|
cp[dir_len] = '\0';
|
|
warning (_("libthread-db-search-path component too long,"
|
|
" ignored: %s."), cp);
|
|
free (cp);
|
|
return 0;
|
|
}
|
|
|
|
memcpy (path, dir, dir_len);
|
|
path[dir_len] = '/';
|
|
strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
|
|
return try_thread_db_load (path);
|
|
}
|
|
|
|
/* Search libthread_db_search_path for libthread_db which "agrees"
|
|
to work on current inferior.
|
|
The result is true for success. */
|
|
|
|
static int
|
|
thread_db_load_search (void)
|
|
{
|
|
VEC (char_ptr) *dir_vec;
|
|
char *this_dir;
|
|
int i, rc = 0;
|
|
|
|
if (libthread_db_search_path == NULL)
|
|
libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
|
|
|
|
dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
|
|
|
|
for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
|
|
{
|
|
const int pdir_len = sizeof ("$pdir") - 1;
|
|
size_t this_dir_len;
|
|
|
|
this_dir_len = strlen (this_dir);
|
|
|
|
if (strncmp (this_dir, "$pdir", pdir_len) == 0
|
|
&& (this_dir[pdir_len] == '\0'
|
|
|| this_dir[pdir_len] == '/'))
|
|
{
|
|
/* We don't maintain a list of loaded libraries so we don't know
|
|
where libpthread lives. We *could* fetch the info, but we don't
|
|
do that yet. Ignore it. */
|
|
}
|
|
else if (strcmp (this_dir, "$sdir") == 0)
|
|
{
|
|
if (try_thread_db_load_from_sdir ())
|
|
{
|
|
rc = 1;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (try_thread_db_load_from_dir (this_dir, this_dir_len))
|
|
{
|
|
rc = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
free_char_ptr_vec (dir_vec);
|
|
if (debug_threads)
|
|
debug_printf ("thread_db_load_search returning %d\n", rc);
|
|
return rc;
|
|
}
|
|
|
|
#endif /* USE_LIBTHREAD_DB_DIRECTLY */
|
|
|
|
int
|
|
thread_db_init (int use_events)
|
|
{
|
|
struct process_info *proc = current_process ();
|
|
|
|
/* FIXME drow/2004-10-16: This is the "overall process ID", which
|
|
GNU/Linux calls tgid, "thread group ID". When we support
|
|
attaching to threads, the original thread may not be the correct
|
|
thread. We would have to get the process ID from /proc for NPTL.
|
|
For LinuxThreads we could do something similar: follow the chain
|
|
of parent processes until we find the highest one we're attached
|
|
to, and use its tgid.
|
|
|
|
This isn't the only place in gdbserver that assumes that the first
|
|
process in the list is the thread group leader. */
|
|
|
|
thread_db_use_events = use_events;
|
|
|
|
if (thread_db_load_search ())
|
|
{
|
|
if (use_events && thread_db_enable_reporting () == 0)
|
|
{
|
|
/* Keep trying; maybe event reporting will work later. */
|
|
thread_db_mourn (proc);
|
|
return 0;
|
|
}
|
|
|
|
/* It's best to avoid td_ta_thr_iter if possible. That walks
|
|
data structures in the inferior's address space that may be
|
|
corrupted, or, if the target is running, the list may change
|
|
while we walk it. In the latter case, it's possible that a
|
|
thread exits just at the exact time that causes GDBserver to
|
|
get stuck in an infinite loop. If the kernel supports clone
|
|
events, and /proc/PID/task/ exits, then we already know about
|
|
all threads in the process. When we need info out of
|
|
thread_db on a given thread (e.g., for TLS), we'll use
|
|
find_one_thread then. That uses thread_db entry points that
|
|
do not walk libpthread's thread list, so should be safe, as
|
|
well as more efficient. */
|
|
if (use_events
|
|
|| !linux_proc_task_list_dir_exists (pid_of (proc)))
|
|
thread_db_find_new_threads ();
|
|
thread_db_look_up_symbols ();
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
any_thread_of (struct inferior_list_entry *entry, void *args)
|
|
{
|
|
int *pid_p = args;
|
|
|
|
if (ptid_get_pid (entry->id) == *pid_p)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
switch_to_process (struct process_info *proc)
|
|
{
|
|
int pid = pid_of (proc);
|
|
|
|
current_thread =
|
|
(struct thread_info *) find_inferior (&all_threads,
|
|
any_thread_of, &pid);
|
|
}
|
|
|
|
/* Disconnect from libthread_db and free resources. */
|
|
|
|
static void
|
|
disable_thread_event_reporting (struct process_info *proc)
|
|
{
|
|
struct thread_db *thread_db = proc->priv->thread_db;
|
|
if (thread_db)
|
|
{
|
|
td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
|
|
td_thr_events_t *event);
|
|
|
|
#ifndef USE_LIBTHREAD_DB_DIRECTLY
|
|
td_ta_clear_event_p
|
|
= (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
|
|
"td_ta_clear_event");
|
|
#else
|
|
td_ta_clear_event_p = &td_ta_clear_event;
|
|
#endif
|
|
|
|
if (td_ta_clear_event_p != NULL)
|
|
{
|
|
struct thread_info *saved_thread = current_thread;
|
|
td_thr_events_t events;
|
|
|
|
switch_to_process (proc);
|
|
|
|
/* Set the process wide mask saying we aren't interested
|
|
in any events anymore. */
|
|
td_event_fillset (&events);
|
|
(*td_ta_clear_event_p) (thread_db->thread_agent, &events);
|
|
|
|
current_thread = saved_thread;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
remove_thread_event_breakpoints (struct process_info *proc)
|
|
{
|
|
struct thread_db *thread_db = proc->priv->thread_db;
|
|
|
|
if (thread_db->td_create_bp != NULL)
|
|
{
|
|
struct thread_info *saved_thread = current_thread;
|
|
|
|
switch_to_process (proc);
|
|
|
|
delete_breakpoint (thread_db->td_create_bp);
|
|
thread_db->td_create_bp = NULL;
|
|
|
|
current_thread = saved_thread;
|
|
}
|
|
}
|
|
|
|
void
|
|
thread_db_detach (struct process_info *proc)
|
|
{
|
|
struct thread_db *thread_db = proc->priv->thread_db;
|
|
|
|
if (thread_db)
|
|
{
|
|
disable_thread_event_reporting (proc);
|
|
remove_thread_event_breakpoints (proc);
|
|
}
|
|
}
|
|
|
|
/* Disconnect from libthread_db and free resources. */
|
|
|
|
void
|
|
thread_db_mourn (struct process_info *proc)
|
|
{
|
|
struct thread_db *thread_db = proc->priv->thread_db;
|
|
if (thread_db)
|
|
{
|
|
td_ta_delete_ftype *td_ta_delete_p;
|
|
|
|
#ifndef USE_LIBTHREAD_DB_DIRECTLY
|
|
td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
|
|
#else
|
|
td_ta_delete_p = &td_ta_delete;
|
|
#endif
|
|
|
|
if (td_ta_delete_p != NULL)
|
|
(*td_ta_delete_p) (thread_db->thread_agent);
|
|
|
|
#ifndef USE_LIBTHREAD_DB_DIRECTLY
|
|
dlclose (thread_db->handle);
|
|
#endif /* USE_LIBTHREAD_DB_DIRECTLY */
|
|
|
|
free (thread_db);
|
|
proc->priv->thread_db = NULL;
|
|
}
|
|
}
|
|
|
|
/* Handle "set libthread-db-search-path" monitor command and return 1.
|
|
For any other command, return 0. */
|
|
|
|
int
|
|
thread_db_handle_monitor_command (char *mon)
|
|
{
|
|
const char *cmd = "set libthread-db-search-path";
|
|
size_t cmd_len = strlen (cmd);
|
|
|
|
if (strncmp (mon, cmd, cmd_len) == 0
|
|
&& (mon[cmd_len] == '\0'
|
|
|| mon[cmd_len] == ' '))
|
|
{
|
|
const char *cp = mon + cmd_len;
|
|
|
|
if (libthread_db_search_path != NULL)
|
|
free (libthread_db_search_path);
|
|
|
|
/* Skip leading space (if any). */
|
|
while (isspace (*cp))
|
|
++cp;
|
|
|
|
if (*cp == '\0')
|
|
cp = LIBTHREAD_DB_SEARCH_PATH;
|
|
libthread_db_search_path = xstrdup (cp);
|
|
|
|
monitor_output ("libthread-db-search-path set to `");
|
|
monitor_output (libthread_db_search_path);
|
|
monitor_output ("'\n");
|
|
return 1;
|
|
}
|
|
|
|
/* Tell server.c to perform default processing. */
|
|
return 0;
|
|
}
|