Note: I needed to split this patch in two, otherwise it's too big for the mailing list. This patch adds explicit casts to situations where a void pointer is assigned to a pointer to the "real" type. Building in C++ mode requires those assignments to use an explicit cast. This includes, for example: - callback arguments (cleanups, comparison functions, ...) - data attached to some object (objfile, program space, etc) in the form of a void pointer - "user data" passed to some function This patch comes from the commit "(mostly) auto-generated patch to insert casts needed for C++", taken from Pedro's C++ branch. Only files built on x86 with --enable-targets=all are modified, so the native files for other arches will need to be dealt with separately. I built-tested this with --enable-targets=all and reg-tested. To my surprise, a test case (selftest.exp) had to be adjusted. Here's the ChangeLog entry. Again, this was relatively quick to make despite the length, thanks to David Malcom's script, although I don't believe it's very useful information in that particular case... gdb/ChangeLog: * aarch64-tdep.c (aarch64_make_prologue_cache): Add cast(s). (aarch64_make_stub_cache): Likewise. (value_of_aarch64_user_reg): Likewise. * ada-lang.c (ada_inferior_data_cleanup): Likewise. (get_ada_inferior_data): Likewise. (get_ada_pspace_data): Likewise. (ada_pspace_data_cleanup): Likewise. (ada_complete_symbol_matcher): Likewise. (ada_exc_search_name_matches): Likewise. * ada-tasks.c (get_ada_tasks_pspace_data): Likewise. (get_ada_tasks_inferior_data): Likewise. * addrmap.c (addrmap_mutable_foreach_worker): Likewise. (splay_obstack_alloc): Likewise. (splay_obstack_free): Likewise. * alpha-linux-tdep.c (alpha_linux_supply_gregset): Likewise. (alpha_linux_collect_gregset): Likewise. (alpha_linux_supply_fpregset): Likewise. (alpha_linux_collect_fpregset): Likewise. * alpha-mdebug-tdep.c (alpha_mdebug_frame_unwind_cache): Likewise. * alpha-tdep.c (alpha_lds): Likewise. (alpha_sts): Likewise. (alpha_sigtramp_frame_unwind_cache): Likewise. (alpha_heuristic_frame_unwind_cache): Likewise. (alpha_supply_int_regs): Likewise. (alpha_fill_int_regs): Likewise. (alpha_supply_fp_regs): Likewise. (alpha_fill_fp_regs): Likewise. * alphanbsd-tdep.c (alphanbsd_supply_fpregset): Likewise. (alphanbsd_aout_supply_gregset): Likewise. (alphanbsd_supply_gregset): Likewise. * amd64-linux-tdep.c (amd64_linux_init_abi): Likewise. (amd64_x32_linux_init_abi): Likewise. * amd64-nat.c (amd64_supply_native_gregset): Likewise. (amd64_collect_native_gregset): Likewise. * amd64-tdep.c (amd64_frame_cache): Likewise. (amd64_sigtramp_frame_cache): Likewise. (amd64_epilogue_frame_cache): Likewise. (amd64_supply_fxsave): Likewise. (amd64_supply_xsave): Likewise. (amd64_collect_fxsave): Likewise. (amd64_collect_xsave): Likewise. * amd64-windows-tdep.c (amd64_windows_frame_cache): Likewise. * amd64obsd-tdep.c (amd64obsd_trapframe_cache): Likewise. * arm-linux-tdep.c (arm_linux_supply_gregset): Likewise. (arm_linux_collect_gregset): Likewise. (arm_linux_supply_nwfpe): Likewise. (arm_linux_collect_nwfpe): Likewise. (arm_linux_supply_vfp): Likewise. (arm_linux_collect_vfp): Likewise. * arm-tdep.c (arm_find_mapping_symbol): Likewise. (arm_prologue_unwind_stop_reason): Likewise. (arm_prologue_this_id): Likewise. (arm_prologue_prev_register): Likewise. (arm_exidx_data_free): Likewise. (arm_find_exidx_entry): Likewise. (arm_stub_this_id): Likewise. (arm_m_exception_this_id): Likewise. (arm_m_exception_prev_register): Likewise. (arm_normal_frame_base): Likewise. (gdb_print_insn_arm): Likewise. (arm_objfile_data_free): Likewise. (arm_record_special_symbol): Likewise. (value_of_arm_user_reg): Likewise. * armbsd-tdep.c (armbsd_supply_fpregset): Likewise. (armbsd_supply_gregset): Likewise. * auto-load.c (auto_load_pspace_data_cleanup): Likewise. (get_auto_load_pspace_data): Likewise. (hash_loaded_script_entry): Likewise. (eq_loaded_script_entry): Likewise. (clear_section_scripts): Likewise. (collect_matching_scripts): Likewise. * auxv.c (auxv_inferior_data_cleanup): Likewise. (get_auxv_inferior_data): Likewise. * avr-tdep.c (avr_frame_unwind_cache): Likewise. * ax-general.c (do_free_agent_expr_cleanup): Likewise. * bfd-target.c (target_bfd_xfer_partial): Likewise. (target_bfd_xclose): Likewise. (target_bfd_get_section_table): Likewise. * bfin-tdep.c (bfin_frame_cache): Likewise. * block.c (find_block_in_blockvector): Likewise. (call_site_for_pc): Likewise. (block_find_non_opaque_type_preferred): Likewise. * break-catch-sig.c (signal_catchpoint_insert_location): Likewise. (signal_catchpoint_remove_location): Likewise. (signal_catchpoint_breakpoint_hit): Likewise. (signal_catchpoint_print_one): Likewise. (signal_catchpoint_print_mention): Likewise. (signal_catchpoint_print_recreate): Likewise. * break-catch-syscall.c (get_catch_syscall_inferior_data): Likewise. * breakpoint.c (do_cleanup_counted_command_line): Likewise. (bp_location_compare_addrs): Likewise. (get_first_locp_gte_addr): Likewise. (check_tracepoint_command): Likewise. (do_map_commands_command): Likewise. (get_breakpoint_objfile_data): Likewise. (free_breakpoint_probes): Likewise. (do_captured_breakpoint_query): Likewise. (compare_breakpoints): Likewise. (bp_location_compare): Likewise. (bpstat_remove_breakpoint_callback): Likewise. (do_delete_breakpoint_cleanup): Likewise. * bsd-uthread.c (bsd_uthread_set_supply_uthread): Likewise. (bsd_uthread_set_collect_uthread): Likewise. (bsd_uthread_activate): Likewise. (bsd_uthread_fetch_registers): Likewise. (bsd_uthread_store_registers): Likewise. * btrace.c (check_xml_btrace_version): Likewise. (parse_xml_btrace_block): Likewise. (parse_xml_btrace_pt_config_cpu): Likewise. (parse_xml_btrace_pt_raw): Likewise. (parse_xml_btrace_pt): Likewise. (parse_xml_btrace_conf_bts): Likewise. (parse_xml_btrace_conf_pt): Likewise. (do_btrace_data_cleanup): Likewise. * c-typeprint.c (find_typedef_for_canonicalize): Likewise. * charset.c (cleanup_iconv): Likewise. (do_cleanup_iterator): Likewise. * cli-out.c (cli_uiout_dtor): Likewise. (cli_table_begin): Likewise. (cli_table_body): Likewise. (cli_table_end): Likewise. (cli_table_header): Likewise. (cli_begin): Likewise. (cli_end): Likewise. (cli_field_int): Likewise. (cli_field_skip): Likewise. (cli_field_string): Likewise. (cli_field_fmt): Likewise. (cli_spaces): Likewise. (cli_text): Likewise. (cli_message): Likewise. (cli_wrap_hint): Likewise. (cli_flush): Likewise. (cli_redirect): Likewise. (out_field_fmt): Likewise. (field_separator): Likewise. (cli_out_set_stream): Likewise. * cli/cli-cmds.c (compare_symtabs): Likewise. * cli/cli-dump.c (call_dump_func): Likewise. (restore_section_callback): Likewise. * cli/cli-script.c (clear_hook_in_cleanup): Likewise. (do_restore_user_call_depth): Likewise. (do_free_command_lines_cleanup): Likewise. * coff-pe-read.c (get_section_vmas): Likewise. (pe_as16): Likewise. (pe_as32): Likewise. * coffread.c (coff_symfile_read): Likewise. * common/agent.c (agent_look_up_symbols): Likewise. * common/filestuff.c (do_close_cleanup): Likewise. * common/format.c (free_format_pieces_cleanup): Likewise. * common/vec.c (vec_o_reserve): Likewise. * compile/compile-c-support.c (print_one_macro): Likewise. * compile/compile-c-symbols.c (hash_symbol_error): Likewise. (eq_symbol_error): Likewise. (del_symbol_error): Likewise. (error_symbol_once): Likewise. (gcc_convert_symbol): Likewise. (gcc_symbol_address): Likewise. (hash_symname): Likewise. (eq_symname): Likewise. * compile/compile-c-types.c (hash_type_map_instance): Likewise. (eq_type_map_instance): Likewise. (insert_type): Likewise. (convert_type): Likewise. * compile/compile-object-load.c (munmap_listp_free_cleanup): Likewise. (setup_sections): Likewise. (link_hash_table_free): Likewise. (copy_sections): Likewise. * compile/compile-object-run.c (do_module_cleanup): Likewise. * compile/compile.c (compile_print_value): Likewise. (do_rmdir): Likewise. (cleanup_compile_instance): Likewise. (cleanup_unlink_file): Likewise. * completer.c (free_completion_tracker): Likewise. * corelow.c (add_to_spuid_list): Likewise. * cp-namespace.c (reset_directive_searched): Likewise. * cp-support.c (reset_directive_searched): Likewise. * cris-tdep.c (cris_sigtramp_frame_unwind_cache): Likewise. (cris_frame_unwind_cache): Likewise. * d-lang.c (builtin_d_type): Likewise. * d-namespace.c (reset_directive_searched): Likewise. * dbxread.c (dbx_free_symfile_info): Likewise. (do_free_bincl_list_cleanup): Likewise. * disasm.c (hash_dis_line_entry): Likewise. (eq_dis_line_entry): Likewise. (dis_asm_print_address): Likewise. (fprintf_disasm): Likewise. (do_ui_file_delete): Likewise. * doublest.c (convert_floatformat_to_doublest): Likewise. * dummy-frame.c (pop_dummy_frame_bpt): Likewise. (dummy_frame_prev_register): Likewise. (dummy_frame_this_id): Likewise. * dwarf2-frame-tailcall.c (cache_hash): Likewise. (cache_eq): Likewise. (cache_find): Likewise. (tailcall_frame_this_id): Likewise. (dwarf2_tailcall_prev_register_first): Likewise. (tailcall_frame_prev_register): Likewise. (tailcall_frame_dealloc_cache): Likewise. (tailcall_frame_prev_arch): Likewise. * dwarf2-frame.c (dwarf2_frame_state_free): Likewise. (dwarf2_frame_set_init_reg): Likewise. (dwarf2_frame_init_reg): Likewise. (dwarf2_frame_set_signal_frame_p): Likewise. (dwarf2_frame_signal_frame_p): Likewise. (dwarf2_frame_set_adjust_regnum): Likewise. (dwarf2_frame_adjust_regnum): Likewise. (clear_pointer_cleanup): Likewise. (dwarf2_frame_cache): Likewise. (find_cie): Likewise. (dwarf2_frame_find_fde): Likewise. * dwarf2expr.c (dwarf_expr_address_type): Likewise. (free_dwarf_expr_context_cleanup): Likewise. * dwarf2loc.c (locexpr_find_frame_base_location): Likewise. (locexpr_get_frame_base): Likewise. (loclist_find_frame_base_location): Likewise. (loclist_get_frame_base): Likewise. (dwarf_expr_dwarf_call): Likewise. (dwarf_expr_get_base_type): Likewise. (dwarf_expr_push_dwarf_reg_entry_value): Likewise. (dwarf_expr_get_obj_addr): Likewise. (entry_data_value_coerce_ref): Likewise. (entry_data_value_copy_closure): Likewise. (entry_data_value_free_closure): Likewise. (get_frame_address_in_block_wrapper): Likewise. (dwarf2_evaluate_property): Likewise. (dwarf2_compile_property_to_c): Likewise. (needs_frame_read_addr_from_reg): Likewise. (needs_frame_get_reg_value): Likewise. (needs_frame_frame_base): Likewise. (needs_frame_frame_cfa): Likewise. (needs_frame_tls_address): Likewise. (needs_frame_dwarf_call): Likewise. (needs_dwarf_reg_entry_value): Likewise. (get_ax_pc): Likewise. (locexpr_read_variable): Likewise. (locexpr_read_variable_at_entry): Likewise. (locexpr_read_needs_frame): Likewise. (locexpr_describe_location): Likewise. (locexpr_tracepoint_var_ref): Likewise. (locexpr_generate_c_location): Likewise. (loclist_read_variable): Likewise. (loclist_read_variable_at_entry): Likewise. (loclist_describe_location): Likewise. (loclist_tracepoint_var_ref): Likewise. (loclist_generate_c_location): Likewise. * dwarf2read.c (line_header_hash_voidp): Likewise. (line_header_eq_voidp): Likewise. (dwarf2_has_info): Likewise. (dwarf2_get_section_info): Likewise. (locate_dwz_sections): Likewise. (hash_file_name_entry): Likewise. (eq_file_name_entry): Likewise. (delete_file_name_entry): Likewise. (dw2_setup): Likewise. (dw2_get_file_names_reader): Likewise. (dw2_find_pc_sect_compunit_symtab): Likewise. (hash_signatured_type): Likewise. (eq_signatured_type): Likewise. (add_signatured_type_cu_to_table): Likewise. (create_debug_types_hash_table): Likewise. (lookup_dwo_signatured_type): Likewise. (lookup_dwp_signatured_type): Likewise. (lookup_signatured_type): Likewise. (hash_type_unit_group): Likewise. (eq_type_unit_group): Likewise. (get_type_unit_group): Likewise. (process_psymtab_comp_unit_reader): Likewise. (sort_tu_by_abbrev_offset): Likewise. (process_skeletonless_type_unit): Likewise. (psymtabs_addrmap_cleanup): Likewise. (dwarf2_read_symtab): Likewise. (psymtab_to_symtab_1): Likewise. (die_hash): Likewise. (die_eq): Likewise. (load_full_comp_unit_reader): Likewise. (reset_die_in_process): Likewise. (free_cu_line_header): Likewise. (handle_DW_AT_stmt_list): Likewise. (hash_dwo_file): Likewise. (eq_dwo_file): Likewise. (hash_dwo_unit): Likewise. (eq_dwo_unit): Likewise. (create_dwo_cu_reader): Likewise. (create_dwo_unit_in_dwp_v1): Likewise. (create_dwo_unit_in_dwp_v2): Likewise. (lookup_dwo_unit_in_dwp): Likewise. (dwarf2_locate_dwo_sections): Likewise. (dwarf2_locate_common_dwp_sections): Likewise. (dwarf2_locate_v2_dwp_sections): Likewise. (hash_dwp_loaded_cutus): Likewise. (eq_dwp_loaded_cutus): Likewise. (lookup_dwo_cutu): Likewise. (abbrev_table_free_cleanup): Likewise. (dwarf2_free_abbrev_table): Likewise. (find_partial_die_in_comp_unit): Likewise. (free_line_header_voidp): Likewise. (follow_die_offset): Likewise. (follow_die_sig_1): Likewise. (free_heap_comp_unit): Likewise. (free_stack_comp_unit): Likewise. (dwarf2_free_objfile): Likewise. (per_cu_offset_and_type_hash): Likewise. (per_cu_offset_and_type_eq): Likewise. (get_die_type_at_offset): Likewise. (partial_die_hash): Likewise. (partial_die_eq): Likewise. (dwarf2_per_objfile_free): Likewise. (hash_strtab_entry): Likewise. (eq_strtab_entry): Likewise. (add_string): Likewise. (hash_symtab_entry): Likewise. (eq_symtab_entry): Likewise. (delete_symtab_entry): Likewise. (cleanup_mapped_symtab): Likewise. (add_indices_to_cpool): Likewise. (hash_psymtab_cu_index): Likewise. (eq_psymtab_cu_index): Likewise. (add_address_entry_worker): Likewise. (unlink_if_set): Likewise. (write_one_signatured_type): Likewise. (save_gdb_index_command): Likewise. * elfread.c (elf_symtab_read): Likewise. (elf_gnu_ifunc_cache_hash): Likewise. (elf_gnu_ifunc_cache_eq): Likewise. (elf_gnu_ifunc_record_cache): Likewise. (elf_gnu_ifunc_resolve_by_cache): Likewise. (elf_get_probes): Likewise. (probe_key_free): Likewise. * f-lang.c (builtin_f_type): Likewise. * frame-base.c (frame_base_append_sniffer): Likewise. (frame_base_set_default): Likewise. (frame_base_find_by_frame): Likewise. * frame-unwind.c (frame_unwind_prepend_unwinder): Likewise. (frame_unwind_append_unwinder): Likewise. (frame_unwind_find_by_frame): Likewise. * frame.c (frame_addr_hash): Likewise. (frame_addr_hash_eq): Likewise. (frame_stash_find): Likewise. (do_frame_register_read): Likewise. (unwind_to_current_frame): Likewise. (frame_cleanup_after_sniffer): Likewise. * frv-linux-tdep.c (frv_linux_sigtramp_frame_cache): Likewise. * frv-tdep.c (frv_frame_unwind_cache): Likewise. * ft32-tdep.c (ft32_frame_cache): Likewise. * gcore.c (do_bfd_delete_cleanup): Likewise. (gcore_create_callback): Likewise. * gdb_bfd.c (hash_bfd): Likewise. (eq_bfd): Likewise. (gdb_bfd_open): Likewise. (free_one_bfd_section): Likewise. (gdb_bfd_ref): Likewise. (gdb_bfd_unref): Likewise. (get_section_descriptor): Likewise. (gdb_bfd_map_section): Likewise. (gdb_bfd_crc): Likewise. (gdb_bfd_mark_parent): Likewise. (gdb_bfd_record_inclusion): Likewise. (gdb_bfd_requires_relocations): Likewise. (print_one_bfd): Likewise. * gdbtypes.c (type_pair_hash): Likewise. (type_pair_eq): Likewise. (builtin_type): Likewise. (objfile_type): Likewise. * gnu-v3-abi.c (vtable_ptrdiff_type): Likewise. (vtable_address_point_offset): Likewise. (gnuv3_get_vtable): Likewise. (hash_value_and_voffset): Likewise. (eq_value_and_voffset): Likewise. (compare_value_and_voffset): Likewise. (compute_vtable_size): Likewise. (gnuv3_get_typeid_type): Likewise. * go-lang.c (builtin_go_type): Likewise. * guile/scm-block.c (bkscm_hash_block_smob): Likewise. (bkscm_eq_block_smob): Likewise. (bkscm_objfile_block_map): Likewise. (bkscm_del_objfile_blocks): Likewise. * guile/scm-breakpoint.c (bpscm_build_bp_list): Likewise. * guile/scm-disasm.c (gdbscm_disasm_read_memory_worker): Likewise. (gdbscm_disasm_print_address): Likewise. * guile/scm-frame.c (frscm_hash_frame_smob): Likewise. (frscm_eq_frame_smob): Likewise. (frscm_inferior_frame_map): Likewise. (frscm_del_inferior_frames): Likewise. * guile/scm-gsmob.c (gdbscm_add_objfile_ref): Likewise. * guile/scm-objfile.c (ofscm_handle_objfile_deleted): Likewise. (ofscm_objfile_smob_from_objfile): Likewise. * guile/scm-ports.c (ioscm_write): Likewise. (ioscm_file_port_delete): Likewise. (ioscm_file_port_rewind): Likewise. (ioscm_file_port_put): Likewise. (ioscm_file_port_write): Likewise. * guile/scm-progspace.c (psscm_handle_pspace_deleted): Likewise. (psscm_pspace_smob_from_pspace): Likewise. * guile/scm-safe-call.c (scscm_recording_pre_unwind_handler): Likewise. (scscm_recording_unwind_handler): Likewise. (gdbscm_with_catch): Likewise. (scscm_call_0_body): Likewise. (scscm_call_1_body): Likewise. (scscm_call_2_body): Likewise. (scscm_call_3_body): Likewise. (scscm_call_4_body): Likewise. (scscm_apply_1_body): Likewise. (scscm_eval_scheme_string): Likewise. (gdbscm_safe_eval_string): Likewise. (scscm_source_scheme_script): Likewise. (gdbscm_safe_source_script): Likewise. * guile/scm-string.c (gdbscm_call_scm_to_stringn): Likewise. (gdbscm_call_scm_from_stringn): Likewise. * guile/scm-symbol.c (syscm_hash_symbol_smob): Likewise. (syscm_eq_symbol_smob): Likewise. (syscm_get_symbol_map): Likewise. (syscm_del_objfile_symbols): Likewise. * guile/scm-symtab.c (stscm_hash_symtab_smob): Likewise. (stscm_eq_symtab_smob): Likewise. (stscm_objfile_symtab_map): Likewise. (stscm_del_objfile_symtabs): Likewise. * guile/scm-type.c (tyscm_hash_type_smob): Likewise. (tyscm_eq_type_smob): Likewise. (tyscm_type_map): Likewise. (tyscm_copy_type_recursive): Likewise. (save_objfile_types): Likewise. * guile/scm-utils.c (extract_arg): Likewise. * h8300-tdep.c (h8300_frame_cache): Likewise. * hppa-linux-tdep.c (hppa_linux_sigtramp_frame_unwind_cache): Likewise. * hppa-tdep.c (compare_unwind_entries): Likewise. (find_unwind_entry): Likewise. (hppa_frame_cache): Likewise. (hppa_stub_frame_unwind_cache): Likewise. * hppanbsd-tdep.c (hppanbsd_supply_gregset): Likewise. * hppaobsd-tdep.c (hppaobsd_supply_gregset): Likewise. (hppaobsd_supply_fpregset): Likewise. * i386-cygwin-tdep.c (core_process_module_section): Likewise. * i386-linux-tdep.c (i386_linux_init_abi): Likewise. * i386-tdep.c (i386_frame_cache): Likewise. (i386_epilogue_frame_cache): Likewise. (i386_sigtramp_frame_cache): Likewise. (i386_supply_gregset): Likewise. (i386_collect_gregset): Likewise. (i386_gdbarch_init): Likewise. * i386obsd-tdep.c (i386obsd_aout_supply_regset): Likewise. (i386obsd_trapframe_cache): Likewise. * i387-tdep.c (i387_supply_fsave): Likewise. (i387_collect_fsave): Likewise. (i387_supply_fxsave): Likewise. (i387_collect_fxsave): Likewise. (i387_supply_xsave): Likewise. (i387_collect_xsave): Likewise. * ia64-tdep.c (ia64_frame_cache): Likewise. (ia64_sigtramp_frame_cache): Likewise. * infcmd.c (attach_command_continuation): Likewise. (attach_command_continuation_free_args): Likewise. * inferior.c (restore_inferior): Likewise. (delete_thread_of_inferior): Likewise. * inflow.c (inflow_inferior_data_cleanup): Likewise. (get_inflow_inferior_data): Likewise. (inflow_inferior_exit): Likewise. * infrun.c (displaced_step_clear_cleanup): Likewise. (restore_current_uiout_cleanup): Likewise. (release_stop_context_cleanup): Likewise. (do_restore_infcall_suspend_state_cleanup): Likewise. (do_restore_infcall_control_state_cleanup): Likewise. (restore_inferior_ptid): Likewise. * inline-frame.c (block_starting_point_at): Likewise. * iq2000-tdep.c (iq2000_frame_cache): Likewise. * jit.c (get_jit_objfile_data): Likewise. (get_jit_program_space_data): Likewise. (jit_object_close_impl): Likewise. (jit_find_objf_with_entry_addr): Likewise. (jit_breakpoint_deleted): Likewise. (jit_unwind_reg_set_impl): Likewise. (jit_unwind_reg_get_impl): Likewise. (jit_dealloc_cache): Likewise. (jit_frame_sniffer): Likewise. (jit_frame_prev_register): Likewise. (jit_prepend_unwinder): Likewise. (jit_inferior_exit_hook): Likewise. (free_objfile_data): Likewise. * jv-lang.c (jv_per_objfile_free): Likewise. (get_dynamics_objfile): Likewise. (get_java_class_symtab): Likewise. (builtin_java_type): Likewise. * language.c (language_string_char_type): Likewise. (language_bool_type): Likewise. (language_lookup_primitive_type): Likewise. (language_lookup_primitive_type_as_symbol): Likewise. * linespec.c (hash_address_entry): Likewise. (eq_address_entry): Likewise. (iterate_inline_only): Likewise. (iterate_name_matcher): Likewise. (decode_line_2_compare_items): Likewise. (collect_one_symbol): Likewise. (compare_symbols): Likewise. (compare_msymbols): Likewise. (add_symtabs_to_list): Likewise. (collect_symbols): Likewise. (compare_msyms): Likewise. (add_minsym): Likewise. (cleanup_linespec_result): Likewise. * linux-fork.c (inferior_call_waitpid_cleanup): Likewise. * linux-nat.c (delete_lwp_cleanup): Likewise. (count_events_callback): Likewise. (select_event_lwp_callback): Likewise. (resume_stopped_resumed_lwps): Likewise. * linux-tdep.c (get_linux_gdbarch_data): Likewise. (invalidate_linux_cache_inf): Likewise. (get_linux_inferior_data): Likewise. (linux_find_memory_regions_thunk): Likewise. (linux_make_mappings_callback): Likewise. (linux_corefile_thread_callback): Likewise. (find_mapping_size): Likewise. * linux-thread-db.c (find_new_threads_callback): Likewise. * lm32-tdep.c (lm32_frame_cache): Likewise. * m2-lang.c (builtin_m2_type): Likewise. * m32c-tdep.c (m32c_analyze_frame_prologue): Likewise. * m32r-linux-tdep.c (m32r_linux_sigtramp_frame_cache): Likewise. (m32r_linux_supply_gregset): Likewise. (m32r_linux_collect_gregset): Likewise. * m32r-tdep.c (m32r_frame_unwind_cache): Likewise. * m68hc11-tdep.c (m68hc11_frame_unwind_cache): Likewise. * m68k-tdep.c (m68k_frame_cache): Likewise. * m68kbsd-tdep.c (m68kbsd_supply_fpregset): Likewise. (m68kbsd_supply_gregset): Likewise. * m68klinux-tdep.c (m68k_linux_sigtramp_frame_cache): Likewise. * m88k-tdep.c (m88k_frame_cache): Likewise. (m88k_supply_gregset): Likewise. gdb/gdbserver/ChangeLog: * dll.c (match_dll): Add cast(s). (unloaded_dll): Likewise. * linux-low.c (second_thread_of_pid_p): Likewise. (delete_lwp_callback): Likewise. (count_events_callback): Likewise. (select_event_lwp_callback): Likewise. (linux_set_resume_request): Likewise. * server.c (accumulate_file_name_length): Likewise. (emit_dll_description): Likewise. (handle_qxfer_threads_worker): Likewise. (visit_actioned_threads): Likewise. * thread-db.c (any_thread_of): Likewise. * tracepoint.c (same_process_p): Likewise. (match_blocktype): Likewise. (build_traceframe_info_xml): Likewise. gdb/testsuite/ChangeLog: * gdb.gdb/selftest.exp (do_steps_and_nexts): Adjust expected source line.
1774 lines
44 KiB
C
1774 lines
44 KiB
C
/* GDB CLI command scripting.
|
||
|
||
Copyright (C) 1986-2015 Free Software Foundation, Inc.
|
||
|
||
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 "defs.h"
|
||
#include "value.h"
|
||
#include "language.h" /* For value_true */
|
||
#include <ctype.h>
|
||
|
||
#include "ui-out.h"
|
||
#include "top.h"
|
||
#include "breakpoint.h"
|
||
#include "cli/cli-cmds.h"
|
||
#include "cli/cli-decode.h"
|
||
#include "cli/cli-script.h"
|
||
|
||
#include "extension.h"
|
||
#include "interps.h"
|
||
#include "compile/compile.h"
|
||
|
||
/* Prototypes for local functions. */
|
||
|
||
static enum command_control_type
|
||
recurse_read_control_structure (char * (*read_next_line_func) (void),
|
||
struct command_line *current_cmd,
|
||
void (*validator)(char *, void *),
|
||
void *closure);
|
||
|
||
static char *insert_args (char *line);
|
||
|
||
static struct cleanup * setup_user_args (char *p);
|
||
|
||
static char *read_next_line (void);
|
||
|
||
/* Level of control structure when reading. */
|
||
static int control_level;
|
||
|
||
/* Level of control structure when executing. */
|
||
static int command_nest_depth = 1;
|
||
|
||
/* This is to prevent certain commands being printed twice. */
|
||
static int suppress_next_print_command_trace = 0;
|
||
|
||
/* Structure for arguments to user defined functions. */
|
||
#define MAXUSERARGS 10
|
||
struct user_args
|
||
{
|
||
struct user_args *next;
|
||
/* It is necessary to store a malloced copy of the command line to
|
||
ensure that the arguments are not overwritten before they are
|
||
used. */
|
||
char *command;
|
||
struct
|
||
{
|
||
char *arg;
|
||
int len;
|
||
}
|
||
a[MAXUSERARGS];
|
||
int count;
|
||
}
|
||
*user_args;
|
||
|
||
|
||
/* Return non-zero if TYPE is a multi-line command (i.e., is terminated
|
||
by "end"). */
|
||
|
||
static int
|
||
multi_line_command_p (enum command_control_type type)
|
||
{
|
||
switch (type)
|
||
{
|
||
case if_control:
|
||
case while_control:
|
||
case while_stepping_control:
|
||
case commands_control:
|
||
case compile_control:
|
||
case python_control:
|
||
case guile_control:
|
||
return 1;
|
||
default:
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/* Allocate, initialize a new command line structure for one of the
|
||
control commands (if/while). */
|
||
|
||
static struct command_line *
|
||
build_command_line (enum command_control_type type, char *args)
|
||
{
|
||
struct command_line *cmd;
|
||
|
||
if (args == NULL && (type == if_control || type == while_control))
|
||
error (_("if/while commands require arguments."));
|
||
gdb_assert (args != NULL);
|
||
|
||
cmd = XNEW (struct command_line);
|
||
cmd->next = NULL;
|
||
cmd->control_type = type;
|
||
|
||
cmd->body_count = 1;
|
||
cmd->body_list = XCNEWVEC (struct command_line *, cmd->body_count);
|
||
cmd->line = xstrdup (args);
|
||
|
||
return cmd;
|
||
}
|
||
|
||
/* Build and return a new command structure for the control commands
|
||
such as "if" and "while". */
|
||
|
||
struct command_line *
|
||
get_command_line (enum command_control_type type, char *arg)
|
||
{
|
||
struct command_line *cmd;
|
||
struct cleanup *old_chain = NULL;
|
||
|
||
/* Allocate and build a new command line structure. */
|
||
cmd = build_command_line (type, arg);
|
||
|
||
old_chain = make_cleanup_free_command_lines (&cmd);
|
||
|
||
/* Read in the body of this command. */
|
||
if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
|
||
== invalid_control)
|
||
{
|
||
warning (_("Error reading in canned sequence of commands."));
|
||
do_cleanups (old_chain);
|
||
return NULL;
|
||
}
|
||
|
||
discard_cleanups (old_chain);
|
||
return cmd;
|
||
}
|
||
|
||
/* Recursively print a command (including full control structures). */
|
||
|
||
void
|
||
print_command_lines (struct ui_out *uiout, struct command_line *cmd,
|
||
unsigned int depth)
|
||
{
|
||
struct command_line *list;
|
||
|
||
list = cmd;
|
||
while (list)
|
||
{
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
|
||
/* A simple command, print it and continue. */
|
||
if (list->control_type == simple_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, list->line);
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* loop_continue to jump to the start of a while loop, print it
|
||
and continue. */
|
||
if (list->control_type == continue_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, "loop_continue");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* loop_break to break out of a while loop, print it and
|
||
continue. */
|
||
if (list->control_type == break_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, "loop_break");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* A while command. Recursively print its subcommands and
|
||
continue. */
|
||
if (list->control_type == while_control
|
||
|| list->control_type == while_stepping_control)
|
||
{
|
||
/* For while-stepping, the line includes the 'while-stepping'
|
||
token. See comment in process_next_line for explanation.
|
||
Here, take care not print 'while-stepping' twice. */
|
||
if (list->control_type == while_control)
|
||
ui_out_field_fmt (uiout, NULL, "while %s", list->line);
|
||
else
|
||
ui_out_field_string (uiout, NULL, list->line);
|
||
ui_out_text (uiout, "\n");
|
||
print_command_lines (uiout, *list->body_list, depth + 1);
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* An if command. Recursively print both arms before
|
||
continueing. */
|
||
if (list->control_type == if_control)
|
||
{
|
||
ui_out_field_fmt (uiout, NULL, "if %s", list->line);
|
||
ui_out_text (uiout, "\n");
|
||
/* The true arm. */
|
||
print_command_lines (uiout, list->body_list[0], depth + 1);
|
||
|
||
/* Show the false arm if it exists. */
|
||
if (list->body_count == 2)
|
||
{
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "else");
|
||
ui_out_text (uiout, "\n");
|
||
print_command_lines (uiout, list->body_list[1], depth + 1);
|
||
}
|
||
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* A commands command. Print the breakpoint commands and
|
||
continue. */
|
||
if (list->control_type == commands_control)
|
||
{
|
||
if (*(list->line))
|
||
ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
|
||
else
|
||
ui_out_field_string (uiout, NULL, "commands");
|
||
ui_out_text (uiout, "\n");
|
||
print_command_lines (uiout, *list->body_list, depth + 1);
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
if (list->control_type == python_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, "python");
|
||
ui_out_text (uiout, "\n");
|
||
/* Don't indent python code at all. */
|
||
print_command_lines (uiout, *list->body_list, 0);
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
if (list->control_type == compile_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, "compile expression");
|
||
ui_out_text (uiout, "\n");
|
||
print_command_lines (uiout, *list->body_list, 0);
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
if (list->control_type == guile_control)
|
||
{
|
||
ui_out_field_string (uiout, NULL, "guile");
|
||
ui_out_text (uiout, "\n");
|
||
print_command_lines (uiout, *list->body_list, depth + 1);
|
||
if (depth)
|
||
ui_out_spaces (uiout, 2 * depth);
|
||
ui_out_field_string (uiout, NULL, "end");
|
||
ui_out_text (uiout, "\n");
|
||
list = list->next;
|
||
continue;
|
||
}
|
||
|
||
/* Ignore illegal command type and try next. */
|
||
list = list->next;
|
||
} /* while (list) */
|
||
}
|
||
|
||
/* Handle pre-post hooks. */
|
||
|
||
static void
|
||
clear_hook_in_cleanup (void *data)
|
||
{
|
||
struct cmd_list_element *c = (struct cmd_list_element *) data;
|
||
|
||
c->hook_in = 0; /* Allow hook to work again once it is complete. */
|
||
}
|
||
|
||
void
|
||
execute_cmd_pre_hook (struct cmd_list_element *c)
|
||
{
|
||
if ((c->hook_pre) && (!c->hook_in))
|
||
{
|
||
struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
|
||
c->hook_in = 1; /* Prevent recursive hooking. */
|
||
execute_user_command (c->hook_pre, (char *) 0);
|
||
do_cleanups (cleanups);
|
||
}
|
||
}
|
||
|
||
void
|
||
execute_cmd_post_hook (struct cmd_list_element *c)
|
||
{
|
||
if ((c->hook_post) && (!c->hook_in))
|
||
{
|
||
struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
|
||
|
||
c->hook_in = 1; /* Prevent recursive hooking. */
|
||
execute_user_command (c->hook_post, (char *) 0);
|
||
do_cleanups (cleanups);
|
||
}
|
||
}
|
||
|
||
/* Execute the command in CMD. */
|
||
static void
|
||
do_restore_user_call_depth (void * call_depth)
|
||
{
|
||
int *depth = (int *) call_depth;
|
||
|
||
(*depth)--;
|
||
if ((*depth) == 0)
|
||
in_user_command = 0;
|
||
}
|
||
|
||
|
||
void
|
||
execute_user_command (struct cmd_list_element *c, char *args)
|
||
{
|
||
struct command_line *cmdlines;
|
||
struct cleanup *old_chain;
|
||
enum command_control_type ret;
|
||
static int user_call_depth = 0;
|
||
extern unsigned int max_user_call_depth;
|
||
|
||
cmdlines = c->user_commands;
|
||
if (cmdlines == 0)
|
||
/* Null command */
|
||
return;
|
||
|
||
old_chain = setup_user_args (args);
|
||
|
||
if (++user_call_depth > max_user_call_depth)
|
||
error (_("Max user call depth exceeded -- command aborted."));
|
||
|
||
make_cleanup (do_restore_user_call_depth, &user_call_depth);
|
||
|
||
/* Set the instream to 0, indicating execution of a
|
||
user-defined function. */
|
||
make_cleanup (do_restore_instream_cleanup, instream);
|
||
instream = (FILE *) 0;
|
||
|
||
/* Also set the global in_user_command, so that NULL instream is
|
||
not confused with Insight. */
|
||
in_user_command = 1;
|
||
|
||
make_cleanup_restore_integer (&interpreter_async);
|
||
interpreter_async = 0;
|
||
|
||
command_nest_depth++;
|
||
while (cmdlines)
|
||
{
|
||
ret = execute_control_command (cmdlines);
|
||
if (ret != simple_control && ret != break_control)
|
||
{
|
||
warning (_("Error executing canned sequence of commands."));
|
||
break;
|
||
}
|
||
cmdlines = cmdlines->next;
|
||
}
|
||
command_nest_depth--;
|
||
do_cleanups (old_chain);
|
||
}
|
||
|
||
/* This function is called every time GDB prints a prompt. It ensures
|
||
that errors and the like do not confuse the command tracing. */
|
||
|
||
void
|
||
reset_command_nest_depth (void)
|
||
{
|
||
command_nest_depth = 1;
|
||
|
||
/* Just in case. */
|
||
suppress_next_print_command_trace = 0;
|
||
}
|
||
|
||
/* Print the command, prefixed with '+' to represent the call depth.
|
||
This is slightly complicated because this function may be called
|
||
from execute_command and execute_control_command. Unfortunately
|
||
execute_command also prints the top level control commands.
|
||
In these cases execute_command will call execute_control_command
|
||
via while_command or if_command. Inner levels of 'if' and 'while'
|
||
are dealt with directly. Therefore we can use these functions
|
||
to determine whether the command has been printed already or not. */
|
||
void
|
||
print_command_trace (const char *cmd)
|
||
{
|
||
int i;
|
||
|
||
if (suppress_next_print_command_trace)
|
||
{
|
||
suppress_next_print_command_trace = 0;
|
||
return;
|
||
}
|
||
|
||
if (!source_verbose && !trace_commands)
|
||
return;
|
||
|
||
for (i=0; i < command_nest_depth; i++)
|
||
printf_filtered ("+");
|
||
|
||
printf_filtered ("%s\n", cmd);
|
||
}
|
||
|
||
enum command_control_type
|
||
execute_control_command (struct command_line *cmd)
|
||
{
|
||
struct expression *expr;
|
||
struct command_line *current;
|
||
struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
|
||
struct value *val;
|
||
struct value *val_mark;
|
||
int loop;
|
||
enum command_control_type ret;
|
||
char *new_line;
|
||
|
||
/* Start by assuming failure, if a problem is detected, the code
|
||
below will simply "break" out of the switch. */
|
||
ret = invalid_control;
|
||
|
||
switch (cmd->control_type)
|
||
{
|
||
case simple_control:
|
||
/* A simple command, execute it and return. */
|
||
new_line = insert_args (cmd->line);
|
||
if (!new_line)
|
||
break;
|
||
make_cleanup (free_current_contents, &new_line);
|
||
execute_command (new_line, 0);
|
||
ret = cmd->control_type;
|
||
break;
|
||
|
||
case continue_control:
|
||
print_command_trace ("loop_continue");
|
||
|
||
/* Return for "continue", and "break" so we can either
|
||
continue the loop at the top, or break out. */
|
||
ret = cmd->control_type;
|
||
break;
|
||
|
||
case break_control:
|
||
print_command_trace ("loop_break");
|
||
|
||
/* Return for "continue", and "break" so we can either
|
||
continue the loop at the top, or break out. */
|
||
ret = cmd->control_type;
|
||
break;
|
||
|
||
case while_control:
|
||
{
|
||
int len = strlen (cmd->line) + 7;
|
||
char *buffer = (char *) alloca (len);
|
||
|
||
xsnprintf (buffer, len, "while %s", cmd->line);
|
||
print_command_trace (buffer);
|
||
|
||
/* Parse the loop control expression for the while statement. */
|
||
new_line = insert_args (cmd->line);
|
||
if (!new_line)
|
||
break;
|
||
make_cleanup (free_current_contents, &new_line);
|
||
expr = parse_expression (new_line);
|
||
make_cleanup (free_current_contents, &expr);
|
||
|
||
ret = simple_control;
|
||
loop = 1;
|
||
|
||
/* Keep iterating so long as the expression is true. */
|
||
while (loop == 1)
|
||
{
|
||
int cond_result;
|
||
|
||
QUIT;
|
||
|
||
/* Evaluate the expression. */
|
||
val_mark = value_mark ();
|
||
val = evaluate_expression (expr);
|
||
cond_result = value_true (val);
|
||
value_free_to_mark (val_mark);
|
||
|
||
/* If the value is false, then break out of the loop. */
|
||
if (!cond_result)
|
||
break;
|
||
|
||
/* Execute the body of the while statement. */
|
||
current = *cmd->body_list;
|
||
while (current)
|
||
{
|
||
command_nest_depth++;
|
||
ret = execute_control_command (current);
|
||
command_nest_depth--;
|
||
|
||
/* If we got an error, or a "break" command, then stop
|
||
looping. */
|
||
if (ret == invalid_control || ret == break_control)
|
||
{
|
||
loop = 0;
|
||
break;
|
||
}
|
||
|
||
/* If we got a "continue" command, then restart the loop
|
||
at this point. */
|
||
if (ret == continue_control)
|
||
break;
|
||
|
||
/* Get the next statement. */
|
||
current = current->next;
|
||
}
|
||
}
|
||
|
||
/* Reset RET so that we don't recurse the break all the way down. */
|
||
if (ret == break_control)
|
||
ret = simple_control;
|
||
|
||
break;
|
||
}
|
||
|
||
case if_control:
|
||
{
|
||
int len = strlen (cmd->line) + 4;
|
||
char *buffer = (char *) alloca (len);
|
||
|
||
xsnprintf (buffer, len, "if %s", cmd->line);
|
||
print_command_trace (buffer);
|
||
|
||
new_line = insert_args (cmd->line);
|
||
if (!new_line)
|
||
break;
|
||
make_cleanup (free_current_contents, &new_line);
|
||
/* Parse the conditional for the if statement. */
|
||
expr = parse_expression (new_line);
|
||
make_cleanup (free_current_contents, &expr);
|
||
|
||
current = NULL;
|
||
ret = simple_control;
|
||
|
||
/* Evaluate the conditional. */
|
||
val_mark = value_mark ();
|
||
val = evaluate_expression (expr);
|
||
|
||
/* Choose which arm to take commands from based on the value
|
||
of the conditional expression. */
|
||
if (value_true (val))
|
||
current = *cmd->body_list;
|
||
else if (cmd->body_count == 2)
|
||
current = *(cmd->body_list + 1);
|
||
value_free_to_mark (val_mark);
|
||
|
||
/* Execute commands in the given arm. */
|
||
while (current)
|
||
{
|
||
command_nest_depth++;
|
||
ret = execute_control_command (current);
|
||
command_nest_depth--;
|
||
|
||
/* If we got an error, get out. */
|
||
if (ret != simple_control)
|
||
break;
|
||
|
||
/* Get the next statement in the body. */
|
||
current = current->next;
|
||
}
|
||
|
||
break;
|
||
}
|
||
|
||
case commands_control:
|
||
{
|
||
/* Breakpoint commands list, record the commands in the
|
||
breakpoint's command list and return. */
|
||
new_line = insert_args (cmd->line);
|
||
if (!new_line)
|
||
break;
|
||
make_cleanup (free_current_contents, &new_line);
|
||
ret = commands_from_control_command (new_line, cmd);
|
||
break;
|
||
}
|
||
|
||
case compile_control:
|
||
eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
|
||
cmd->control_u.compile.scope_data);
|
||
ret = simple_control;
|
||
break;
|
||
|
||
case python_control:
|
||
case guile_control:
|
||
{
|
||
eval_ext_lang_from_control_command (cmd);
|
||
ret = simple_control;
|
||
break;
|
||
}
|
||
|
||
default:
|
||
warning (_("Invalid control type in canned commands structure."));
|
||
break;
|
||
}
|
||
|
||
do_cleanups (old_chain);
|
||
|
||
return ret;
|
||
}
|
||
|
||
/* Like execute_control_command, but first set
|
||
suppress_next_print_command_trace. */
|
||
|
||
enum command_control_type
|
||
execute_control_command_untraced (struct command_line *cmd)
|
||
{
|
||
suppress_next_print_command_trace = 1;
|
||
return execute_control_command (cmd);
|
||
}
|
||
|
||
|
||
/* "while" command support. Executes a body of statements while the
|
||
loop condition is nonzero. */
|
||
|
||
static void
|
||
while_command (char *arg, int from_tty)
|
||
{
|
||
struct command_line *command = NULL;
|
||
struct cleanup *old_chain;
|
||
|
||
control_level = 1;
|
||
command = get_command_line (while_control, arg);
|
||
|
||
if (command == NULL)
|
||
return;
|
||
|
||
old_chain = make_cleanup_restore_integer (&interpreter_async);
|
||
interpreter_async = 0;
|
||
|
||
execute_control_command_untraced (command);
|
||
free_command_lines (&command);
|
||
|
||
do_cleanups (old_chain);
|
||
}
|
||
|
||
/* "if" command support. Execute either the true or false arm depending
|
||
on the value of the if conditional. */
|
||
|
||
static void
|
||
if_command (char *arg, int from_tty)
|
||
{
|
||
struct command_line *command = NULL;
|
||
struct cleanup *old_chain;
|
||
|
||
control_level = 1;
|
||
command = get_command_line (if_control, arg);
|
||
|
||
if (command == NULL)
|
||
return;
|
||
|
||
old_chain = make_cleanup_restore_integer (&interpreter_async);
|
||
interpreter_async = 0;
|
||
|
||
execute_control_command_untraced (command);
|
||
free_command_lines (&command);
|
||
|
||
do_cleanups (old_chain);
|
||
}
|
||
|
||
/* Cleanup */
|
||
static void
|
||
arg_cleanup (void *ignore)
|
||
{
|
||
struct user_args *oargs = user_args;
|
||
|
||
if (!user_args)
|
||
internal_error (__FILE__, __LINE__,
|
||
_("arg_cleanup called with no user args.\n"));
|
||
|
||
user_args = user_args->next;
|
||
xfree (oargs->command);
|
||
xfree (oargs);
|
||
}
|
||
|
||
/* Bind the incomming arguments for a user defined command to
|
||
$arg0, $arg1 ... $argMAXUSERARGS. */
|
||
|
||
static struct cleanup *
|
||
setup_user_args (char *p)
|
||
{
|
||
struct user_args *args;
|
||
struct cleanup *old_chain;
|
||
unsigned int arg_count = 0;
|
||
|
||
args = XNEW (struct user_args);
|
||
memset (args, 0, sizeof (struct user_args));
|
||
|
||
args->next = user_args;
|
||
user_args = args;
|
||
|
||
old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
|
||
|
||
if (p == NULL)
|
||
return old_chain;
|
||
|
||
user_args->command = p = xstrdup (p);
|
||
|
||
while (*p)
|
||
{
|
||
char *start_arg;
|
||
int squote = 0;
|
||
int dquote = 0;
|
||
int bsquote = 0;
|
||
|
||
if (arg_count >= MAXUSERARGS)
|
||
error (_("user defined function may only have %d arguments."),
|
||
MAXUSERARGS);
|
||
|
||
/* Strip whitespace. */
|
||
while (*p == ' ' || *p == '\t')
|
||
p++;
|
||
|
||
/* P now points to an argument. */
|
||
start_arg = p;
|
||
user_args->a[arg_count].arg = p;
|
||
|
||
/* Get to the end of this argument. */
|
||
while (*p)
|
||
{
|
||
if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
|
||
break;
|
||
else
|
||
{
|
||
if (bsquote)
|
||
bsquote = 0;
|
||
else if (*p == '\\')
|
||
bsquote = 1;
|
||
else if (squote)
|
||
{
|
||
if (*p == '\'')
|
||
squote = 0;
|
||
}
|
||
else if (dquote)
|
||
{
|
||
if (*p == '"')
|
||
dquote = 0;
|
||
}
|
||
else
|
||
{
|
||
if (*p == '\'')
|
||
squote = 1;
|
||
else if (*p == '"')
|
||
dquote = 1;
|
||
}
|
||
p++;
|
||
}
|
||
}
|
||
|
||
user_args->a[arg_count].len = p - start_arg;
|
||
arg_count++;
|
||
user_args->count++;
|
||
}
|
||
return old_chain;
|
||
}
|
||
|
||
/* Given character string P, return a point to the first argument
|
||
($arg), or NULL if P contains no arguments. */
|
||
|
||
static char *
|
||
locate_arg (char *p)
|
||
{
|
||
while ((p = strchr (p, '$')))
|
||
{
|
||
if (startswith (p, "$arg")
|
||
&& (isdigit (p[4]) || p[4] == 'c'))
|
||
return p;
|
||
p++;
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/* Insert the user defined arguments stored in user_arg into the $arg
|
||
arguments found in line, with the updated copy being placed into
|
||
nline. */
|
||
|
||
static char *
|
||
insert_args (char *line)
|
||
{
|
||
char *p, *save_line, *new_line;
|
||
unsigned len, i;
|
||
|
||
/* If we are not in a user-defined function, treat $argc, $arg0, et
|
||
cetera as normal convenience variables. */
|
||
if (user_args == NULL)
|
||
return xstrdup (line);
|
||
|
||
/* First we need to know how much memory to allocate for the new
|
||
line. */
|
||
save_line = line;
|
||
len = 0;
|
||
while ((p = locate_arg (line)))
|
||
{
|
||
len += p - line;
|
||
i = p[4] - '0';
|
||
|
||
if (p[4] == 'c')
|
||
{
|
||
/* $argc. Number will be <=10. */
|
||
len += user_args->count == 10 ? 2 : 1;
|
||
}
|
||
else if (i >= user_args->count)
|
||
{
|
||
error (_("Missing argument %d in user function."), i);
|
||
return NULL;
|
||
}
|
||
else
|
||
{
|
||
len += user_args->a[i].len;
|
||
}
|
||
line = p + 5;
|
||
}
|
||
|
||
/* Don't forget the tail. */
|
||
len += strlen (line);
|
||
|
||
/* Allocate space for the new line and fill it in. */
|
||
new_line = (char *) xmalloc (len + 1);
|
||
if (new_line == NULL)
|
||
return NULL;
|
||
|
||
/* Restore pointer to beginning of old line. */
|
||
line = save_line;
|
||
|
||
/* Save pointer to beginning of new line. */
|
||
save_line = new_line;
|
||
|
||
while ((p = locate_arg (line)))
|
||
{
|
||
int i, len;
|
||
|
||
memcpy (new_line, line, p - line);
|
||
new_line += p - line;
|
||
|
||
if (p[4] == 'c')
|
||
{
|
||
gdb_assert (user_args->count >= 0 && user_args->count <= 10);
|
||
if (user_args->count == 10)
|
||
{
|
||
*(new_line++) = '1';
|
||
*(new_line++) = '0';
|
||
}
|
||
else
|
||
*(new_line++) = user_args->count + '0';
|
||
}
|
||
else
|
||
{
|
||
i = p[4] - '0';
|
||
len = user_args->a[i].len;
|
||
if (len)
|
||
{
|
||
memcpy (new_line, user_args->a[i].arg, len);
|
||
new_line += len;
|
||
}
|
||
}
|
||
line = p + 5;
|
||
}
|
||
/* Don't forget the tail. */
|
||
strcpy (new_line, line);
|
||
|
||
/* Return a pointer to the beginning of the new line. */
|
||
return save_line;
|
||
}
|
||
|
||
|
||
/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
|
||
code bodies. This is typically used when we encounter an "else"
|
||
clause for an "if" command. */
|
||
|
||
static void
|
||
realloc_body_list (struct command_line *command, int new_length)
|
||
{
|
||
int n;
|
||
struct command_line **body_list;
|
||
|
||
n = command->body_count;
|
||
|
||
/* Nothing to do? */
|
||
if (new_length <= n)
|
||
return;
|
||
|
||
body_list = XCNEWVEC (struct command_line *, new_length);
|
||
|
||
memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
|
||
|
||
xfree (command->body_list);
|
||
command->body_list = body_list;
|
||
command->body_count = new_length;
|
||
}
|
||
|
||
/* Read next line from stdout. Passed to read_command_line_1 and
|
||
recurse_read_control_structure whenever we need to read commands
|
||
from stdout. */
|
||
|
||
static char *
|
||
read_next_line (void)
|
||
{
|
||
char *prompt_ptr, control_prompt[256];
|
||
int i = 0;
|
||
|
||
if (control_level >= 254)
|
||
error (_("Control nesting too deep!"));
|
||
|
||
/* Set a prompt based on the nesting of the control commands. */
|
||
if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
|
||
{
|
||
for (i = 0; i < control_level; i++)
|
||
control_prompt[i] = ' ';
|
||
control_prompt[i] = '>';
|
||
control_prompt[i + 1] = '\0';
|
||
prompt_ptr = (char *) &control_prompt[0];
|
||
}
|
||
else
|
||
prompt_ptr = NULL;
|
||
|
||
return command_line_input (prompt_ptr, instream == stdin, "commands");
|
||
}
|
||
|
||
/* Process one input line. If the command is an "end", return such an
|
||
indication to the caller. If PARSE_COMMANDS is true, strip leading
|
||
whitespace (trailing whitespace is always stripped) in the line,
|
||
attempt to recognize GDB control commands, and also return an
|
||
indication if the command is an "else" or a nop.
|
||
|
||
Otherwise, only "end" is recognized. */
|
||
|
||
static enum misc_command_type
|
||
process_next_line (char *p, struct command_line **command, int parse_commands,
|
||
void (*validator)(char *, void *), void *closure)
|
||
{
|
||
char *p_end;
|
||
char *p_start;
|
||
int not_handled = 0;
|
||
|
||
/* Not sure what to do here. */
|
||
if (p == NULL)
|
||
return end_command;
|
||
|
||
/* Strip trailing whitespace. */
|
||
p_end = p + strlen (p);
|
||
while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
|
||
p_end--;
|
||
|
||
p_start = p;
|
||
/* Strip leading whitespace. */
|
||
while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
|
||
p_start++;
|
||
|
||
/* 'end' is always recognized, regardless of parse_commands value.
|
||
We also permit whitespace before end and after. */
|
||
if (p_end - p_start == 3 && startswith (p_start, "end"))
|
||
return end_command;
|
||
|
||
if (parse_commands)
|
||
{
|
||
/* If commands are parsed, we skip initial spaces. Otherwise,
|
||
which is the case for Python commands and documentation
|
||
(see the 'document' command), spaces are preserved. */
|
||
p = p_start;
|
||
|
||
/* Blanks and comments don't really do anything, but we need to
|
||
distinguish them from else, end and other commands which can
|
||
be executed. */
|
||
if (p_end == p || p[0] == '#')
|
||
return nop_command;
|
||
|
||
/* Is the else clause of an if control structure? */
|
||
if (p_end - p == 4 && startswith (p, "else"))
|
||
return else_command;
|
||
|
||
/* Check for while, if, break, continue, etc and build a new
|
||
command line structure for them. */
|
||
if ((p_end - p >= 14 && startswith (p, "while-stepping"))
|
||
|| (p_end - p >= 8 && startswith (p, "stepping"))
|
||
|| (p_end - p >= 2 && startswith (p, "ws")))
|
||
{
|
||
/* Because validate_actionline and encode_action lookup
|
||
command's line as command, we need the line to
|
||
include 'while-stepping'.
|
||
|
||
For 'ws' alias, the command will have 'ws', not expanded
|
||
to 'while-stepping'. This is intentional -- we don't
|
||
really want frontend to send a command list with 'ws',
|
||
and next break-info returning command line with
|
||
'while-stepping'. This should work, but might cause the
|
||
breakpoint to be marked as changed while it's actually
|
||
not. */
|
||
*command = build_command_line (while_stepping_control, p);
|
||
}
|
||
else if (p_end - p > 5 && startswith (p, "while"))
|
||
{
|
||
char *first_arg;
|
||
|
||
first_arg = p + 5;
|
||
while (first_arg < p_end && isspace (*first_arg))
|
||
first_arg++;
|
||
*command = build_command_line (while_control, first_arg);
|
||
}
|
||
else if (p_end - p > 2 && startswith (p, "if"))
|
||
{
|
||
char *first_arg;
|
||
|
||
first_arg = p + 2;
|
||
while (first_arg < p_end && isspace (*first_arg))
|
||
first_arg++;
|
||
*command = build_command_line (if_control, first_arg);
|
||
}
|
||
else if (p_end - p >= 8 && startswith (p, "commands"))
|
||
{
|
||
char *first_arg;
|
||
|
||
first_arg = p + 8;
|
||
while (first_arg < p_end && isspace (*first_arg))
|
||
first_arg++;
|
||
*command = build_command_line (commands_control, first_arg);
|
||
}
|
||
else if (p_end - p == 6 && startswith (p, "python"))
|
||
{
|
||
/* Note that we ignore the inline "python command" form
|
||
here. */
|
||
*command = build_command_line (python_control, "");
|
||
}
|
||
else if (p_end - p == 6 && startswith (p, "compile"))
|
||
{
|
||
/* Note that we ignore the inline "compile command" form
|
||
here. */
|
||
*command = build_command_line (compile_control, "");
|
||
(*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
|
||
}
|
||
|
||
else if (p_end - p == 5 && startswith (p, "guile"))
|
||
{
|
||
/* Note that we ignore the inline "guile command" form here. */
|
||
*command = build_command_line (guile_control, "");
|
||
}
|
||
else if (p_end - p == 10 && startswith (p, "loop_break"))
|
||
{
|
||
*command = XNEW (struct command_line);
|
||
(*command)->next = NULL;
|
||
(*command)->line = NULL;
|
||
(*command)->control_type = break_control;
|
||
(*command)->body_count = 0;
|
||
(*command)->body_list = NULL;
|
||
}
|
||
else if (p_end - p == 13 && startswith (p, "loop_continue"))
|
||
{
|
||
*command = XNEW (struct command_line);
|
||
(*command)->next = NULL;
|
||
(*command)->line = NULL;
|
||
(*command)->control_type = continue_control;
|
||
(*command)->body_count = 0;
|
||
(*command)->body_list = NULL;
|
||
}
|
||
else
|
||
not_handled = 1;
|
||
}
|
||
|
||
if (!parse_commands || not_handled)
|
||
{
|
||
/* A normal command. */
|
||
*command = XNEW (struct command_line);
|
||
(*command)->next = NULL;
|
||
(*command)->line = savestring (p, p_end - p);
|
||
(*command)->control_type = simple_control;
|
||
(*command)->body_count = 0;
|
||
(*command)->body_list = NULL;
|
||
}
|
||
|
||
if (validator)
|
||
{
|
||
|
||
TRY
|
||
{
|
||
validator ((*command)->line, closure);
|
||
}
|
||
CATCH (ex, RETURN_MASK_ALL)
|
||
{
|
||
xfree (*command);
|
||
throw_exception (ex);
|
||
}
|
||
END_CATCH
|
||
}
|
||
|
||
/* Nothing special. */
|
||
return ok_command;
|
||
}
|
||
|
||
/* Recursively read in the control structures and create a
|
||
command_line structure from them. Use read_next_line_func to
|
||
obtain lines of the command. */
|
||
|
||
static enum command_control_type
|
||
recurse_read_control_structure (char * (*read_next_line_func) (void),
|
||
struct command_line *current_cmd,
|
||
void (*validator)(char *, void *),
|
||
void *closure)
|
||
{
|
||
int current_body, i;
|
||
enum misc_command_type val;
|
||
enum command_control_type ret;
|
||
struct command_line **body_ptr, *child_tail, *next;
|
||
|
||
child_tail = NULL;
|
||
current_body = 1;
|
||
|
||
/* Sanity checks. */
|
||
if (current_cmd->control_type == simple_control)
|
||
error (_("Recursed on a simple control type."));
|
||
|
||
if (current_body > current_cmd->body_count)
|
||
error (_("Allocated body is smaller than this command type needs."));
|
||
|
||
/* Read lines from the input stream and build control structures. */
|
||
while (1)
|
||
{
|
||
dont_repeat ();
|
||
|
||
next = NULL;
|
||
val = process_next_line (read_next_line_func (), &next,
|
||
current_cmd->control_type != python_control
|
||
&& current_cmd->control_type != guile_control
|
||
&& current_cmd->control_type != compile_control,
|
||
validator, closure);
|
||
|
||
/* Just skip blanks and comments. */
|
||
if (val == nop_command)
|
||
continue;
|
||
|
||
if (val == end_command)
|
||
{
|
||
if (multi_line_command_p (current_cmd->control_type))
|
||
{
|
||
/* Success reading an entire canned sequence of commands. */
|
||
ret = simple_control;
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
ret = invalid_control;
|
||
break;
|
||
}
|
||
}
|
||
|
||
/* Not the end of a control structure. */
|
||
if (val == else_command)
|
||
{
|
||
if (current_cmd->control_type == if_control
|
||
&& current_body == 1)
|
||
{
|
||
realloc_body_list (current_cmd, 2);
|
||
current_body = 2;
|
||
child_tail = NULL;
|
||
continue;
|
||
}
|
||
else
|
||
{
|
||
ret = invalid_control;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (child_tail)
|
||
{
|
||
child_tail->next = next;
|
||
}
|
||
else
|
||
{
|
||
body_ptr = current_cmd->body_list;
|
||
for (i = 1; i < current_body; i++)
|
||
body_ptr++;
|
||
|
||
*body_ptr = next;
|
||
|
||
}
|
||
|
||
child_tail = next;
|
||
|
||
/* If the latest line is another control structure, then recurse
|
||
on it. */
|
||
if (multi_line_command_p (next->control_type))
|
||
{
|
||
control_level++;
|
||
ret = recurse_read_control_structure (read_next_line_func, next,
|
||
validator, closure);
|
||
control_level--;
|
||
|
||
if (ret != simple_control)
|
||
break;
|
||
}
|
||
}
|
||
|
||
dont_repeat ();
|
||
|
||
return ret;
|
||
}
|
||
|
||
static void
|
||
restore_interp (void *arg)
|
||
{
|
||
interp_set_temp (interp_name ((struct interp *)arg));
|
||
}
|
||
|
||
/* Read lines from the input stream and accumulate them in a chain of
|
||
struct command_line's, which is then returned. For input from a
|
||
terminal, the special command "end" is used to mark the end of the
|
||
input, and is not included in the returned chain of commands.
|
||
|
||
If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
|
||
is always stripped) in the line and attempt to recognize GDB control
|
||
commands. Otherwise, only "end" is recognized. */
|
||
|
||
#define END_MESSAGE "End with a line saying just \"end\"."
|
||
|
||
struct command_line *
|
||
read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
|
||
void (*validator)(char *, void *), void *closure)
|
||
{
|
||
struct command_line *head;
|
||
|
||
if (from_tty && input_from_terminal_p ())
|
||
{
|
||
if (deprecated_readline_begin_hook)
|
||
{
|
||
/* Note - intentional to merge messages with no newline. */
|
||
(*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg,
|
||
END_MESSAGE);
|
||
}
|
||
else
|
||
{
|
||
printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
|
||
gdb_flush (gdb_stdout);
|
||
}
|
||
}
|
||
|
||
|
||
/* Reading commands assumes the CLI behavior, so temporarily
|
||
override the current interpreter with CLI. */
|
||
if (current_interp_named_p (INTERP_CONSOLE))
|
||
head = read_command_lines_1 (read_next_line, parse_commands,
|
||
validator, closure);
|
||
else
|
||
{
|
||
struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
|
||
struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
|
||
|
||
head = read_command_lines_1 (read_next_line, parse_commands,
|
||
validator, closure);
|
||
do_cleanups (old_chain);
|
||
}
|
||
|
||
if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
|
||
{
|
||
(*deprecated_readline_end_hook) ();
|
||
}
|
||
return (head);
|
||
}
|
||
|
||
/* Act the same way as read_command_lines, except that each new line is
|
||
obtained using READ_NEXT_LINE_FUNC. */
|
||
|
||
struct command_line *
|
||
read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
|
||
void (*validator)(char *, void *), void *closure)
|
||
{
|
||
struct command_line *head, *tail, *next;
|
||
struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
|
||
enum command_control_type ret;
|
||
enum misc_command_type val;
|
||
|
||
control_level = 0;
|
||
head = tail = NULL;
|
||
|
||
while (1)
|
||
{
|
||
dont_repeat ();
|
||
val = process_next_line (read_next_line_func (), &next, parse_commands,
|
||
validator, closure);
|
||
|
||
/* Ignore blank lines or comments. */
|
||
if (val == nop_command)
|
||
continue;
|
||
|
||
if (val == end_command)
|
||
{
|
||
ret = simple_control;
|
||
break;
|
||
}
|
||
|
||
if (val != ok_command)
|
||
{
|
||
ret = invalid_control;
|
||
break;
|
||
}
|
||
|
||
if (multi_line_command_p (next->control_type))
|
||
{
|
||
control_level++;
|
||
ret = recurse_read_control_structure (read_next_line_func, next,
|
||
validator, closure);
|
||
control_level--;
|
||
|
||
if (ret == invalid_control)
|
||
break;
|
||
}
|
||
|
||
if (tail)
|
||
{
|
||
tail->next = next;
|
||
}
|
||
else
|
||
{
|
||
head = next;
|
||
make_cleanup_free_command_lines (&head);
|
||
}
|
||
tail = next;
|
||
}
|
||
|
||
dont_repeat ();
|
||
|
||
if (ret != invalid_control)
|
||
discard_cleanups (old_chain);
|
||
else
|
||
do_cleanups (old_chain);
|
||
|
||
return head;
|
||
}
|
||
|
||
/* Free a chain of struct command_line's. */
|
||
|
||
void
|
||
free_command_lines (struct command_line **lptr)
|
||
{
|
||
struct command_line *l = *lptr;
|
||
struct command_line *next;
|
||
struct command_line **blist;
|
||
int i;
|
||
|
||
while (l)
|
||
{
|
||
if (l->body_count > 0)
|
||
{
|
||
blist = l->body_list;
|
||
for (i = 0; i < l->body_count; i++, blist++)
|
||
free_command_lines (blist);
|
||
}
|
||
next = l->next;
|
||
xfree (l->line);
|
||
xfree (l);
|
||
l = next;
|
||
}
|
||
*lptr = NULL;
|
||
}
|
||
|
||
static void
|
||
do_free_command_lines_cleanup (void *arg)
|
||
{
|
||
free_command_lines ((struct command_line **) arg);
|
||
}
|
||
|
||
struct cleanup *
|
||
make_cleanup_free_command_lines (struct command_line **arg)
|
||
{
|
||
return make_cleanup (do_free_command_lines_cleanup, arg);
|
||
}
|
||
|
||
struct command_line *
|
||
copy_command_lines (struct command_line *cmds)
|
||
{
|
||
struct command_line *result = NULL;
|
||
|
||
if (cmds)
|
||
{
|
||
result = XNEW (struct command_line);
|
||
|
||
result->next = copy_command_lines (cmds->next);
|
||
result->line = xstrdup (cmds->line);
|
||
result->control_type = cmds->control_type;
|
||
result->body_count = cmds->body_count;
|
||
if (cmds->body_count > 0)
|
||
{
|
||
int i;
|
||
|
||
result->body_list = XNEWVEC (struct command_line *, cmds->body_count);
|
||
|
||
for (i = 0; i < cmds->body_count; i++)
|
||
result->body_list[i] = copy_command_lines (cmds->body_list[i]);
|
||
}
|
||
else
|
||
result->body_list = NULL;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/* Validate that *COMNAME is a valid name for a command. Return the
|
||
containing command list, in case it starts with a prefix command.
|
||
The prefix must already exist. *COMNAME is advanced to point after
|
||
any prefix, and a NUL character overwrites the space after the
|
||
prefix. */
|
||
|
||
static struct cmd_list_element **
|
||
validate_comname (char **comname)
|
||
{
|
||
struct cmd_list_element **list = &cmdlist;
|
||
char *p, *last_word;
|
||
|
||
if (*comname == 0)
|
||
error_no_arg (_("name of command to define"));
|
||
|
||
/* Find the last word of the argument. */
|
||
p = *comname + strlen (*comname);
|
||
while (p > *comname && isspace (p[-1]))
|
||
p--;
|
||
while (p > *comname && !isspace (p[-1]))
|
||
p--;
|
||
last_word = p;
|
||
|
||
/* Find the corresponding command list. */
|
||
if (last_word != *comname)
|
||
{
|
||
struct cmd_list_element *c;
|
||
char saved_char;
|
||
const char *tem = *comname;
|
||
|
||
/* Separate the prefix and the command. */
|
||
saved_char = last_word[-1];
|
||
last_word[-1] = '\0';
|
||
|
||
c = lookup_cmd (&tem, cmdlist, "", 0, 1);
|
||
if (c->prefixlist == NULL)
|
||
error (_("\"%s\" is not a prefix command."), *comname);
|
||
|
||
list = c->prefixlist;
|
||
last_word[-1] = saved_char;
|
||
*comname = last_word;
|
||
}
|
||
|
||
p = *comname;
|
||
while (*p)
|
||
{
|
||
if (!isalnum (*p) && *p != '-' && *p != '_')
|
||
error (_("Junk in argument list: \"%s\""), p);
|
||
p++;
|
||
}
|
||
|
||
return list;
|
||
}
|
||
|
||
/* This is just a placeholder in the command data structures. */
|
||
static void
|
||
user_defined_command (char *ignore, int from_tty)
|
||
{
|
||
}
|
||
|
||
static void
|
||
define_command (char *comname, int from_tty)
|
||
{
|
||
#define MAX_TMPBUF 128
|
||
enum cmd_hook_type
|
||
{
|
||
CMD_NO_HOOK = 0,
|
||
CMD_PRE_HOOK,
|
||
CMD_POST_HOOK
|
||
};
|
||
struct command_line *cmds;
|
||
struct cmd_list_element *c, *newc, *hookc = 0, **list;
|
||
char *tem, *comfull;
|
||
const char *tem_c;
|
||
char tmpbuf[MAX_TMPBUF];
|
||
int hook_type = CMD_NO_HOOK;
|
||
int hook_name_size = 0;
|
||
|
||
#define HOOK_STRING "hook-"
|
||
#define HOOK_LEN 5
|
||
#define HOOK_POST_STRING "hookpost-"
|
||
#define HOOK_POST_LEN 9
|
||
|
||
comfull = comname;
|
||
list = validate_comname (&comname);
|
||
|
||
/* Look it up, and verify that we got an exact match. */
|
||
tem_c = comname;
|
||
c = lookup_cmd (&tem_c, *list, "", -1, 1);
|
||
if (c && strcmp (comname, c->name) != 0)
|
||
c = 0;
|
||
|
||
if (c)
|
||
{
|
||
int q;
|
||
|
||
if (c->theclass == class_user || c->theclass == class_alias)
|
||
q = query (_("Redefine command \"%s\"? "), c->name);
|
||
else
|
||
q = query (_("Really redefine built-in command \"%s\"? "), c->name);
|
||
if (!q)
|
||
error (_("Command \"%s\" not redefined."), c->name);
|
||
}
|
||
|
||
/* If this new command is a hook, then mark the command which it
|
||
is hooking. Note that we allow hooking `help' commands, so that
|
||
we can hook the `stop' pseudo-command. */
|
||
|
||
if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
|
||
{
|
||
hook_type = CMD_PRE_HOOK;
|
||
hook_name_size = HOOK_LEN;
|
||
}
|
||
else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
|
||
{
|
||
hook_type = CMD_POST_HOOK;
|
||
hook_name_size = HOOK_POST_LEN;
|
||
}
|
||
|
||
if (hook_type != CMD_NO_HOOK)
|
||
{
|
||
/* Look up cmd it hooks, and verify that we got an exact match. */
|
||
tem_c = comname + hook_name_size;
|
||
hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
|
||
if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
|
||
hookc = 0;
|
||
if (!hookc)
|
||
{
|
||
warning (_("Your new `%s' command does not "
|
||
"hook any existing command."),
|
||
comfull);
|
||
if (!query (_("Proceed? ")))
|
||
error (_("Not confirmed."));
|
||
}
|
||
}
|
||
|
||
comname = xstrdup (comname);
|
||
|
||
/* If the rest of the commands will be case insensitive, this one
|
||
should behave in the same manner. */
|
||
for (tem = comname; *tem; tem++)
|
||
if (isupper (*tem))
|
||
*tem = tolower (*tem);
|
||
|
||
xsnprintf (tmpbuf, sizeof (tmpbuf),
|
||
"Type commands for definition of \"%s\".", comfull);
|
||
cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
|
||
|
||
if (c && c->theclass == class_user)
|
||
free_command_lines (&c->user_commands);
|
||
|
||
newc = add_cmd (comname, class_user, user_defined_command,
|
||
(c && c->theclass == class_user)
|
||
? c->doc : xstrdup ("User-defined."), list);
|
||
newc->user_commands = cmds;
|
||
|
||
/* If this new command is a hook, then mark both commands as being
|
||
tied. */
|
||
if (hookc)
|
||
{
|
||
switch (hook_type)
|
||
{
|
||
case CMD_PRE_HOOK:
|
||
hookc->hook_pre = newc; /* Target gets hooked. */
|
||
newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
|
||
break;
|
||
case CMD_POST_HOOK:
|
||
hookc->hook_post = newc; /* Target gets hooked. */
|
||
newc->hookee_post = hookc; /* We are marked as hooking
|
||
target cmd. */
|
||
break;
|
||
default:
|
||
/* Should never come here as hookc would be 0. */
|
||
internal_error (__FILE__, __LINE__, _("bad switch"));
|
||
}
|
||
}
|
||
}
|
||
|
||
static void
|
||
document_command (char *comname, int from_tty)
|
||
{
|
||
struct command_line *doclines;
|
||
struct cmd_list_element *c, **list;
|
||
const char *tem;
|
||
char *comfull;
|
||
char tmpbuf[128];
|
||
|
||
comfull = comname;
|
||
list = validate_comname (&comname);
|
||
|
||
tem = comname;
|
||
c = lookup_cmd (&tem, *list, "", 0, 1);
|
||
|
||
if (c->theclass != class_user)
|
||
error (_("Command \"%s\" is built-in."), comfull);
|
||
|
||
xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
|
||
comfull);
|
||
doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
|
||
|
||
if (c->doc)
|
||
xfree ((char *) c->doc);
|
||
|
||
{
|
||
struct command_line *cl1;
|
||
int len = 0;
|
||
char *doc;
|
||
|
||
for (cl1 = doclines; cl1; cl1 = cl1->next)
|
||
len += strlen (cl1->line) + 1;
|
||
|
||
doc = (char *) xmalloc (len + 1);
|
||
*doc = 0;
|
||
|
||
for (cl1 = doclines; cl1; cl1 = cl1->next)
|
||
{
|
||
strcat (doc, cl1->line);
|
||
if (cl1->next)
|
||
strcat (doc, "\n");
|
||
}
|
||
|
||
c->doc = doc;
|
||
}
|
||
|
||
free_command_lines (&doclines);
|
||
}
|
||
|
||
struct source_cleanup_lines_args
|
||
{
|
||
int old_line;
|
||
const char *old_file;
|
||
};
|
||
|
||
static void
|
||
source_cleanup_lines (void *args)
|
||
{
|
||
struct source_cleanup_lines_args *p =
|
||
(struct source_cleanup_lines_args *) args;
|
||
|
||
source_line_number = p->old_line;
|
||
source_file_name = p->old_file;
|
||
}
|
||
|
||
/* Used to implement source_command. */
|
||
|
||
void
|
||
script_from_file (FILE *stream, const char *file)
|
||
{
|
||
struct cleanup *old_cleanups;
|
||
struct source_cleanup_lines_args old_lines;
|
||
|
||
if (stream == NULL)
|
||
internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
|
||
|
||
old_lines.old_line = source_line_number;
|
||
old_lines.old_file = source_file_name;
|
||
old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
|
||
source_line_number = 0;
|
||
source_file_name = file;
|
||
|
||
make_cleanup_restore_integer (&interpreter_async);
|
||
interpreter_async = 0;
|
||
|
||
{
|
||
|
||
TRY
|
||
{
|
||
read_command_file (stream);
|
||
}
|
||
CATCH (e, RETURN_MASK_ERROR)
|
||
{
|
||
/* Re-throw the error, but with the file name information
|
||
prepended. */
|
||
throw_error (e.error,
|
||
_("%s:%d: Error in sourced command file:\n%s"),
|
||
source_file_name, source_line_number, e.message);
|
||
}
|
||
END_CATCH
|
||
}
|
||
|
||
do_cleanups (old_cleanups);
|
||
}
|
||
|
||
/* Print the definition of user command C to STREAM. Or, if C is a
|
||
prefix command, show the definitions of all user commands under C
|
||
(recursively). PREFIX and NAME combined are the name of the
|
||
current command. */
|
||
void
|
||
show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
|
||
struct ui_file *stream)
|
||
{
|
||
struct command_line *cmdlines;
|
||
|
||
if (c->prefixlist != NULL)
|
||
{
|
||
const char *prefixname = c->prefixname;
|
||
|
||
for (c = *c->prefixlist; c != NULL; c = c->next)
|
||
if (c->theclass == class_user || c->prefixlist != NULL)
|
||
show_user_1 (c, prefixname, c->name, gdb_stdout);
|
||
return;
|
||
}
|
||
|
||
cmdlines = c->user_commands;
|
||
fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
|
||
|
||
if (!cmdlines)
|
||
return;
|
||
print_command_lines (current_uiout, cmdlines, 1);
|
||
fputs_filtered ("\n", stream);
|
||
}
|
||
|
||
|
||
|
||
initialize_file_ftype _initialize_cli_script;
|
||
|
||
void
|
||
_initialize_cli_script (void)
|
||
{
|
||
add_com ("document", class_support, document_command, _("\
|
||
Document a user-defined command.\n\
|
||
Give command name as argument. Give documentation on following lines.\n\
|
||
End with a line of just \"end\"."));
|
||
add_com ("define", class_support, define_command, _("\
|
||
Define a new command name. Command name is argument.\n\
|
||
Definition appears on following lines, one command per line.\n\
|
||
End with a line of just \"end\".\n\
|
||
Use the \"document\" command to give documentation for the new command.\n\
|
||
Commands defined in this way may have up to ten arguments."));
|
||
|
||
add_com ("while", class_support, while_command, _("\
|
||
Execute nested commands WHILE the conditional expression is non zero.\n\
|
||
The conditional expression must follow the word `while' and must in turn be\n\
|
||
followed by a new line. The nested commands must be entered one per line,\n\
|
||
and should be terminated by the word `end'."));
|
||
|
||
add_com ("if", class_support, if_command, _("\
|
||
Execute nested commands once IF the conditional expression is non zero.\n\
|
||
The conditional expression must follow the word `if' and must in turn be\n\
|
||
followed by a new line. The nested commands must be entered one per line,\n\
|
||
and should be terminated by the word 'else' or `end'. If an else clause\n\
|
||
is used, the same rules apply to its nested commands as to the first ones."));
|
||
}
|