void mi_execute_command (char *cmd, int from_tty) { struct mi_parse *command; struct captured_mi_execute_command_args args; struct ui_out *saved_uiout = uiout; /* This is to handle EOF (^D). We just quit gdb. */ /* FIXME: we should call some API function here. */ if (cmd == 0) quit_force (NULL, from_tty); command = mi_parse (cmd); if (command != NULL) { struct gdb_exception result; if (do_timings) { command->cmd_start = (struct mi_timestamp *) xmalloc (sizeof (struct mi_timestamp)); timestamp (command->cmd_start); } /* FIXME: cagney/1999-11-04: Can this use of catch_exceptions either be pushed even further down or even eliminated? */ args.command = command; result = catch_exception (uiout, captured_mi_execute_command, &args, RETURN_MASK_ALL); exception_print (gdb_stderr, result); if (args.action == EXECUTE_COMMAND_SUPRESS_PROMPT) { /* The command is executing synchronously. Bail out early suppressing the finished prompt. */ mi_parse_free (command); return; } if (result.reason < 0) { /* The command execution failed and error() was called somewhere. */ fputs_unfiltered (command->token, raw_stdout); fputs_unfiltered ("^error,msg=\"", raw_stdout); if (result.message == NULL) fputs_unfiltered ("unknown error", raw_stdout); else fputstr_unfiltered (result.message, '"', raw_stdout); fputs_unfiltered ("\"\n", raw_stdout); mi_out_rewind (uiout); } mi_parse_free (command); } fputs_unfiltered ("(gdb) \n", raw_stdout); gdb_flush (raw_stdout); /* Print any buffered hook code. */ /* ..... */ }
static void check_status_exception_catchpoint (struct bpstats *bs) { struct exception_catchpoint *self = (struct exception_catchpoint *) bs->breakpoint_at; char *type_name = NULL; bkpt_breakpoint_ops.check_status (bs); if (bs->stop == 0) return; if (self->pattern == NULL) return; TRY { struct value *typeinfo_arg; char *canon; fetch_probe_arguments (NULL, &typeinfo_arg); type_name = cplus_typename_from_type_info (typeinfo_arg); canon = cp_canonicalize_string (type_name); if (canon != NULL) { xfree (type_name); type_name = canon; } } CATCH (e, RETURN_MASK_ERROR) { exception_print (gdb_stderr, e); }
/* Called by the event loop to process a SIGHUP. */ static void async_disconnect (gdb_client_data arg) { volatile struct gdb_exception exception; TRY_CATCH (exception, RETURN_MASK_ALL) { quit_cover (); } if (exception.reason < 0) { fputs_filtered ("Could not kill the program being debugged", gdb_stderr); exception_print (gdb_stderr, exception); } TRY_CATCH (exception, RETURN_MASK_ALL) { pop_all_targets (); } signal (SIGHUP, SIG_DFL); /*FIXME: ??????????? */ raise (SIGHUP); }
static void check_status_exception_catchpoint (struct bpstats *bs) { struct exception_catchpoint *self = (struct exception_catchpoint *) bs->breakpoint_at; std::string type_name; bkpt_breakpoint_ops.check_status (bs); if (bs->stop == 0) return; if (self->pattern == NULL) return; TRY { struct value *typeinfo_arg; std::string canon; fetch_probe_arguments (NULL, &typeinfo_arg); type_name = cplus_typename_from_type_info (typeinfo_arg); canon = cp_canonicalize_string (type_name.c_str ()); if (!canon.empty ()) std::swap (type_name, canon); } CATCH (e, RETURN_MASK_ERROR) { exception_print (gdb_stderr, e); }
int catch_exceptions_with_msg (struct ui_out *func_uiout, catch_exceptions_ftype *func, void *func_args, char **gdberrmsg, return_mask mask) { struct gdb_exception exception = exception_none; volatile int val = 0; struct ui_out *saved_uiout; /* Save and override the global ``struct ui_out'' builder. */ saved_uiout = current_uiout; current_uiout = func_uiout; TRY { val = (*func) (current_uiout, func_args); } CATCH (ex, RETURN_MASK_ALL) { exception = ex; } END_CATCH /* Restore the global builder. */ current_uiout = saved_uiout; if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0) { /* The caller didn't request that the event be caught. Rethrow. */ throw_exception (exception); } exception_print (gdb_stderr, exception); gdb_assert (val >= 0); gdb_assert (exception.reason <= 0); if (exception.reason < 0) { /* If caller wants a copy of the low-level error message, make one. This is used in the case of a silent error whereby the caller may optionally want to issue the message. */ if (gdberrmsg != NULL) { if (exception.message != NULL) *gdberrmsg = xstrdup (exception.message); else *gdberrmsg = NULL; } return exception.reason; } return val; }
static struct gdb_exception safe_execute_command (struct ui_out *uiout, char *command, int from_tty) { struct gdb_exception e; struct captured_execute_command_args args; args.command = command; args.from_tty = from_tty; e = catch_exception (uiout, do_captured_execute_command, &args, RETURN_MASK_ALL); /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the caller should print the exception. */ exception_print (gdb_stderr, e); return e; }
void inferior_event_handler (enum inferior_event_type event_type, gdb_client_data client_data) { switch (event_type) { case INF_REG_EVENT: fetch_inferior_event (client_data); break; case INF_EXEC_COMPLETE: if (!non_stop) { /* Unregister the inferior from the event loop. This is done so that when the inferior is not running we don't get distracted by spurious inferior output. */ if (target_has_execution && target_can_async_p ()) target_async (0); } /* Do all continuations associated with the whole inferior (not a particular thread). */ if (!ptid_equal (inferior_ptid, null_ptid)) do_all_inferior_continuations (0); /* When running a command list (from a user command, say), these are only run when the command list is all done. */ if (interpreter_async) { check_frame_language_change (); /* Don't propagate breakpoint commands errors. Either we're stopping or some command resumes the inferior. The user will be informed. */ TRY { bpstat_do_actions (); } CATCH (e, RETURN_MASK_ALL) { exception_print (gdb_stderr, e); } END_CATCH } break; default: printf_unfiltered (_("Event type not recognized.\n")); break; }
exception_t* exception_catch(exception_t* exception) { // Check that the exception is non-NULL. if ( exception == NULL ) { fprintf(stderr, "exception_catch failed: exception_t is NULL.\n"); fflush(stderr); exit(-1); } // Print the exception message. exception_print(exception); // Free the exception and return NULL (see: exception_free()). return exception_free(exception); }
static int handle_command_errors (struct gdb_exception e) { if (e.reason < 0) { exception_print (gdb_stderr, e); /* If any exception escaped to here, we better enable stdin. Otherwise, any command that calls async_disable_stdin, and then throws, will leave stdin inoperable. */ async_enable_stdin (); return 0; } return 1; }
static void add_vsyscall_page (struct target_ops *target, int from_tty) { struct mem_range vsyscall_range; if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)) { struct bfd *bfd; if (core_bfd != NULL) bfd = core_bfd; else if (exec_bfd != NULL) bfd = exec_bfd; else /* FIXME: cagney/2004-05-06: Should not require an existing BFD when trying to create a run-time BFD of the VSYSCALL page in the inferior. Unfortunately that's the current interface so for the moment bail. Introducing a ``bfd_runtime'' (a BFD created using the loaded image) file format should fix this. */ { warning (_("Could not load vsyscall page " "because no executable was specified")); return; } char *name = xstrprintf ("system-supplied DSO at %s", paddress (target_gdbarch (), vsyscall_range.start)); TRY { /* Pass zero for FROM_TTY, because the action of loading the vsyscall DSO was not triggered by the user, even if the user typed "run" at the TTY. */ symbol_file_add_from_memory (bfd, vsyscall_range.start, vsyscall_range.length, name, 0 /* from_tty */); } CATCH (ex, RETURN_MASK_ALL) { exception_print (gdb_stderr, ex); } END_CATCH }
void start_event_loop (void) { /* Loop until there is nothing to do. This is the entry point to the event loop engine. gdb_do_one_event will process one event for each invocation. It blocks waiting for an event and then processes it. */ while (1) { int result = 0; TRY { result = gdb_do_one_event (); } CATCH (ex, RETURN_MASK_ALL) { exception_print (gdb_stderr, ex); /* If any exception escaped to here, we better enable stdin. Otherwise, any command that calls async_disable_stdin, and then throws, will leave stdin inoperable. */ async_enable_stdin (); /* If we long-jumped out of do_one_event, we probably didn't get around to resetting the prompt, which leaves readline in a messed-up state. Reset it here. */ current_ui->prompt_state = PROMPT_NEEDED; observer_notify_command_error (); /* This call looks bizarre, but it is required. If the user entered a command that caused an error, after_char_processing_hook won't be called from rl_callback_read_char_wrapper. Using a cleanup there won't work, since we want this function to be called after a new prompt is printed. */ if (after_char_processing_hook) (*after_char_processing_hook) (); /* Maybe better to set a flag to be checked somewhere as to whether display the prompt or not. */ } END_CATCH if (result < 0) break; }
/* General function to handle events in the inferior. So far it just takes care of detecting errors reported by select() or poll(), otherwise it assumes that all is OK, and goes on reading data from the fd. This however may not always be what we want to do. */ void inferior_event_handler (enum inferior_event_type event_type, gdb_client_data client_data) { struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup (); switch (event_type) { case INF_REG_EVENT: /* Use catch errors for now, until the inner layers of fetch_inferior_event (i.e. readchar) can return meaningful error status. If an error occurs while getting an event from the target, just cancel the current command. */ if (!catch_errors (fetch_inferior_event_wrapper, client_data, "", RETURN_MASK_ALL)) { bpstat_clear_actions (); do_all_intermediate_continuations (1); do_all_continuations (1); async_enable_stdin (); display_gdb_prompt (0); } break; case INF_EXEC_COMPLETE: if (!non_stop) { /* Unregister the inferior from the event loop. This is done so that when the inferior is not running we don't get distracted by spurious inferior output. */ if (target_has_execution) target_async (NULL, 0); } /* Do all continuations associated with the whole inferior (not a particular thread). */ if (!ptid_equal (inferior_ptid, null_ptid)) do_all_inferior_continuations (0); /* If we were doing a multi-step (eg: step n, next n), but it got interrupted by a breakpoint, still do the pending continuations. The continuation itself is responsible for distinguishing the cases. The continuations are allowed to touch the inferior memory, e.g. to remove breakpoints, so run them before running breakpoint commands, which may resume the target. */ if (non_stop && target_has_execution && !ptid_equal (inferior_ptid, null_ptid)) do_all_intermediate_continuations_thread (inferior_thread (), 0); else do_all_intermediate_continuations (0); /* Always finish the previous command before running any breakpoint commands. Any stop cancels the previous command. E.g. a "finish" or "step-n" command interrupted by an unrelated breakpoint is canceled. */ if (non_stop && target_has_execution && !ptid_equal (inferior_ptid, null_ptid)) do_all_continuations_thread (inferior_thread (), 0); else do_all_continuations (0); /* When running a command list (from a user command, say), these are only run when the command list is all done. */ if (interpreter_async) { volatile struct gdb_exception e; check_frame_language_change (); /* Don't propagate breakpoint commands errors. Either we're stopping or some command resumes the inferior. The user will be informed. */ TRY_CATCH (e, RETURN_MASK_ALL) { bpstat_do_actions (); } exception_print (gdb_stderr, e); } break; case INF_EXEC_CONTINUE: /* Is there anything left to do for the command issued to complete? */ if (non_stop) do_all_intermediate_continuations_thread (inferior_thread (), 0); else do_all_intermediate_continuations (0); break; case INF_TIMER: default: printf_unfiltered (_("Event type not recognized.\n")); break; } discard_cleanups (cleanup_if_error); }
static void error_print(FILE *out, int state, int cgi, int mode, VALUE code) { char buff[BUFSIZ]; #if RUBY_VERSION_CODE < 180 rb_defout = rb_stdout; #endif if (cgi) { char *imgdir; if ((imgdir = getenv("SCRIPT_NAME")) == NULL) imgdir = "UNKNOWN_IMG_DIR"; if (mode == MODE_NPHCGI) print_http_headers(); fprintf(out, "Content-Type: text/html\r\n"); fprintf(out, "Content-Style-Type: text/css\r\n"); fprintf(out, "\r\n"); fprintf(out, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n"); fprintf(out, "<html>\n"); fprintf(out, "<head>\n"); fprintf(out, "<title>eRuby</title>\n"); fprintf(out, "<style type=\"text/css\">\n"); fprintf(out, "<!--\n"); fprintf(out, "body { background-color: #ffffff }\n"); fprintf(out, "table { width: 100%%; padding: 5pt; border-style: none }\n"); fprintf(out, "th { color: #6666ff; background-color: #b0d0d0; text-align: left }\n"); fprintf(out, "td { color: #336666; background-color: #d0ffff }\n"); fprintf(out, "strong { color: #ff0000; font-weight: bold }\n"); fprintf(out, "div.backtrace { text-indent: 15%% }\n"); fprintf(out, "#version { color: #ff9900 }\n"); fprintf(out, "-->\n"); fprintf(out, "</style>\n"); fprintf(out, "</head>\n"); fprintf(out, "<body>\n"); fprintf(out, "<table summary=\"eRuby error information\">\n"); fprintf(out, "<caption>\n"); fprintf(out, "<img src=\"%s/logo.png\" alt=\"eRuby\">\n", imgdir); fprintf(out, "<span id=version>version: %s</span>\n", ERUBY_VERSION); fprintf(out, "</caption>\n"); fprintf(out, "<tr><th id=\"error\">\n"); fprintf(out, "ERROR\n"); fprintf(out, "</th></tr>\n"); fprintf(out, "<tr><td headers=\"error\">\n"); } switch (state) { case TAG_RETURN: error_pos(out, cgi); fprintf(out, ": unexpected return\n"); break; case TAG_NEXT: error_pos(out, cgi); fprintf(out, ": unexpected next\n"); break; case TAG_BREAK: error_pos(out, cgi); fprintf(out, ": unexpected break\n"); break; case TAG_REDO: error_pos(out, cgi); fprintf(out, ": unexpected redo\n"); break; case TAG_RETRY: error_pos(out, cgi); fprintf(out, ": retry outside of rescue clause\n"); break; case TAG_RAISE: case TAG_FATAL: exception_print(out, cgi); break; default: error_pos(out, cgi); snprintf(buff, BUFSIZ, ": unknown longjmp status %d", state); fputs(buff, out); break; } if (cgi) { fprintf(out, "</td></tr>\n"); } if (!NIL_P(code)) print_generated_code(out, code, cgi); if (cgi) { fprintf(out, "</table>\n"); fprintf(out, "</body>\n"); fprintf(out, "</html>\n"); } }
CATCH (exception, RETURN_MASK_ALL) { fputs_filtered ("Could not kill the program being debugged", gdb_stderr); exception_print (gdb_stderr, exception); }
static void core_open (const char *arg, int from_tty) { const char *p; int siggy; struct cleanup *old_chain; char *temp; int scratch_chan; int flags; char *filename; target_preopen (from_tty); if (!arg) { if (core_bfd) error (_("No core file specified. (Use `detach' " "to stop debugging a core file.)")); else error (_("No core file specified.")); } filename = tilde_expand (arg); if (!IS_ABSOLUTE_PATH (filename)) { temp = concat (current_directory, "/", filename, (char *) NULL); xfree (filename); filename = temp; } old_chain = make_cleanup (xfree, filename); flags = O_BINARY | O_LARGEFILE; if (write_files) flags |= O_RDWR; else flags |= O_RDONLY; scratch_chan = gdb_open_cloexec (filename, flags, 0); if (scratch_chan < 0) perror_with_name (filename); gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename, gnutarget, write_files ? FOPEN_RUB : FOPEN_RB, scratch_chan)); if (temp_bfd == NULL) perror_with_name (filename); if (!bfd_check_format (temp_bfd.get (), bfd_core) && !gdb_check_format (temp_bfd.get ())) { /* Do it after the err msg */ /* FIXME: should be checking for errors from bfd_close (for one thing, on error it does not free all the storage associated with the bfd). */ error (_("\"%s\" is not a core dump: %s"), filename, bfd_errmsg (bfd_get_error ())); } /* Looks semi-reasonable. Toss the old core file and work on the new. */ do_cleanups (old_chain); unpush_target (&core_ops); core_bfd = temp_bfd.release (); old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/); core_gdbarch = gdbarch_from_bfd (core_bfd); /* Find a suitable core file handler to munch on core_bfd */ core_vec = sniff_core_bfd (core_bfd); validate_files (); core_data = XCNEW (struct target_section_table); /* Find the data section */ if (build_section_table (core_bfd, &core_data->sections, &core_data->sections_end)) error (_("\"%s\": Can't find sections: %s"), bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); /* If we have no exec file, try to set the architecture from the core file. We don't do this unconditionally since an exec file typically contains more information that helps us determine the architecture than a core file. */ if (!exec_bfd) set_gdbarch_from_file (core_bfd); push_target (&core_ops); discard_cleanups (old_chain); /* Do this before acknowledging the inferior, so if post_create_inferior throws (can happen easilly if you're loading a core file with the wrong exec), we aren't left with threads from the previous inferior. */ init_thread_list (); inferior_ptid = null_ptid; /* Need to flush the register cache (and the frame cache) from a previous debug session. If inferior_ptid ends up the same as the last debug session --- e.g., b foo; run; gcore core1; step; gcore core2; core core1; core core2 --- then there's potential for get_current_regcache to return the cached regcache of the previous session, and the frame cache being stale. */ registers_changed (); /* Build up thread list from BFD sections, and possibly set the current thread to the .reg/NN section matching the .reg section. */ bfd_map_over_sections (core_bfd, add_to_thread_list, bfd_get_section_by_name (core_bfd, ".reg")); if (ptid_equal (inferior_ptid, null_ptid)) { /* Either we found no .reg/NN section, and hence we have a non-threaded core (single-threaded, from gdb's perspective), or for some reason add_to_thread_list couldn't determine which was the "main" thread. The latter case shouldn't usually happen, but we're dealing with input here, which can always be broken in different ways. */ struct thread_info *thread = first_thread_of_process (-1); if (thread == NULL) { inferior_appeared (current_inferior (), CORELOW_PID); inferior_ptid = pid_to_ptid (CORELOW_PID); add_thread_silent (inferior_ptid); } else switch_to_thread (thread->ptid); } post_create_inferior (&core_ops, from_tty); /* Now go through the target stack looking for threads since there may be a thread_stratum target loaded on top of target core by now. The layer above should claim threads found in the BFD sections. */ TRY { target_update_thread_list (); } CATCH (except, RETURN_MASK_ERROR) { exception_print (gdb_stderr, except); }
void dwarf2_tailcall_sniffer_first (struct frame_info *this_frame, void **tailcall_cachep, const LONGEST *entry_cfa_sp_offsetp) { CORE_ADDR prev_pc = 0, prev_sp = 0; /* GCC warning. */ int prev_sp_p = 0; CORE_ADDR this_pc; struct gdbarch *prev_gdbarch; struct call_site_chain *chain = NULL; struct tailcall_cache *cache; gdb_assert (*tailcall_cachep == NULL); /* PC may be after the function if THIS_FRAME calls noreturn function, get_frame_address_in_block will decrease it by 1 in such case. */ this_pc = get_frame_address_in_block (this_frame); /* Catch any unwinding errors. */ TRY { int sp_regnum; prev_gdbarch = frame_unwind_arch (this_frame); /* Simulate frame_unwind_pc without setting this_frame->prev_pc.p. */ prev_pc = gdbarch_unwind_pc (prev_gdbarch, this_frame); /* call_site_find_chain can throw an exception. */ chain = call_site_find_chain (prev_gdbarch, prev_pc, this_pc); if (entry_cfa_sp_offsetp == NULL) break; sp_regnum = gdbarch_sp_regnum (prev_gdbarch); if (sp_regnum == -1) break; prev_sp = frame_unwind_register_unsigned (this_frame, sp_regnum); prev_sp_p = 1; } CATCH (except, RETURN_MASK_ERROR) { if (entry_values_debug) exception_print (gdb_stdout, except); return; } END_CATCH /* Ambiguous unwind or unambiguous unwind verified as matching. */ if (chain == NULL || chain->length == 0) { xfree (chain); return; } cache = cache_new_ref1 (this_frame); *tailcall_cachep = cache; cache->chain = chain; cache->prev_pc = prev_pc; cache->chain_levels = pretended_chain_levels (chain); cache->prev_sp_p = prev_sp_p; if (cache->prev_sp_p) { cache->prev_sp = prev_sp; cache->entry_cfa_sp_offset = *entry_cfa_sp_offsetp; } gdb_assert (cache->chain_levels > 0); }
static void core_open (char *filename, int from_tty) { const char *p; int siggy; struct cleanup *old_chain; char *temp; bfd *temp_bfd; int scratch_chan; int flags; volatile struct gdb_exception except; target_preopen (from_tty); if (!filename) { if (core_bfd) error (_("No core file specified. (Use `detach' " "to stop debugging a core file.)")); else error (_("No core file specified.")); } filename = tilde_expand (filename); if (!IS_ABSOLUTE_PATH (filename)) { temp = concat (current_directory, "/", filename, (char *) NULL); xfree (filename); filename = temp; } old_chain = make_cleanup (xfree, filename); flags = O_BINARY | O_LARGEFILE; if (write_files) flags |= O_RDWR; else flags |= O_RDONLY; scratch_chan = open (filename, flags, 0); if (scratch_chan < 0) perror_with_name (filename); temp_bfd = bfd_fopen (filename, gnutarget, write_files ? FOPEN_RUB : FOPEN_RB, scratch_chan); if (temp_bfd == NULL) perror_with_name (filename); if (!bfd_check_format (temp_bfd, bfd_core) && !gdb_check_format (temp_bfd)) { /* Do it after the err msg */ /* FIXME: should be checking for errors from bfd_close (for one thing, on error it does not free all the storage associated with the bfd). */ make_cleanup_bfd_close (temp_bfd); error (_("\"%s\" is not a core dump: %s"), filename, bfd_errmsg (bfd_get_error ())); } /* Looks semi-reasonable. Toss the old core file and work on the new. */ discard_cleanups (old_chain); /* Don't free filename any more */ unpush_target (&core_ops); core_bfd = temp_bfd; old_chain = make_cleanup (core_close_cleanup, 0 /*ignore*/); /* FIXME: kettenis/20031023: This is very dangerous. The CORE_GDBARCH that results from this call may very well be different from CURRENT_GDBARCH. However, its methods may only work if it is selected as the current architecture, because they rely on swapped data (see gdbarch.c). We should get rid of that swapped data. */ core_gdbarch = gdbarch_from_bfd (core_bfd); /* Find a suitable core file handler to munch on core_bfd */ core_vec = sniff_core_bfd (core_bfd); validate_files (); core_data = XZALLOC (struct target_section_table); /* Find the data section */ if (build_section_table (core_bfd, &core_data->sections, &core_data->sections_end)) error (_("\"%s\": Can't find sections: %s"), bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ())); /* If we have no exec file, try to set the architecture from the core file. We don't do this unconditionally since an exec file typically contains more information that helps us determine the architecture than a core file. */ if (!exec_bfd) set_gdbarch_from_file (core_bfd); push_target (&core_ops); discard_cleanups (old_chain); /* Do this before acknowledging the inferior, so if post_create_inferior throws (can happen easilly if you're loading a core file with the wrong exec), we aren't left with threads from the previous inferior. */ init_thread_list (); inferior_ptid = null_ptid; /* Need to flush the register cache (and the frame cache) from a previous debug session. If inferior_ptid ends up the same as the last debug session --- e.g., b foo; run; gcore core1; step; gcore core2; core core1; core core2 --- then there's potential for get_current_regcache to return the cached regcache of the previous session, and the frame cache being stale. */ registers_changed (); /* Build up thread list from BFD sections, and possibly set the current thread to the .reg/NN section matching the .reg section. */ bfd_map_over_sections (core_bfd, add_to_thread_list, bfd_get_section_by_name (core_bfd, ".reg")); if (ptid_equal (inferior_ptid, null_ptid)) { /* Either we found no .reg/NN section, and hence we have a non-threaded core (single-threaded, from gdb's perspective), or for some reason add_to_thread_list couldn't determine which was the "main" thread. The latter case shouldn't usually happen, but we're dealing with input here, which can always be broken in different ways. */ struct thread_info *thread = first_thread_of_process (-1); if (thread == NULL) { inferior_appeared (current_inferior (), CORELOW_PID); inferior_ptid = pid_to_ptid (CORELOW_PID); add_thread_silent (inferior_ptid); } else switch_to_thread (thread->ptid); } post_create_inferior (&core_ops, from_tty); /* Now go through the target stack looking for threads since there may be a thread_stratum target loaded on top of target core by now. The layer above should claim threads found in the BFD sections. */ TRY_CATCH (except, RETURN_MASK_ERROR) { target_find_new_threads (); } if (except.reason < 0) exception_print (gdb_stderr, except); p = bfd_core_file_failing_command (core_bfd); if (p) printf_filtered (_("Core was generated by `%s'.\n"), p); siggy = bfd_core_file_failing_signal (core_bfd); if (siggy > 0) { /* If we don't have a CORE_GDBARCH to work with, assume a native core (map gdb_signal from host signals). If we do have CORE_GDBARCH to work with, but no gdb_signal_from_target implementation for that gdbarch, as a fallback measure, assume the host signal mapping. It'll be correct for native cores, but most likely incorrect for cross-cores. */ enum gdb_signal sig = (core_gdbarch != NULL && gdbarch_gdb_signal_from_target_p (core_gdbarch) ? gdbarch_gdb_signal_from_target (core_gdbarch, siggy) : gdb_signal_from_host (siggy)); printf_filtered (_("Program terminated with signal %d, %s.\n"), siggy, gdb_signal_to_string (sig)); } /* Fetch all registers from core file. */ target_fetch_registers (get_current_regcache (), -1); /* Now, set up the frame cache, and print the top of stack. */ reinit_frame_cache (); print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); }
void core_target_open (const char *arg, int from_tty) { const char *p; int siggy; int scratch_chan; int flags; target_preopen (from_tty); if (!arg) { if (core_bfd) error (_("No core file specified. (Use `detach' " "to stop debugging a core file.)")); else error (_("No core file specified.")); } gdb::unique_xmalloc_ptr<char> filename (tilde_expand (arg)); if (!IS_ABSOLUTE_PATH (filename.get ())) filename.reset (concat (current_directory, "/", filename.get (), (char *) NULL)); flags = O_BINARY | O_LARGEFILE; if (write_files) flags |= O_RDWR; else flags |= O_RDONLY; scratch_chan = gdb_open_cloexec (filename.get (), flags, 0); if (scratch_chan < 0) perror_with_name (filename.get ()); gdb_bfd_ref_ptr temp_bfd (gdb_bfd_fopen (filename.get (), gnutarget, write_files ? FOPEN_RUB : FOPEN_RB, scratch_chan)); if (temp_bfd == NULL) perror_with_name (filename.get ()); if (!bfd_check_format (temp_bfd.get (), bfd_core) && !gdb_check_format (temp_bfd.get ())) { /* Do it after the err msg */ /* FIXME: should be checking for errors from bfd_close (for one thing, on error it does not free all the storage associated with the bfd). */ error (_("\"%s\" is not a core dump: %s"), filename.get (), bfd_errmsg (bfd_get_error ())); } current_program_space->cbfd = std::move (temp_bfd); core_target *target = new core_target (); /* Own the target until it is successfully pushed. */ target_ops_up target_holder (target); validate_files (); /* If we have no exec file, try to set the architecture from the core file. We don't do this unconditionally since an exec file typically contains more information that helps us determine the architecture than a core file. */ if (!exec_bfd) set_gdbarch_from_file (core_bfd); push_target (std::move (target_holder)); inferior_ptid = null_ptid; /* Need to flush the register cache (and the frame cache) from a previous debug session. If inferior_ptid ends up the same as the last debug session --- e.g., b foo; run; gcore core1; step; gcore core2; core core1; core core2 --- then there's potential for get_current_regcache to return the cached regcache of the previous session, and the frame cache being stale. */ registers_changed (); /* Build up thread list from BFD sections, and possibly set the current thread to the .reg/NN section matching the .reg section. */ bfd_map_over_sections (core_bfd, add_to_thread_list, bfd_get_section_by_name (core_bfd, ".reg")); if (inferior_ptid == null_ptid) { /* Either we found no .reg/NN section, and hence we have a non-threaded core (single-threaded, from gdb's perspective), or for some reason add_to_thread_list couldn't determine which was the "main" thread. The latter case shouldn't usually happen, but we're dealing with input here, which can always be broken in different ways. */ thread_info *thread = first_thread_of_inferior (current_inferior ()); if (thread == NULL) { inferior_appeared (current_inferior (), CORELOW_PID); inferior_ptid = ptid_t (CORELOW_PID); add_thread_silent (inferior_ptid); } else switch_to_thread (thread); } post_create_inferior (target, from_tty); /* Now go through the target stack looking for threads since there may be a thread_stratum target loaded on top of target core by now. The layer above should claim threads found in the BFD sections. */ try { target_update_thread_list (); } catch (const gdb_exception_error &except) { exception_print (gdb_stderr, except); } p = bfd_core_file_failing_command (core_bfd); if (p) printf_filtered (_("Core was generated by `%s'.\n"), p); /* Clearing any previous state of convenience variables. */ clear_exit_convenience_vars (); siggy = bfd_core_file_failing_signal (core_bfd); if (siggy > 0) { gdbarch *core_gdbarch = target->core_gdbarch (); /* If we don't have a CORE_GDBARCH to work with, assume a native core (map gdb_signal from host signals). If we do have CORE_GDBARCH to work with, but no gdb_signal_from_target implementation for that gdbarch, as a fallback measure, assume the host signal mapping. It'll be correct for native cores, but most likely incorrect for cross-cores. */ enum gdb_signal sig = (core_gdbarch != NULL && gdbarch_gdb_signal_from_target_p (core_gdbarch) ? gdbarch_gdb_signal_from_target (core_gdbarch, siggy) : gdb_signal_from_host (siggy)); printf_filtered (_("Program terminated with signal %s, %s.\n"), gdb_signal_to_name (sig), gdb_signal_to_string (sig)); /* Set the value of the internal variable $_exitsignal, which holds the signal uncaught by the inferior. */ set_internalvar_integer (lookup_internalvar ("_exitsignal"), siggy); } /* Fetch all registers from core file. */ target_fetch_registers (get_current_regcache (), -1); /* Now, set up the frame cache, and print the top of stack. */ reinit_frame_cache (); print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1); /* Current thread should be NUM 1 but the user does not know that. If a program is single threaded gdb in general does not mention anything about threads. That is why the test is >= 2. */ if (thread_count () >= 2) { try { thread_command (NULL, from_tty); } catch (const gdb_exception_error &except) { exception_print (gdb_stderr, except); } } }