119370Spst/* Core dump and executable file functions above target vector, for GDB. 219370Spst 3130803Smarcel Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1996, 1997, 4130803Smarcel 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc. 5130803Smarcel 698944Sobrien This file is part of GDB. 719370Spst 898944Sobrien This program is free software; you can redistribute it and/or modify 998944Sobrien it under the terms of the GNU General Public License as published by 1098944Sobrien the Free Software Foundation; either version 2 of the License, or 1198944Sobrien (at your option) any later version. 1219370Spst 1398944Sobrien This program is distributed in the hope that it will be useful, 1498944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1598944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1698944Sobrien GNU General Public License for more details. 1719370Spst 1898944Sobrien You should have received a copy of the GNU General Public License 1998944Sobrien along with this program; if not, write to the Free Software 2098944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2198944Sobrien Boston, MA 02111-1307, USA. */ 2219370Spst 2319370Spst#include "defs.h" 2419370Spst#include "gdb_string.h" 2519370Spst#include <errno.h> 2619370Spst#include <signal.h> 2719370Spst#include <fcntl.h> 2819370Spst#include "inferior.h" 2919370Spst#include "symtab.h" 3019370Spst#include "command.h" 3119370Spst#include "gdbcmd.h" 3219370Spst#include "bfd.h" 3319370Spst#include "target.h" 3419370Spst#include "gdbcore.h" 3519370Spst#include "dis-asm.h" 3646283Sdfr#include "gdb_stat.h" 3798944Sobrien#include "completer.h" 3819370Spst 3946283Sdfr/* Local function declarations. */ 4019370Spst 4198944Sobrienextern void _initialize_core (void); 4298944Sobrienstatic void call_extra_exec_file_hooks (char *filename); 4319370Spst 4446283Sdfr/* You can have any number of hooks for `exec_file_command' command to call. 4546283Sdfr If there's only one hook, it is set in exec_file_display hook. 4646283Sdfr If there are two or more hooks, they are set in exec_file_extra_hooks[], 4746283Sdfr and exec_file_display_hook is set to a function that calls all of them. 4846283Sdfr This extra complexity is needed to preserve compatibility with 4946283Sdfr old code that assumed that only one hook could be set, and which called 5046283Sdfr exec_file_display_hook directly. */ 5146283Sdfr 5298944Sobrientypedef void (*hook_type) (char *); 5346283Sdfr 5498944Sobrienhook_type exec_file_display_hook; /* the original hook */ 5546283Sdfrstatic hook_type *exec_file_extra_hooks; /* array of additional hooks */ 5698944Sobrienstatic int exec_file_hook_count = 0; /* size of array */ 5746283Sdfr 5819370Spst/* Binary file diddling handle for the core file. */ 5919370Spst 6019370Spstbfd *core_bfd = NULL; 6198944Sobrien 6219370Spst 6319370Spst/* Backward compatability with old way of specifying core files. */ 6419370Spst 6519370Spstvoid 6698944Sobriencore_file_command (char *filename, int from_tty) 6719370Spst{ 6819370Spst struct target_ops *t; 6919370Spst 7098944Sobrien dont_repeat (); /* Either way, seems bogus. */ 7119370Spst 7219370Spst t = find_core_target (); 7398944Sobrien if (t == NULL) 7498944Sobrien error ("GDB can't read core files on this machine."); 7546283Sdfr 7698944Sobrien if (!filename) 7798944Sobrien (t->to_detach) (filename, from_tty); 7819370Spst else 7998944Sobrien (t->to_open) (filename, from_tty); 8019370Spst} 8198944Sobrien 8219370Spst 8346283Sdfr/* If there are two or more functions that wish to hook into exec_file_command, 8446283Sdfr * this function will call all of the hook functions. */ 8546283Sdfr 8646283Sdfrstatic void 8798944Sobriencall_extra_exec_file_hooks (char *filename) 8846283Sdfr{ 8946283Sdfr int i; 9046283Sdfr 9146283Sdfr for (i = 0; i < exec_file_hook_count; i++) 9298944Sobrien (*exec_file_extra_hooks[i]) (filename); 9346283Sdfr} 9446283Sdfr 9519370Spst/* Call this to specify the hook for exec_file_command to call back. 9619370Spst This is called from the x-window display code. */ 9719370Spst 9819370Spstvoid 9998944Sobrienspecify_exec_file_hook (void (*hook) (char *)) 10019370Spst{ 10146283Sdfr hook_type *new_array; 10246283Sdfr 10346283Sdfr if (exec_file_display_hook != NULL) 10446283Sdfr { 10546283Sdfr /* There's already a hook installed. Arrange to have both it 10646283Sdfr * and the subsequent hooks called. */ 10746283Sdfr if (exec_file_hook_count == 0) 10846283Sdfr { 10946283Sdfr /* If this is the first extra hook, initialize the hook array. */ 11098944Sobrien exec_file_extra_hooks = (hook_type *) xmalloc (sizeof (hook_type)); 11146283Sdfr exec_file_extra_hooks[0] = exec_file_display_hook; 11246283Sdfr exec_file_display_hook = call_extra_exec_file_hooks; 11346283Sdfr exec_file_hook_count = 1; 11446283Sdfr } 11546283Sdfr 11646283Sdfr /* Grow the hook array by one and add the new hook to the end. 11746283Sdfr Yes, it's inefficient to grow it by one each time but since 11846283Sdfr this is hardly ever called it's not a big deal. */ 11946283Sdfr exec_file_hook_count++; 12046283Sdfr new_array = 12146283Sdfr (hook_type *) xrealloc (exec_file_extra_hooks, 12298944Sobrien exec_file_hook_count * sizeof (hook_type)); 12346283Sdfr exec_file_extra_hooks = new_array; 12446283Sdfr exec_file_extra_hooks[exec_file_hook_count - 1] = hook; 12546283Sdfr } 12646283Sdfr else 12746283Sdfr exec_file_display_hook = hook; 12819370Spst} 12919370Spst 13019370Spst/* The exec file must be closed before running an inferior. 13119370Spst If it is needed again after the inferior dies, it must 13219370Spst be reopened. */ 13319370Spst 13419370Spstvoid 13598944Sobrienclose_exec_file (void) 13619370Spst{ 13798944Sobrien#if 0 /* FIXME */ 13819370Spst if (exec_bfd) 13919370Spst bfd_tempclose (exec_bfd); 14019370Spst#endif 14119370Spst} 14219370Spst 14319370Spstvoid 14498944Sobrienreopen_exec_file (void) 14519370Spst{ 14698944Sobrien#if 0 /* FIXME */ 14719370Spst if (exec_bfd) 14819370Spst bfd_reopen (exec_bfd); 14946283Sdfr#else 15046283Sdfr char *filename; 15146283Sdfr int res; 15246283Sdfr struct stat st; 15346283Sdfr long mtime; 15446283Sdfr 15546283Sdfr /* Don't do anything if the current target isn't exec. */ 15646283Sdfr if (exec_bfd == NULL || strcmp (target_shortname, "exec") != 0) 15746283Sdfr return; 15898944Sobrien 15946283Sdfr /* If the timestamp of the exec file has changed, reopen it. */ 16098944Sobrien filename = xstrdup (bfd_get_filename (exec_bfd)); 16198944Sobrien make_cleanup (xfree, filename); 16298944Sobrien mtime = bfd_get_mtime (exec_bfd); 16346283Sdfr res = stat (filename, &st); 16446283Sdfr 16546283Sdfr if (mtime && mtime != st.st_mtime) 16698944Sobrien { 16798944Sobrien exec_open (filename, 0); 16898944Sobrien } 16919370Spst#endif 17019370Spst} 17119370Spst 17219370Spst/* If we have both a core file and an exec file, 17319370Spst print a warning if they don't go together. */ 17419370Spst 17519370Spstvoid 17698944Sobrienvalidate_files (void) 17719370Spst{ 17819370Spst if (exec_bfd && core_bfd) 17919370Spst { 18019370Spst if (!core_file_matches_executable_p (core_bfd, exec_bfd)) 18119370Spst warning ("core file may not match specified executable file."); 18298944Sobrien else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd)) 18319370Spst warning ("exec file is newer than core file."); 18419370Spst } 18519370Spst} 18619370Spst 18719370Spst/* Return the name of the executable file as a string. 18819370Spst ERR nonzero means get error if there is none specified; 18919370Spst otherwise return 0 in that case. */ 19019370Spst 19119370Spstchar * 19298944Sobrienget_exec_file (int err) 19319370Spst{ 19498944Sobrien if (exec_bfd) 19598944Sobrien return bfd_get_filename (exec_bfd); 19698944Sobrien if (!err) 19798944Sobrien return NULL; 19819370Spst 19919370Spst error ("No executable file specified.\n\ 20019370SpstUse the \"file\" or \"exec-file\" command."); 20119370Spst return NULL; 20219370Spst} 20398944Sobrien 20419370Spst 20519370Spst/* Report a memory error with error(). */ 20619370Spst 20719370Spstvoid 20898944Sobrienmemory_error (int status, CORE_ADDR memaddr) 20919370Spst{ 21098944Sobrien struct ui_file *tmp_stream = mem_fileopen (); 21198944Sobrien make_cleanup_ui_file_delete (tmp_stream); 21298944Sobrien 21319370Spst if (status == EIO) 21419370Spst { 21519370Spst /* Actually, address between memaddr and memaddr + len 21698944Sobrien was out of bounds. */ 21798944Sobrien fprintf_unfiltered (tmp_stream, "Cannot access memory at address "); 21898944Sobrien print_address_numeric (memaddr, 1, tmp_stream); 21919370Spst } 22019370Spst else 22119370Spst { 22298944Sobrien fprintf_filtered (tmp_stream, "Error accessing memory address "); 22398944Sobrien print_address_numeric (memaddr, 1, tmp_stream); 22498944Sobrien fprintf_filtered (tmp_stream, ": %s.", 22598944Sobrien safe_strerror (status)); 22619370Spst } 22798944Sobrien 22898944Sobrien error_stream (tmp_stream); 22919370Spst} 23019370Spst 23119370Spst/* Same as target_read_memory, but report an error if can't read. */ 23219370Spstvoid 23398944Sobrienread_memory (CORE_ADDR memaddr, char *myaddr, int len) 23419370Spst{ 23519370Spst int status; 23619370Spst status = target_read_memory (memaddr, myaddr, len); 23719370Spst if (status != 0) 23819370Spst memory_error (status, memaddr); 23919370Spst} 24019370Spst 241130803Smarcel/* Argument / return result struct for use with 242130803Smarcel do_captured_read_memory_integer(). MEMADDR and LEN are filled in 243130803Smarcel by gdb_read_memory_integer(). RESULT is the contents that were 244130803Smarcel successfully read from MEMADDR of length LEN. */ 24519370Spst 24698944Sobrienstruct captured_read_memory_integer_arguments 24719370Spst{ 24898944Sobrien CORE_ADDR memaddr; 24998944Sobrien int len; 25098944Sobrien LONGEST result; 25198944Sobrien}; 25298944Sobrien 253130803Smarcel/* Helper function for gdb_read_memory_integer(). DATA must be a 254130803Smarcel pointer to a captured_read_memory_integer_arguments struct. 255130803Smarcel Return 1 if successful. Note that the catch_errors() interface 256130803Smarcel will return 0 if an error occurred while reading memory. This 257130803Smarcel choice of return code is so that we can distinguish between 258130803Smarcel success and failure. */ 259130803Smarcel 26098944Sobrienstatic int 26198944Sobriendo_captured_read_memory_integer (void *data) 26298944Sobrien{ 26398944Sobrien struct captured_read_memory_integer_arguments *args = (struct captured_read_memory_integer_arguments*) data; 26498944Sobrien CORE_ADDR memaddr = args->memaddr; 26598944Sobrien int len = args->len; 26698944Sobrien 26798944Sobrien args->result = read_memory_integer (memaddr, len); 26898944Sobrien 269130803Smarcel return 1; 27098944Sobrien} 27198944Sobrien 272130803Smarcel/* Read memory at MEMADDR of length LEN and put the contents in 273130803Smarcel RETURN_VALUE. Return 0 if MEMADDR couldn't be read and non-zero 274130803Smarcel if successful. */ 275130803Smarcel 27698944Sobrienint 27798944Sobriensafe_read_memory_integer (CORE_ADDR memaddr, int len, LONGEST *return_value) 27898944Sobrien{ 27919370Spst int status; 28098944Sobrien struct captured_read_memory_integer_arguments args; 28198944Sobrien args.memaddr = memaddr; 28298944Sobrien args.len = len; 28319370Spst 28498944Sobrien status = catch_errors (do_captured_read_memory_integer, &args, 28598944Sobrien "", RETURN_MASK_ALL); 286130803Smarcel if (status) 28798944Sobrien *return_value = args.result; 28898944Sobrien 28998944Sobrien return status; 29019370Spst} 29119370Spst 29219370SpstLONGEST 29398944Sobrienread_memory_integer (CORE_ADDR memaddr, int len) 29419370Spst{ 29519370Spst char buf[sizeof (LONGEST)]; 29619370Spst 29719370Spst read_memory (memaddr, buf, len); 29819370Spst return extract_signed_integer (buf, len); 29919370Spst} 30019370Spst 30146283SdfrULONGEST 30298944Sobrienread_memory_unsigned_integer (CORE_ADDR memaddr, int len) 30319370Spst{ 30446283Sdfr char buf[sizeof (ULONGEST)]; 30519370Spst 30619370Spst read_memory (memaddr, buf, len); 30719370Spst return extract_unsigned_integer (buf, len); 30819370Spst} 30946283Sdfr 31046283Sdfrvoid 31198944Sobrienread_memory_string (CORE_ADDR memaddr, char *buffer, int max_len) 31246283Sdfr{ 313130803Smarcel char *cp; 314130803Smarcel int i; 31546283Sdfr int cnt; 31646283Sdfr 31746283Sdfr cp = buffer; 31846283Sdfr while (1) 31946283Sdfr { 32046283Sdfr if (cp - buffer >= max_len) 32198944Sobrien { 32298944Sobrien buffer[max_len - 1] = '\0'; 32398944Sobrien break; 32498944Sobrien } 32546283Sdfr cnt = max_len - (cp - buffer); 32646283Sdfr if (cnt > 8) 32746283Sdfr cnt = 8; 32846283Sdfr read_memory (memaddr + (int) (cp - buffer), cp, cnt); 32946283Sdfr for (i = 0; i < cnt && *cp; i++, cp++) 33098944Sobrien ; /* null body */ 33146283Sdfr 33246283Sdfr if (i < cnt && !*cp) 33398944Sobrien break; 33446283Sdfr } 33546283Sdfr} 33646283Sdfr 337130803SmarcelCORE_ADDR 338130803Smarcelread_memory_typed_address (CORE_ADDR addr, struct type *type) 339130803Smarcel{ 340130803Smarcel char *buf = alloca (TYPE_LENGTH (type)); 341130803Smarcel read_memory (addr, buf, TYPE_LENGTH (type)); 342130803Smarcel return extract_typed_address (buf, type); 343130803Smarcel} 344130803Smarcel 34598944Sobrien/* Same as target_write_memory, but report an error if can't write. */ 34698944Sobrienvoid 34798944Sobrienwrite_memory (CORE_ADDR memaddr, char *myaddr, int len) 34898944Sobrien{ 34998944Sobrien int status; 35098944Sobrien 35198944Sobrien status = target_write_memory (memaddr, myaddr, len); 35298944Sobrien if (status != 0) 35398944Sobrien memory_error (status, memaddr); 35498944Sobrien} 35598944Sobrien 35698944Sobrien/* Store VALUE at ADDR in the inferior as a LEN-byte unsigned integer. */ 35798944Sobrienvoid 35898944Sobrienwrite_memory_unsigned_integer (CORE_ADDR addr, int len, ULONGEST value) 35998944Sobrien{ 36098944Sobrien char *buf = alloca (len); 36198944Sobrien store_unsigned_integer (buf, len, value); 36298944Sobrien write_memory (addr, buf, len); 36398944Sobrien} 36498944Sobrien 36598944Sobrien/* Store VALUE at ADDR in the inferior as a LEN-byte signed integer. */ 36698944Sobrienvoid 36798944Sobrienwrite_memory_signed_integer (CORE_ADDR addr, int len, LONGEST value) 36898944Sobrien{ 36998944Sobrien char *buf = alloca (len); 37098944Sobrien store_signed_integer (buf, len, value); 37198944Sobrien write_memory (addr, buf, len); 37298944Sobrien} 37398944Sobrien 37419370Spst 37598944Sobrien 37619370Spst#if 0 37719370Spst/* Enable after 4.12. It is not tested. */ 37819370Spst 37919370Spst/* Search code. Targets can just make this their search function, or 38019370Spst if the protocol has a less general search function, they can call this 38119370Spst in the cases it can't handle. */ 38219370Spstvoid 38398944Sobriengeneric_search (int len, char *data, char *mask, CORE_ADDR startaddr, 38498944Sobrien int increment, CORE_ADDR lorange, CORE_ADDR hirange, 38598944Sobrien CORE_ADDR *addr_found, char *data_found) 38619370Spst{ 38719370Spst int i; 38819370Spst CORE_ADDR curaddr = startaddr; 38919370Spst 39019370Spst while (curaddr >= lorange && curaddr < hirange) 39119370Spst { 39219370Spst read_memory (curaddr, data_found, len); 39319370Spst for (i = 0; i < len; ++i) 39419370Spst if ((data_found[i] & mask[i]) != data[i]) 39519370Spst goto try_again; 39619370Spst /* It matches. */ 39719370Spst *addr_found = curaddr; 39819370Spst return; 39919370Spst 40019370Spst try_again: 40119370Spst curaddr += increment; 40219370Spst } 40398944Sobrien *addr_found = (CORE_ADDR) 0; 40419370Spst return; 40519370Spst} 40619370Spst#endif /* 0 */ 40719370Spst 40819370Spst/* The current default bfd target. Points to storage allocated for 40919370Spst gnutarget_string. */ 41019370Spstchar *gnutarget; 41119370Spst 41219370Spst/* Same thing, except it is "auto" not NULL for the default case. */ 41319370Spststatic char *gnutarget_string; 41419370Spst 41598944Sobrienstatic void set_gnutarget_command (char *, int, struct cmd_list_element *); 41619370Spst 41719370Spststatic void 41898944Sobrienset_gnutarget_command (char *ignore, int from_tty, struct cmd_list_element *c) 41919370Spst{ 420130803Smarcel if (strcmp (gnutarget_string, "auto") == 0) 42119370Spst gnutarget = NULL; 42219370Spst else 42319370Spst gnutarget = gnutarget_string; 42419370Spst} 42519370Spst 42619370Spst/* Set the gnutarget. */ 42719370Spstvoid 42898944Sobrienset_gnutarget (char *newtarget) 42919370Spst{ 43019370Spst if (gnutarget_string != NULL) 43198944Sobrien xfree (gnutarget_string); 43219370Spst gnutarget_string = savestring (newtarget, strlen (newtarget)); 43319370Spst set_gnutarget_command (NULL, 0, NULL); 43419370Spst} 43519370Spst 43619370Spstvoid 43798944Sobrien_initialize_core (void) 43819370Spst{ 43919370Spst struct cmd_list_element *c; 44019370Spst c = add_cmd ("core-file", class_files, core_file_command, 44119370Spst "Use FILE as core dump for examining memory and registers.\n\ 44219370SpstNo arg means have no core file. This command has been superseded by the\n\ 44319370Spst`target core' and `detach' commands.", &cmdlist); 444130803Smarcel set_cmd_completer (c, filename_completer); 44519370Spst 44619370Spst c = add_set_cmd ("gnutarget", class_files, var_string_noescape, 44798944Sobrien (char *) &gnutarget_string, 44898944Sobrien "Set the current BFD target.\n\ 44919370SpstUse `set gnutarget auto' to specify automatic detection.", 45098944Sobrien &setlist); 45198944Sobrien set_cmd_sfunc (c, set_gnutarget_command); 45219370Spst add_show_from_set (c, &showlist); 45319370Spst 45419370Spst if (getenv ("GNUTARGET")) 45519370Spst set_gnutarget (getenv ("GNUTARGET")); 45619370Spst else 45719370Spst set_gnutarget ("auto"); 45819370Spst} 459