1 /* Core dump and executable file functions above target vector, for GDB.
2 
3    Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include <signal.h>
21 #include <fcntl.h>
22 #include "event-top.h"
23 #include "extract-store-integer.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "command.h"
27 #include "cli/cli-cmds.h"
28 #include "bfd.h"
29 #include "target.h"
30 #include "gdbcore.h"
31 #include "dis-asm.h"
32 #include <sys/stat.h>
33 #include "completer.h"
34 #include "observable.h"
35 #include "cli/cli-utils.h"
36 #include "gdbarch.h"
37 #include "interps.h"
38 
39 void
reopen_exec_file(void)40 reopen_exec_file (void)
41 {
42   bfd *exec_bfd = current_program_space->exec_bfd ();
43 
44   /* Don't do anything if there isn't an exec file.  */
45   if (exec_bfd == nullptr)
46     return;
47 
48   /* The main executable can't be an in-memory BFD object.  If it was then
49      the use of bfd_stat below would not work as expected.  */
50   gdb_assert ((exec_bfd->flags & BFD_IN_MEMORY) == 0);
51 
52   /* If the timestamp of the exec file has changed, reopen it.  */
53   struct stat st;
54   int res = bfd_stat (exec_bfd, &st);
55 
56   if (res == 0
57       && current_program_space->ebfd_mtime != 0
58       && current_program_space->ebfd_mtime != st.st_mtime)
59     exec_file_attach (bfd_get_filename (exec_bfd), 0);
60 }
61 
62 /* If we have both a core file and an exec file,
63    print a warning if they don't go together.  */
64 
65 void
validate_files(void)66 validate_files (void)
67 {
68   if (current_program_space->exec_bfd () && current_program_space->core_bfd ())
69     {
70       if (!core_file_matches_executable_p (current_program_space->core_bfd (),
71                                                      current_program_space->exec_bfd ()))
72           warning (_("core file may not match specified executable file."));
73       else if (bfd_get_mtime (current_program_space->exec_bfd ())
74                  > bfd_get_mtime (current_program_space->core_bfd ()))
75           warning (_("exec file is newer than core file."));
76     }
77 }
78 
79 /* See gdbsupport/common-inferior.h.  */
80 
81 const char *
get_exec_file(int err)82 get_exec_file (int err)
83 {
84   if (current_program_space->exec_filename != nullptr)
85     return current_program_space->exec_filename.get ();
86   if (!err)
87     return NULL;
88 
89   error (_("No executable file specified.\n\
90 Use the \"file\" or \"exec-file\" command."));
91 }
92 
93 
94 std::string
memory_error_message(enum target_xfer_status err,struct gdbarch * gdbarch,CORE_ADDR memaddr)95 memory_error_message (enum target_xfer_status err,
96                           struct gdbarch *gdbarch, CORE_ADDR memaddr)
97 {
98   switch (err)
99     {
100     case TARGET_XFER_E_IO:
101       /* Actually, address between memaddr and memaddr + len was out of
102            bounds.  */
103       return string_printf (_("Cannot access memory at address %s"),
104                                   paddress (gdbarch, memaddr));
105     case TARGET_XFER_UNAVAILABLE:
106       return string_printf (_("Memory at address %s unavailable."),
107                                   paddress (gdbarch, memaddr));
108     default:
109       internal_error ("unhandled target_xfer_status: %s (%s)",
110                           target_xfer_status_to_string (err),
111                           plongest (err));
112     }
113 }
114 
115 /* Report a memory error by throwing a suitable exception.  */
116 
117 void
memory_error(enum target_xfer_status err,CORE_ADDR memaddr)118 memory_error (enum target_xfer_status err, CORE_ADDR memaddr)
119 {
120   enum errors exception = GDB_NO_ERROR;
121 
122   /* Build error string.  */
123   std::string str
124     = memory_error_message (err, current_inferior ()->arch (), memaddr);
125 
126   /* Choose the right error to throw.  */
127   switch (err)
128     {
129     case TARGET_XFER_E_IO:
130       exception = MEMORY_ERROR;
131       break;
132     case TARGET_XFER_UNAVAILABLE:
133       exception = NOT_AVAILABLE_ERROR;
134       break;
135     }
136 
137   /* Throw it.  */
138   throw_error (exception, ("%s"), str.c_str ());
139 }
140 
141 /* Helper function.  */
142 
143 static void
read_memory_object(enum target_object object,CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)144 read_memory_object (enum target_object object, CORE_ADDR memaddr,
145                         gdb_byte *myaddr, ssize_t len)
146 {
147   ULONGEST xfered = 0;
148 
149   while (xfered < len)
150     {
151       enum target_xfer_status status;
152       ULONGEST xfered_len;
153 
154       status = target_xfer_partial (current_inferior ()->top_target (), object,
155                                             NULL, myaddr + xfered, NULL,
156                                             memaddr + xfered, len - xfered,
157                                             &xfered_len);
158 
159       if (status != TARGET_XFER_OK)
160           memory_error (status == TARGET_XFER_EOF ? TARGET_XFER_E_IO : status,
161                           memaddr + xfered);
162 
163       xfered += xfered_len;
164       QUIT;
165     }
166 }
167 
168 /* Same as target_read_memory, but report an error if can't read.  */
169 
170 void
read_memory(CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)171 read_memory (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
172 {
173   read_memory_object (TARGET_OBJECT_MEMORY, memaddr, myaddr, len);
174 }
175 
176 /* Same as target_read_stack, but report an error if can't read.  */
177 
178 void
read_stack(CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)179 read_stack (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
180 {
181   read_memory_object (TARGET_OBJECT_STACK_MEMORY, memaddr, myaddr, len);
182 }
183 
184 /* Same as target_read_code, but report an error if can't read.  */
185 
186 void
read_code(CORE_ADDR memaddr,gdb_byte * myaddr,ssize_t len)187 read_code (CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
188 {
189   read_memory_object (TARGET_OBJECT_CODE_MEMORY, memaddr, myaddr, len);
190 }
191 
192 /* Read memory at MEMADDR of length LEN and put the contents in
193    RETURN_VALUE.  Return 0 if MEMADDR couldn't be read and non-zero
194    if successful.  */
195 
196 int
safe_read_memory_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order,LONGEST * return_value)197 safe_read_memory_integer (CORE_ADDR memaddr, int len,
198                                 enum bfd_endian byte_order,
199                                 LONGEST *return_value)
200 {
201   gdb_byte buf[sizeof (LONGEST)];
202 
203   if (target_read_memory (memaddr, buf, len))
204     return 0;
205 
206   *return_value = extract_signed_integer (buf, len, byte_order);
207   return 1;
208 }
209 
210 /* Read memory at MEMADDR of length LEN and put the contents in
211    RETURN_VALUE.  Return 0 if MEMADDR couldn't be read and non-zero
212    if successful.  */
213 
214 int
safe_read_memory_unsigned_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order,ULONGEST * return_value)215 safe_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
216                                            enum bfd_endian byte_order,
217                                            ULONGEST *return_value)
218 {
219   gdb_byte buf[sizeof (ULONGEST)];
220 
221   if (target_read_memory (memaddr, buf, len))
222     return 0;
223 
224   *return_value = extract_unsigned_integer (buf, len, byte_order);
225   return 1;
226 }
227 
228 LONGEST
read_memory_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order)229 read_memory_integer (CORE_ADDR memaddr, int len,
230                          enum bfd_endian byte_order)
231 {
232   gdb_byte buf[sizeof (LONGEST)];
233 
234   read_memory (memaddr, buf, len);
235   return extract_signed_integer (buf, len, byte_order);
236 }
237 
238 ULONGEST
read_memory_unsigned_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order)239 read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
240                                     enum bfd_endian byte_order)
241 {
242   gdb_byte buf[sizeof (ULONGEST)];
243 
244   read_memory (memaddr, buf, len);
245   return extract_unsigned_integer (buf, len, byte_order);
246 }
247 
248 LONGEST
read_code_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order)249 read_code_integer (CORE_ADDR memaddr, int len,
250                        enum bfd_endian byte_order)
251 {
252   gdb_byte buf[sizeof (LONGEST)];
253 
254   read_code (memaddr, buf, len);
255   return extract_signed_integer (buf, len, byte_order);
256 }
257 
258 ULONGEST
read_code_unsigned_integer(CORE_ADDR memaddr,int len,enum bfd_endian byte_order)259 read_code_unsigned_integer (CORE_ADDR memaddr, int len,
260                                   enum bfd_endian byte_order)
261 {
262   gdb_byte buf[sizeof (ULONGEST)];
263 
264   read_code (memaddr, buf, len);
265   return extract_unsigned_integer (buf, len, byte_order);
266 }
267 
268 CORE_ADDR
read_memory_typed_address(CORE_ADDR addr,struct type * type)269 read_memory_typed_address (CORE_ADDR addr, struct type *type)
270 {
271   gdb_byte *buf = (gdb_byte *) alloca (type->length ());
272 
273   read_memory (addr, buf, type->length ());
274   return extract_typed_address (buf, type);
275 }
276 
277 /* See gdbcore.h.  */
278 
279 void
write_memory(CORE_ADDR memaddr,const bfd_byte * myaddr,ssize_t len)280 write_memory (CORE_ADDR memaddr,
281                 const bfd_byte *myaddr, ssize_t len)
282 {
283   int status;
284 
285   status = target_write_memory (memaddr, myaddr, len);
286   if (status != 0)
287     memory_error (TARGET_XFER_E_IO, memaddr);
288 }
289 
290 /* Notify interpreters and observers that INF's memory was changed.  */
291 
292 static void
notify_memory_changed(inferior * inf,CORE_ADDR addr,ssize_t len,const bfd_byte * data)293 notify_memory_changed (inferior *inf, CORE_ADDR addr, ssize_t len,
294                            const bfd_byte *data)
295 {
296   interps_notify_memory_changed (inf, addr, len, data);
297   gdb::observers::memory_changed.notify (inf, addr, len, data);
298 }
299 
300 /* Same as write_memory, but notify 'memory_changed' observers.  */
301 
302 void
write_memory_with_notification(CORE_ADDR memaddr,const bfd_byte * myaddr,ssize_t len)303 write_memory_with_notification (CORE_ADDR memaddr, const bfd_byte *myaddr,
304                                         ssize_t len)
305 {
306   write_memory (memaddr, myaddr, len);
307   notify_memory_changed (current_inferior (), memaddr, len, myaddr);
308 }
309 
310 /* Store VALUE at ADDR in the inferior as a LEN-byte unsigned
311    integer.  */
312 void
write_memory_unsigned_integer(CORE_ADDR addr,int len,enum bfd_endian byte_order,ULONGEST value)313 write_memory_unsigned_integer (CORE_ADDR addr, int len,
314                                      enum bfd_endian byte_order,
315                                      ULONGEST value)
316 {
317   gdb_byte *buf = (gdb_byte *) alloca (len);
318 
319   store_unsigned_integer (buf, len, byte_order, value);
320   write_memory (addr, buf, len);
321 }
322 
323 /* Store VALUE at ADDR in the inferior as a LEN-byte signed
324    integer.  */
325 void
write_memory_signed_integer(CORE_ADDR addr,int len,enum bfd_endian byte_order,LONGEST value)326 write_memory_signed_integer (CORE_ADDR addr, int len,
327                                    enum bfd_endian byte_order,
328                                    LONGEST value)
329 {
330   gdb_byte *buf = (gdb_byte *) alloca (len);
331 
332   store_signed_integer (buf, len, byte_order, value);
333   write_memory (addr, buf, len);
334 }
335 
336 /* The current default bfd target.  Points to storage allocated for
337    gnutarget_string.  */
338 const char *gnutarget;
339 
340 /* Same thing, except it is "auto" not NULL for the default case.  */
341 static std::string gnutarget_string;
342 static void
show_gnutarget_string(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)343 show_gnutarget_string (struct ui_file *file, int from_tty,
344                            struct cmd_list_element *c,
345                            const char *value)
346 {
347   gdb_printf (file,
348                 _("The current BFD target is \"%s\".\n"), value);
349 }
350 
351 static void
set_gnutarget_command(const char * ignore,int from_tty,struct cmd_list_element * c)352 set_gnutarget_command (const char *ignore, int from_tty,
353                            struct cmd_list_element *c)
354 {
355   const char *gend = gnutarget_string.c_str () + gnutarget_string.size ();
356   gend = remove_trailing_whitespace (gnutarget_string.c_str (), gend);
357   gnutarget_string
358     = gnutarget_string.substr (0, gend - gnutarget_string.data ());
359 
360   if (gnutarget_string == "auto")
361     gnutarget = NULL;
362   else
363     gnutarget = gnutarget_string.c_str ();
364 }
365 
366 /* A completion function for "set gnutarget".  */
367 
368 static void
complete_set_gnutarget(struct cmd_list_element * cmd,completion_tracker & tracker,const char * text,const char * word)369 complete_set_gnutarget (struct cmd_list_element *cmd,
370                               completion_tracker &tracker,
371                               const char *text, const char *word)
372 {
373   static const char **bfd_targets;
374 
375   if (bfd_targets == NULL)
376     {
377       int last;
378 
379       bfd_targets = bfd_target_list ();
380       for (last = 0; bfd_targets[last] != NULL; ++last)
381           ;
382 
383       bfd_targets = XRESIZEVEC (const char *, bfd_targets, last + 2);
384       bfd_targets[last] = "auto";
385       bfd_targets[last + 1] = NULL;
386     }
387 
388   complete_on_enum (tracker, bfd_targets, text, word);
389 }
390 
391 /* Set the gnutarget.  */
392 void
set_gnutarget(const char * newtarget)393 set_gnutarget (const char *newtarget)
394 {
395   gnutarget_string = newtarget;
396   set_gnutarget_command (NULL, 0, NULL);
397 }
398 
399 void _initialize_core ();
400 void
_initialize_core()401 _initialize_core ()
402 {
403   cmd_list_element *core_file_cmd
404     = add_cmd ("core-file", class_files, core_file_command, _("\
405 Use FILE as core dump for examining memory and registers.\n\
406 Usage: core-file FILE\n\
407 No arg means have no core file.  This command has been superseded by the\n\
408 `target core' and `detach' commands."), &cmdlist);
409   set_cmd_completer (core_file_cmd, filename_completer);
410 
411 
412   set_show_commands set_show_gnutarget
413     = add_setshow_string_noescape_cmd ("gnutarget", class_files,
414                                                &gnutarget_string, _("\
415 Set the current BFD target."), _("\
416 Show the current BFD target."), _("\
417 Use `set gnutarget auto' to specify automatic detection."),
418                                                set_gnutarget_command,
419                                                show_gnutarget_string,
420                                                &setlist, &showlist);
421   set_cmd_completer (set_show_gnutarget.set, complete_set_gnutarget);
422 
423   add_alias_cmd ("g", set_show_gnutarget.set, class_files, 1, &setlist);
424 
425   if (getenv ("GNUTARGET"))
426     set_gnutarget (getenv ("GNUTARGET"));
427   else
428     set_gnutarget ("auto");
429 }
430