| 1 |
24 |
jeremybenn |
/* Dump-to-file commands, for GDB, the GNU debugger.
|
| 2 |
|
|
|
| 3 |
|
|
Copyright (c) 2002, 2005, 2007, 2008 Free Software Foundation, Inc.
|
| 4 |
|
|
|
| 5 |
|
|
Contributed by Red Hat.
|
| 6 |
|
|
|
| 7 |
|
|
This file is part of GDB.
|
| 8 |
|
|
|
| 9 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 10 |
|
|
it under the terms of the GNU General Public License as published by
|
| 11 |
|
|
the Free Software Foundation; either version 3 of the License, or
|
| 12 |
|
|
(at your option) any later version.
|
| 13 |
|
|
|
| 14 |
|
|
This program is distributed in the hope that it will be useful,
|
| 15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 |
|
|
GNU General Public License for more details.
|
| 18 |
|
|
|
| 19 |
|
|
You should have received a copy of the GNU General Public License
|
| 20 |
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
| 21 |
|
|
|
| 22 |
|
|
#include "defs.h"
|
| 23 |
|
|
#include "gdb_string.h"
|
| 24 |
|
|
#include "cli/cli-decode.h"
|
| 25 |
|
|
#include "cli/cli-cmds.h"
|
| 26 |
|
|
#include "value.h"
|
| 27 |
|
|
#include "completer.h"
|
| 28 |
|
|
#include "cli/cli-dump.h"
|
| 29 |
|
|
#include "gdb_assert.h"
|
| 30 |
|
|
#include <ctype.h>
|
| 31 |
|
|
#include "target.h"
|
| 32 |
|
|
#include "readline/readline.h"
|
| 33 |
|
|
|
| 34 |
|
|
#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
|
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
|
|
char *
|
| 38 |
|
|
skip_spaces (char *chp)
|
| 39 |
|
|
{
|
| 40 |
|
|
if (chp == NULL)
|
| 41 |
|
|
return NULL;
|
| 42 |
|
|
while (isspace (*chp))
|
| 43 |
|
|
chp++;
|
| 44 |
|
|
return chp;
|
| 45 |
|
|
}
|
| 46 |
|
|
|
| 47 |
|
|
char *
|
| 48 |
|
|
scan_expression_with_cleanup (char **cmd, const char *def)
|
| 49 |
|
|
{
|
| 50 |
|
|
if ((*cmd) == NULL || (**cmd) == '\0')
|
| 51 |
|
|
{
|
| 52 |
|
|
char *exp = xstrdup (def);
|
| 53 |
|
|
make_cleanup (xfree, exp);
|
| 54 |
|
|
return exp;
|
| 55 |
|
|
}
|
| 56 |
|
|
else
|
| 57 |
|
|
{
|
| 58 |
|
|
char *exp;
|
| 59 |
|
|
char *end;
|
| 60 |
|
|
|
| 61 |
|
|
end = (*cmd) + strcspn (*cmd, " \t");
|
| 62 |
|
|
exp = savestring ((*cmd), end - (*cmd));
|
| 63 |
|
|
make_cleanup (xfree, exp);
|
| 64 |
|
|
(*cmd) = skip_spaces (end);
|
| 65 |
|
|
return exp;
|
| 66 |
|
|
}
|
| 67 |
|
|
}
|
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
static void
|
| 71 |
|
|
do_fclose_cleanup (void *arg)
|
| 72 |
|
|
{
|
| 73 |
|
|
FILE *file = arg;
|
| 74 |
|
|
fclose (arg);
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
static struct cleanup *
|
| 78 |
|
|
make_cleanup_fclose (FILE *file)
|
| 79 |
|
|
{
|
| 80 |
|
|
return make_cleanup (do_fclose_cleanup, file);
|
| 81 |
|
|
}
|
| 82 |
|
|
|
| 83 |
|
|
char *
|
| 84 |
|
|
scan_filename_with_cleanup (char **cmd, const char *defname)
|
| 85 |
|
|
{
|
| 86 |
|
|
char *filename;
|
| 87 |
|
|
char *fullname;
|
| 88 |
|
|
|
| 89 |
|
|
/* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */
|
| 90 |
|
|
|
| 91 |
|
|
/* File. */
|
| 92 |
|
|
if ((*cmd) == NULL)
|
| 93 |
|
|
{
|
| 94 |
|
|
if (defname == NULL)
|
| 95 |
|
|
error (_("Missing filename."));
|
| 96 |
|
|
filename = xstrdup (defname);
|
| 97 |
|
|
make_cleanup (xfree, filename);
|
| 98 |
|
|
}
|
| 99 |
|
|
else
|
| 100 |
|
|
{
|
| 101 |
|
|
/* FIXME: should parse a possibly quoted string. */
|
| 102 |
|
|
char *end;
|
| 103 |
|
|
|
| 104 |
|
|
(*cmd) = skip_spaces (*cmd);
|
| 105 |
|
|
end = *cmd + strcspn (*cmd, " \t");
|
| 106 |
|
|
filename = savestring ((*cmd), end - (*cmd));
|
| 107 |
|
|
make_cleanup (xfree, filename);
|
| 108 |
|
|
(*cmd) = skip_spaces (end);
|
| 109 |
|
|
}
|
| 110 |
|
|
gdb_assert (filename != NULL);
|
| 111 |
|
|
|
| 112 |
|
|
fullname = tilde_expand (filename);
|
| 113 |
|
|
make_cleanup (xfree, fullname);
|
| 114 |
|
|
|
| 115 |
|
|
return fullname;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
FILE *
|
| 119 |
|
|
fopen_with_cleanup (const char *filename, const char *mode)
|
| 120 |
|
|
{
|
| 121 |
|
|
FILE *file = fopen (filename, mode);
|
| 122 |
|
|
if (file == NULL)
|
| 123 |
|
|
perror_with_name (filename);
|
| 124 |
|
|
make_cleanup_fclose (file);
|
| 125 |
|
|
return file;
|
| 126 |
|
|
}
|
| 127 |
|
|
|
| 128 |
|
|
static bfd *
|
| 129 |
|
|
bfd_openr_with_cleanup (const char *filename, const char *target)
|
| 130 |
|
|
{
|
| 131 |
|
|
bfd *ibfd;
|
| 132 |
|
|
|
| 133 |
|
|
ibfd = bfd_openr (filename, target);
|
| 134 |
|
|
if (ibfd == NULL)
|
| 135 |
|
|
error (_("Failed to open %s: %s."), filename,
|
| 136 |
|
|
bfd_errmsg (bfd_get_error ()));
|
| 137 |
|
|
|
| 138 |
|
|
make_cleanup_bfd_close (ibfd);
|
| 139 |
|
|
if (!bfd_check_format (ibfd, bfd_object))
|
| 140 |
|
|
error (_("'%s' is not a recognized file format."), filename);
|
| 141 |
|
|
|
| 142 |
|
|
return ibfd;
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
static bfd *
|
| 146 |
|
|
bfd_openw_with_cleanup (const char *filename, const char *target,
|
| 147 |
|
|
const char *mode)
|
| 148 |
|
|
{
|
| 149 |
|
|
bfd *obfd;
|
| 150 |
|
|
|
| 151 |
|
|
if (*mode == 'w') /* Write: create new file */
|
| 152 |
|
|
{
|
| 153 |
|
|
obfd = bfd_openw (filename, target);
|
| 154 |
|
|
if (obfd == NULL)
|
| 155 |
|
|
error (_("Failed to open %s: %s."), filename,
|
| 156 |
|
|
bfd_errmsg (bfd_get_error ()));
|
| 157 |
|
|
make_cleanup_bfd_close (obfd);
|
| 158 |
|
|
if (!bfd_set_format (obfd, bfd_object))
|
| 159 |
|
|
error (_("bfd_openw_with_cleanup: %s."), bfd_errmsg (bfd_get_error ()));
|
| 160 |
|
|
}
|
| 161 |
|
|
else if (*mode == 'a') /* Append to existing file */
|
| 162 |
|
|
{ /* FIXME -- doesn't work... */
|
| 163 |
|
|
error (_("bfd_openw does not work with append."));
|
| 164 |
|
|
}
|
| 165 |
|
|
else
|
| 166 |
|
|
error (_("bfd_openw_with_cleanup: unknown mode %s."), mode);
|
| 167 |
|
|
|
| 168 |
|
|
return obfd;
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
struct cmd_list_element *dump_cmdlist;
|
| 172 |
|
|
struct cmd_list_element *append_cmdlist;
|
| 173 |
|
|
struct cmd_list_element *srec_cmdlist;
|
| 174 |
|
|
struct cmd_list_element *ihex_cmdlist;
|
| 175 |
|
|
struct cmd_list_element *tekhex_cmdlist;
|
| 176 |
|
|
struct cmd_list_element *binary_dump_cmdlist;
|
| 177 |
|
|
struct cmd_list_element *binary_append_cmdlist;
|
| 178 |
|
|
|
| 179 |
|
|
static void
|
| 180 |
|
|
dump_command (char *cmd, int from_tty)
|
| 181 |
|
|
{
|
| 182 |
|
|
printf_unfiltered (_("\"dump\" must be followed by a subcommand.\n\n"));
|
| 183 |
|
|
help_list (dump_cmdlist, "dump ", -1, gdb_stdout);
|
| 184 |
|
|
}
|
| 185 |
|
|
|
| 186 |
|
|
static void
|
| 187 |
|
|
append_command (char *cmd, int from_tty)
|
| 188 |
|
|
{
|
| 189 |
|
|
printf_unfiltered (_("\"append\" must be followed by a subcommand.\n\n"));
|
| 190 |
|
|
help_list (dump_cmdlist, "append ", -1, gdb_stdout);
|
| 191 |
|
|
}
|
| 192 |
|
|
|
| 193 |
|
|
static void
|
| 194 |
|
|
dump_binary_file (const char *filename, const char *mode,
|
| 195 |
|
|
const bfd_byte *buf, int len)
|
| 196 |
|
|
{
|
| 197 |
|
|
FILE *file;
|
| 198 |
|
|
int status;
|
| 199 |
|
|
|
| 200 |
|
|
file = fopen_with_cleanup (filename, mode);
|
| 201 |
|
|
status = fwrite (buf, len, 1, file);
|
| 202 |
|
|
if (status != 1)
|
| 203 |
|
|
perror_with_name (filename);
|
| 204 |
|
|
}
|
| 205 |
|
|
|
| 206 |
|
|
static void
|
| 207 |
|
|
dump_bfd_file (const char *filename, const char *mode,
|
| 208 |
|
|
const char *target, CORE_ADDR vaddr,
|
| 209 |
|
|
const bfd_byte *buf, int len)
|
| 210 |
|
|
{
|
| 211 |
|
|
bfd *obfd;
|
| 212 |
|
|
asection *osection;
|
| 213 |
|
|
|
| 214 |
|
|
obfd = bfd_openw_with_cleanup (filename, target, mode);
|
| 215 |
|
|
osection = bfd_make_section_anyway (obfd, ".newsec");
|
| 216 |
|
|
bfd_set_section_size (obfd, osection, len);
|
| 217 |
|
|
bfd_set_section_vma (obfd, osection, vaddr);
|
| 218 |
|
|
bfd_set_section_alignment (obfd, osection, 0);
|
| 219 |
|
|
bfd_set_section_flags (obfd, osection, (SEC_HAS_CONTENTS
|
| 220 |
|
|
| SEC_ALLOC
|
| 221 |
|
|
| SEC_LOAD));
|
| 222 |
|
|
osection->entsize = 0;
|
| 223 |
|
|
bfd_set_section_contents (obfd, osection, buf, 0, len);
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
static void
|
| 227 |
|
|
dump_memory_to_file (char *cmd, char *mode, char *file_format)
|
| 228 |
|
|
{
|
| 229 |
|
|
struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
|
| 230 |
|
|
CORE_ADDR lo;
|
| 231 |
|
|
CORE_ADDR hi;
|
| 232 |
|
|
ULONGEST count;
|
| 233 |
|
|
char *filename;
|
| 234 |
|
|
void *buf;
|
| 235 |
|
|
char *lo_exp;
|
| 236 |
|
|
char *hi_exp;
|
| 237 |
|
|
int len;
|
| 238 |
|
|
|
| 239 |
|
|
/* Open the file. */
|
| 240 |
|
|
filename = scan_filename_with_cleanup (&cmd, NULL);
|
| 241 |
|
|
|
| 242 |
|
|
/* Find the low address. */
|
| 243 |
|
|
if (cmd == NULL || *cmd == '\0')
|
| 244 |
|
|
error (_("Missing start address."));
|
| 245 |
|
|
lo_exp = scan_expression_with_cleanup (&cmd, NULL);
|
| 246 |
|
|
|
| 247 |
|
|
/* Find the second address - rest of line. */
|
| 248 |
|
|
if (cmd == NULL || *cmd == '\0')
|
| 249 |
|
|
error (_("Missing stop address."));
|
| 250 |
|
|
hi_exp = cmd;
|
| 251 |
|
|
|
| 252 |
|
|
lo = parse_and_eval_address (lo_exp);
|
| 253 |
|
|
hi = parse_and_eval_address (hi_exp);
|
| 254 |
|
|
if (hi <= lo)
|
| 255 |
|
|
error (_("Invalid memory address range (start >= end)."));
|
| 256 |
|
|
count = hi - lo;
|
| 257 |
|
|
|
| 258 |
|
|
/* FIXME: Should use read_memory_partial() and a magic blocking
|
| 259 |
|
|
value. */
|
| 260 |
|
|
buf = xmalloc (count);
|
| 261 |
|
|
make_cleanup (xfree, buf);
|
| 262 |
|
|
target_read_memory (lo, buf, count);
|
| 263 |
|
|
|
| 264 |
|
|
/* Have everything. Open/write the data. */
|
| 265 |
|
|
if (file_format == NULL || strcmp (file_format, "binary") == 0)
|
| 266 |
|
|
{
|
| 267 |
|
|
dump_binary_file (filename, mode, buf, count);
|
| 268 |
|
|
}
|
| 269 |
|
|
else
|
| 270 |
|
|
{
|
| 271 |
|
|
dump_bfd_file (filename, mode, file_format, lo, buf, count);
|
| 272 |
|
|
}
|
| 273 |
|
|
|
| 274 |
|
|
do_cleanups (old_cleanups);
|
| 275 |
|
|
}
|
| 276 |
|
|
|
| 277 |
|
|
static void
|
| 278 |
|
|
dump_memory_command (char *cmd, char *mode)
|
| 279 |
|
|
{
|
| 280 |
|
|
dump_memory_to_file (cmd, mode, "binary");
|
| 281 |
|
|
}
|
| 282 |
|
|
|
| 283 |
|
|
static void
|
| 284 |
|
|
dump_value_to_file (char *cmd, char *mode, char *file_format)
|
| 285 |
|
|
{
|
| 286 |
|
|
struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
|
| 287 |
|
|
struct value *val;
|
| 288 |
|
|
char *filename;
|
| 289 |
|
|
|
| 290 |
|
|
/* Open the file. */
|
| 291 |
|
|
filename = scan_filename_with_cleanup (&cmd, NULL);
|
| 292 |
|
|
|
| 293 |
|
|
/* Find the value. */
|
| 294 |
|
|
if (cmd == NULL || *cmd == '\0')
|
| 295 |
|
|
error (_("No value to %s."), *mode == 'a' ? "append" : "dump");
|
| 296 |
|
|
val = parse_and_eval (cmd);
|
| 297 |
|
|
if (val == NULL)
|
| 298 |
|
|
error (_("Invalid expression."));
|
| 299 |
|
|
|
| 300 |
|
|
/* Have everything. Open/write the data. */
|
| 301 |
|
|
if (file_format == NULL || strcmp (file_format, "binary") == 0)
|
| 302 |
|
|
{
|
| 303 |
|
|
dump_binary_file (filename, mode, value_contents (val),
|
| 304 |
|
|
TYPE_LENGTH (value_type (val)));
|
| 305 |
|
|
}
|
| 306 |
|
|
else
|
| 307 |
|
|
{
|
| 308 |
|
|
CORE_ADDR vaddr;
|
| 309 |
|
|
|
| 310 |
|
|
if (VALUE_LVAL (val))
|
| 311 |
|
|
{
|
| 312 |
|
|
vaddr = VALUE_ADDRESS (val);
|
| 313 |
|
|
}
|
| 314 |
|
|
else
|
| 315 |
|
|
{
|
| 316 |
|
|
vaddr = 0;
|
| 317 |
|
|
warning (_("value is not an lval: address assumed to be zero"));
|
| 318 |
|
|
}
|
| 319 |
|
|
|
| 320 |
|
|
dump_bfd_file (filename, mode, file_format, vaddr,
|
| 321 |
|
|
value_contents (val),
|
| 322 |
|
|
TYPE_LENGTH (value_type (val)));
|
| 323 |
|
|
}
|
| 324 |
|
|
|
| 325 |
|
|
do_cleanups (old_cleanups);
|
| 326 |
|
|
}
|
| 327 |
|
|
|
| 328 |
|
|
static void
|
| 329 |
|
|
dump_value_command (char *cmd, char *mode)
|
| 330 |
|
|
{
|
| 331 |
|
|
dump_value_to_file (cmd, mode, "binary");
|
| 332 |
|
|
}
|
| 333 |
|
|
|
| 334 |
|
|
static void
|
| 335 |
|
|
dump_srec_memory (char *args, int from_tty)
|
| 336 |
|
|
{
|
| 337 |
|
|
dump_memory_to_file (args, FOPEN_WB, "srec");
|
| 338 |
|
|
}
|
| 339 |
|
|
|
| 340 |
|
|
static void
|
| 341 |
|
|
dump_srec_value (char *args, int from_tty)
|
| 342 |
|
|
{
|
| 343 |
|
|
dump_value_to_file (args, FOPEN_WB, "srec");
|
| 344 |
|
|
}
|
| 345 |
|
|
|
| 346 |
|
|
static void
|
| 347 |
|
|
dump_ihex_memory (char *args, int from_tty)
|
| 348 |
|
|
{
|
| 349 |
|
|
dump_memory_to_file (args, FOPEN_WB, "ihex");
|
| 350 |
|
|
}
|
| 351 |
|
|
|
| 352 |
|
|
static void
|
| 353 |
|
|
dump_ihex_value (char *args, int from_tty)
|
| 354 |
|
|
{
|
| 355 |
|
|
dump_value_to_file (args, FOPEN_WB, "ihex");
|
| 356 |
|
|
}
|
| 357 |
|
|
|
| 358 |
|
|
static void
|
| 359 |
|
|
dump_tekhex_memory (char *args, int from_tty)
|
| 360 |
|
|
{
|
| 361 |
|
|
dump_memory_to_file (args, FOPEN_WB, "tekhex");
|
| 362 |
|
|
}
|
| 363 |
|
|
|
| 364 |
|
|
static void
|
| 365 |
|
|
dump_tekhex_value (char *args, int from_tty)
|
| 366 |
|
|
{
|
| 367 |
|
|
dump_value_to_file (args, FOPEN_WB, "tekhex");
|
| 368 |
|
|
}
|
| 369 |
|
|
|
| 370 |
|
|
static void
|
| 371 |
|
|
dump_binary_memory (char *args, int from_tty)
|
| 372 |
|
|
{
|
| 373 |
|
|
dump_memory_to_file (args, FOPEN_WB, "binary");
|
| 374 |
|
|
}
|
| 375 |
|
|
|
| 376 |
|
|
static void
|
| 377 |
|
|
dump_binary_value (char *args, int from_tty)
|
| 378 |
|
|
{
|
| 379 |
|
|
dump_value_to_file (args, FOPEN_WB, "binary");
|
| 380 |
|
|
}
|
| 381 |
|
|
|
| 382 |
|
|
static void
|
| 383 |
|
|
append_binary_memory (char *args, int from_tty)
|
| 384 |
|
|
{
|
| 385 |
|
|
dump_memory_to_file (args, FOPEN_AB, "binary");
|
| 386 |
|
|
}
|
| 387 |
|
|
|
| 388 |
|
|
static void
|
| 389 |
|
|
append_binary_value (char *args, int from_tty)
|
| 390 |
|
|
{
|
| 391 |
|
|
dump_value_to_file (args, FOPEN_AB, "binary");
|
| 392 |
|
|
}
|
| 393 |
|
|
|
| 394 |
|
|
struct dump_context
|
| 395 |
|
|
{
|
| 396 |
|
|
void (*func) (char *cmd, char *mode);
|
| 397 |
|
|
char *mode;
|
| 398 |
|
|
};
|
| 399 |
|
|
|
| 400 |
|
|
static void
|
| 401 |
|
|
call_dump_func (struct cmd_list_element *c, char *args, int from_tty)
|
| 402 |
|
|
{
|
| 403 |
|
|
struct dump_context *d = get_cmd_context (c);
|
| 404 |
|
|
d->func (args, d->mode);
|
| 405 |
|
|
}
|
| 406 |
|
|
|
| 407 |
|
|
void
|
| 408 |
|
|
add_dump_command (char *name, void (*func) (char *args, char *mode),
|
| 409 |
|
|
char *descr)
|
| 410 |
|
|
|
| 411 |
|
|
{
|
| 412 |
|
|
struct cmd_list_element *c;
|
| 413 |
|
|
struct dump_context *d;
|
| 414 |
|
|
|
| 415 |
|
|
c = add_cmd (name, all_commands, NULL, descr, &dump_cmdlist);
|
| 416 |
|
|
c->completer = filename_completer;
|
| 417 |
|
|
d = XMALLOC (struct dump_context);
|
| 418 |
|
|
d->func = func;
|
| 419 |
|
|
d->mode = FOPEN_WB;
|
| 420 |
|
|
set_cmd_context (c, d);
|
| 421 |
|
|
c->func = call_dump_func;
|
| 422 |
|
|
|
| 423 |
|
|
c = add_cmd (name, all_commands, NULL, descr, &append_cmdlist);
|
| 424 |
|
|
c->completer = filename_completer;
|
| 425 |
|
|
d = XMALLOC (struct dump_context);
|
| 426 |
|
|
d->func = func;
|
| 427 |
|
|
d->mode = FOPEN_AB;
|
| 428 |
|
|
set_cmd_context (c, d);
|
| 429 |
|
|
c->func = call_dump_func;
|
| 430 |
|
|
|
| 431 |
|
|
/* Replace "Dump " at start of docstring with "Append " (borrowed
|
| 432 |
|
|
from [deleted] deprecated_add_show_from_set). */
|
| 433 |
|
|
if ( c->doc[0] == 'W'
|
| 434 |
|
|
&& c->doc[1] == 'r'
|
| 435 |
|
|
&& c->doc[2] == 'i'
|
| 436 |
|
|
&& c->doc[3] == 't'
|
| 437 |
|
|
&& c->doc[4] == 'e'
|
| 438 |
|
|
&& c->doc[5] == ' ')
|
| 439 |
|
|
c->doc = concat ("Append ", c->doc + 6, (char *)NULL);
|
| 440 |
|
|
}
|
| 441 |
|
|
|
| 442 |
|
|
/* Opaque data for restore_section_callback. */
|
| 443 |
|
|
struct callback_data {
|
| 444 |
|
|
long load_offset;
|
| 445 |
|
|
CORE_ADDR load_start;
|
| 446 |
|
|
CORE_ADDR load_end;
|
| 447 |
|
|
};
|
| 448 |
|
|
|
| 449 |
|
|
/* Function: restore_section_callback.
|
| 450 |
|
|
|
| 451 |
|
|
Callback function for bfd_map_over_sections.
|
| 452 |
|
|
Selectively loads the sections into memory. */
|
| 453 |
|
|
|
| 454 |
|
|
static void
|
| 455 |
|
|
restore_section_callback (bfd *ibfd, asection *isec, void *args)
|
| 456 |
|
|
{
|
| 457 |
|
|
struct callback_data *data = args;
|
| 458 |
|
|
bfd_vma sec_start = bfd_section_vma (ibfd, isec);
|
| 459 |
|
|
bfd_size_type size = bfd_section_size (ibfd, isec);
|
| 460 |
|
|
bfd_vma sec_end = sec_start + size;
|
| 461 |
|
|
bfd_size_type sec_offset = 0;
|
| 462 |
|
|
bfd_size_type sec_load_count = size;
|
| 463 |
|
|
struct cleanup *old_chain;
|
| 464 |
|
|
gdb_byte *buf;
|
| 465 |
|
|
int ret;
|
| 466 |
|
|
|
| 467 |
|
|
/* Ignore non-loadable sections, eg. from elf files. */
|
| 468 |
|
|
if (!(bfd_get_section_flags (ibfd, isec) & SEC_LOAD))
|
| 469 |
|
|
return;
|
| 470 |
|
|
|
| 471 |
|
|
/* Does the section overlap with the desired restore range? */
|
| 472 |
|
|
if (sec_end <= data->load_start
|
| 473 |
|
|
|| (data->load_end > 0 && sec_start >= data->load_end))
|
| 474 |
|
|
{
|
| 475 |
|
|
/* No, no useable data in this section. */
|
| 476 |
|
|
printf_filtered (_("skipping section %s...\n"),
|
| 477 |
|
|
bfd_section_name (ibfd, isec));
|
| 478 |
|
|
return;
|
| 479 |
|
|
}
|
| 480 |
|
|
|
| 481 |
|
|
/* Compare section address range with user-requested
|
| 482 |
|
|
address range (if any). Compute where the actual
|
| 483 |
|
|
transfer should start and end. */
|
| 484 |
|
|
if (sec_start < data->load_start)
|
| 485 |
|
|
sec_offset = data->load_start - sec_start;
|
| 486 |
|
|
/* Size of a partial transfer: */
|
| 487 |
|
|
sec_load_count -= sec_offset;
|
| 488 |
|
|
if (data->load_end > 0 && sec_end > data->load_end)
|
| 489 |
|
|
sec_load_count -= sec_end - data->load_end;
|
| 490 |
|
|
|
| 491 |
|
|
/* Get the data. */
|
| 492 |
|
|
buf = xmalloc (size);
|
| 493 |
|
|
old_chain = make_cleanup (xfree, buf);
|
| 494 |
|
|
if (!bfd_get_section_contents (ibfd, isec, buf, 0, size))
|
| 495 |
|
|
error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd),
|
| 496 |
|
|
bfd_errmsg (bfd_get_error ()));
|
| 497 |
|
|
|
| 498 |
|
|
printf_filtered ("Restoring section %s (0x%lx to 0x%lx)",
|
| 499 |
|
|
bfd_section_name (ibfd, isec),
|
| 500 |
|
|
(unsigned long) sec_start,
|
| 501 |
|
|
(unsigned long) sec_end);
|
| 502 |
|
|
|
| 503 |
|
|
if (data->load_offset != 0 || data->load_start != 0 || data->load_end != 0)
|
| 504 |
|
|
printf_filtered (" into memory (0x%s to 0x%s)\n",
|
| 505 |
|
|
paddr_nz ((unsigned long) sec_start
|
| 506 |
|
|
+ sec_offset + data->load_offset),
|
| 507 |
|
|
paddr_nz ((unsigned long) sec_start + sec_offset
|
| 508 |
|
|
+ data->load_offset + sec_load_count));
|
| 509 |
|
|
else
|
| 510 |
|
|
puts_filtered ("\n");
|
| 511 |
|
|
|
| 512 |
|
|
/* Write the data. */
|
| 513 |
|
|
ret = target_write_memory (sec_start + sec_offset + data->load_offset,
|
| 514 |
|
|
buf + sec_offset, sec_load_count);
|
| 515 |
|
|
if (ret != 0)
|
| 516 |
|
|
warning (_("restore: memory write failed (%s)."), safe_strerror (ret));
|
| 517 |
|
|
do_cleanups (old_chain);
|
| 518 |
|
|
return;
|
| 519 |
|
|
}
|
| 520 |
|
|
|
| 521 |
|
|
static void
|
| 522 |
|
|
restore_binary_file (char *filename, struct callback_data *data)
|
| 523 |
|
|
{
|
| 524 |
|
|
FILE *file = fopen_with_cleanup (filename, FOPEN_RB);
|
| 525 |
|
|
int status;
|
| 526 |
|
|
gdb_byte *buf;
|
| 527 |
|
|
long len;
|
| 528 |
|
|
|
| 529 |
|
|
/* Get the file size for reading. */
|
| 530 |
|
|
if (fseek (file, 0, SEEK_END) == 0)
|
| 531 |
|
|
len = ftell (file);
|
| 532 |
|
|
else
|
| 533 |
|
|
perror_with_name (filename);
|
| 534 |
|
|
|
| 535 |
|
|
if (len <= data->load_start)
|
| 536 |
|
|
error (_("Start address is greater than length of binary file %s."),
|
| 537 |
|
|
filename);
|
| 538 |
|
|
|
| 539 |
|
|
/* Chop off "len" if it exceeds the requested load_end addr. */
|
| 540 |
|
|
if (data->load_end != 0 && data->load_end < len)
|
| 541 |
|
|
len = data->load_end;
|
| 542 |
|
|
/* Chop off "len" if the requested load_start addr skips some bytes. */
|
| 543 |
|
|
if (data->load_start > 0)
|
| 544 |
|
|
len -= data->load_start;
|
| 545 |
|
|
|
| 546 |
|
|
printf_filtered
|
| 547 |
|
|
("Restoring binary file %s into memory (0x%lx to 0x%lx)\n",
|
| 548 |
|
|
filename,
|
| 549 |
|
|
(unsigned long) data->load_start + data->load_offset,
|
| 550 |
|
|
(unsigned long) data->load_start + data->load_offset + len);
|
| 551 |
|
|
|
| 552 |
|
|
/* Now set the file pos to the requested load start pos. */
|
| 553 |
|
|
if (fseek (file, data->load_start, SEEK_SET) != 0)
|
| 554 |
|
|
perror_with_name (filename);
|
| 555 |
|
|
|
| 556 |
|
|
/* Now allocate a buffer and read the file contents. */
|
| 557 |
|
|
buf = xmalloc (len);
|
| 558 |
|
|
make_cleanup (xfree, buf);
|
| 559 |
|
|
if (fread (buf, 1, len, file) != len)
|
| 560 |
|
|
perror_with_name (filename);
|
| 561 |
|
|
|
| 562 |
|
|
/* Now write the buffer into target memory. */
|
| 563 |
|
|
len = target_write_memory (data->load_start + data->load_offset, buf, len);
|
| 564 |
|
|
if (len != 0)
|
| 565 |
|
|
warning (_("restore: memory write failed (%s)."), safe_strerror (len));
|
| 566 |
|
|
return;
|
| 567 |
|
|
}
|
| 568 |
|
|
|
| 569 |
|
|
static void
|
| 570 |
|
|
restore_command (char *args, int from_tty)
|
| 571 |
|
|
{
|
| 572 |
|
|
char *filename;
|
| 573 |
|
|
struct callback_data data;
|
| 574 |
|
|
bfd *ibfd;
|
| 575 |
|
|
int binary_flag = 0;
|
| 576 |
|
|
|
| 577 |
|
|
if (!target_has_execution)
|
| 578 |
|
|
noprocess ();
|
| 579 |
|
|
|
| 580 |
|
|
data.load_offset = 0;
|
| 581 |
|
|
data.load_start = 0;
|
| 582 |
|
|
data.load_end = 0;
|
| 583 |
|
|
|
| 584 |
|
|
/* Parse the input arguments. First is filename (required). */
|
| 585 |
|
|
filename = scan_filename_with_cleanup (&args, NULL);
|
| 586 |
|
|
if (args != NULL && *args != '\0')
|
| 587 |
|
|
{
|
| 588 |
|
|
char *binary_string = "binary";
|
| 589 |
|
|
|
| 590 |
|
|
/* Look for optional "binary" flag. */
|
| 591 |
|
|
if (strncmp (args, binary_string, strlen (binary_string)) == 0)
|
| 592 |
|
|
{
|
| 593 |
|
|
binary_flag = 1;
|
| 594 |
|
|
args += strlen (binary_string);
|
| 595 |
|
|
args = skip_spaces (args);
|
| 596 |
|
|
}
|
| 597 |
|
|
/* Parse offset (optional). */
|
| 598 |
|
|
if (args != NULL && *args != '\0')
|
| 599 |
|
|
data.load_offset =
|
| 600 |
|
|
parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
|
| 601 |
|
|
if (args != NULL && *args != '\0')
|
| 602 |
|
|
{
|
| 603 |
|
|
/* Parse start address (optional). */
|
| 604 |
|
|
data.load_start =
|
| 605 |
|
|
parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
|
| 606 |
|
|
if (args != NULL && *args != '\0')
|
| 607 |
|
|
{
|
| 608 |
|
|
/* Parse end address (optional). */
|
| 609 |
|
|
data.load_end = parse_and_eval_long (args);
|
| 610 |
|
|
if (data.load_end <= data.load_start)
|
| 611 |
|
|
error (_("Start must be less than end."));
|
| 612 |
|
|
}
|
| 613 |
|
|
}
|
| 614 |
|
|
}
|
| 615 |
|
|
|
| 616 |
|
|
if (info_verbose)
|
| 617 |
|
|
printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
|
| 618 |
|
|
filename, (unsigned long) data.load_offset,
|
| 619 |
|
|
(unsigned long) data.load_start,
|
| 620 |
|
|
(unsigned long) data.load_end);
|
| 621 |
|
|
|
| 622 |
|
|
if (binary_flag)
|
| 623 |
|
|
{
|
| 624 |
|
|
restore_binary_file (filename, &data);
|
| 625 |
|
|
}
|
| 626 |
|
|
else
|
| 627 |
|
|
{
|
| 628 |
|
|
/* Open the file for loading. */
|
| 629 |
|
|
ibfd = bfd_openr_with_cleanup (filename, NULL);
|
| 630 |
|
|
|
| 631 |
|
|
/* Process the sections. */
|
| 632 |
|
|
bfd_map_over_sections (ibfd, restore_section_callback, &data);
|
| 633 |
|
|
}
|
| 634 |
|
|
return;
|
| 635 |
|
|
}
|
| 636 |
|
|
|
| 637 |
|
|
static void
|
| 638 |
|
|
srec_dump_command (char *cmd, int from_tty)
|
| 639 |
|
|
{
|
| 640 |
|
|
printf_unfiltered ("\"dump srec\" must be followed by a subcommand.\n");
|
| 641 |
|
|
help_list (srec_cmdlist, "dump srec ", -1, gdb_stdout);
|
| 642 |
|
|
}
|
| 643 |
|
|
|
| 644 |
|
|
static void
|
| 645 |
|
|
ihex_dump_command (char *cmd, int from_tty)
|
| 646 |
|
|
{
|
| 647 |
|
|
printf_unfiltered ("\"dump ihex\" must be followed by a subcommand.\n");
|
| 648 |
|
|
help_list (ihex_cmdlist, "dump ihex ", -1, gdb_stdout);
|
| 649 |
|
|
}
|
| 650 |
|
|
|
| 651 |
|
|
static void
|
| 652 |
|
|
tekhex_dump_command (char *cmd, int from_tty)
|
| 653 |
|
|
{
|
| 654 |
|
|
printf_unfiltered ("\"dump tekhex\" must be followed by a subcommand.\n");
|
| 655 |
|
|
help_list (tekhex_cmdlist, "dump tekhex ", -1, gdb_stdout);
|
| 656 |
|
|
}
|
| 657 |
|
|
|
| 658 |
|
|
static void
|
| 659 |
|
|
binary_dump_command (char *cmd, int from_tty)
|
| 660 |
|
|
{
|
| 661 |
|
|
printf_unfiltered ("\"dump binary\" must be followed by a subcommand.\n");
|
| 662 |
|
|
help_list (binary_dump_cmdlist, "dump binary ", -1, gdb_stdout);
|
| 663 |
|
|
}
|
| 664 |
|
|
|
| 665 |
|
|
static void
|
| 666 |
|
|
binary_append_command (char *cmd, int from_tty)
|
| 667 |
|
|
{
|
| 668 |
|
|
printf_unfiltered ("\"append binary\" must be followed by a subcommand.\n");
|
| 669 |
|
|
help_list (binary_append_cmdlist, "append binary ", -1, gdb_stdout);
|
| 670 |
|
|
}
|
| 671 |
|
|
|
| 672 |
|
|
extern initialize_file_ftype _initialize_cli_dump; /* -Wmissing-prototypes */
|
| 673 |
|
|
|
| 674 |
|
|
void
|
| 675 |
|
|
_initialize_cli_dump (void)
|
| 676 |
|
|
{
|
| 677 |
|
|
struct cmd_list_element *c;
|
| 678 |
|
|
add_prefix_cmd ("dump", class_vars, dump_command, _("\
|
| 679 |
|
|
Dump target code/data to a local file."),
|
| 680 |
|
|
&dump_cmdlist, "dump ",
|
| 681 |
|
|
0/*allow-unknown*/,
|
| 682 |
|
|
&cmdlist);
|
| 683 |
|
|
add_prefix_cmd ("append", class_vars, append_command, _("\
|
| 684 |
|
|
Append target code/data to a local file."),
|
| 685 |
|
|
&append_cmdlist, "append ",
|
| 686 |
|
|
0/*allow-unknown*/,
|
| 687 |
|
|
&cmdlist);
|
| 688 |
|
|
|
| 689 |
|
|
add_dump_command ("memory", dump_memory_command, "\
|
| 690 |
|
|
Write contents of memory to a raw binary file.\n\
|
| 691 |
|
|
Arguments are FILE START STOP. Writes the contents of memory within the\n\
|
| 692 |
|
|
range [START .. STOP) to the specifed FILE in raw target ordered bytes.");
|
| 693 |
|
|
|
| 694 |
|
|
add_dump_command ("value", dump_value_command, "\
|
| 695 |
|
|
Write the value of an expression to a raw binary file.\n\
|
| 696 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION to\n\
|
| 697 |
|
|
the specified FILE in raw target ordered bytes.");
|
| 698 |
|
|
|
| 699 |
|
|
add_prefix_cmd ("srec", all_commands, srec_dump_command, _("\
|
| 700 |
|
|
Write target code/data to an srec file."),
|
| 701 |
|
|
&srec_cmdlist, "dump srec ",
|
| 702 |
|
|
|
| 703 |
|
|
&dump_cmdlist);
|
| 704 |
|
|
|
| 705 |
|
|
add_prefix_cmd ("ihex", all_commands, ihex_dump_command, _("\
|
| 706 |
|
|
Write target code/data to an intel hex file."),
|
| 707 |
|
|
&ihex_cmdlist, "dump ihex ",
|
| 708 |
|
|
|
| 709 |
|
|
&dump_cmdlist);
|
| 710 |
|
|
|
| 711 |
|
|
add_prefix_cmd ("tekhex", all_commands, tekhex_dump_command, _("\
|
| 712 |
|
|
Write target code/data to a tekhex file."),
|
| 713 |
|
|
&tekhex_cmdlist, "dump tekhex ",
|
| 714 |
|
|
|
| 715 |
|
|
&dump_cmdlist);
|
| 716 |
|
|
|
| 717 |
|
|
add_prefix_cmd ("binary", all_commands, binary_dump_command, _("\
|
| 718 |
|
|
Write target code/data to a raw binary file."),
|
| 719 |
|
|
&binary_dump_cmdlist, "dump binary ",
|
| 720 |
|
|
|
| 721 |
|
|
&dump_cmdlist);
|
| 722 |
|
|
|
| 723 |
|
|
add_prefix_cmd ("binary", all_commands, binary_append_command, _("\
|
| 724 |
|
|
Append target code/data to a raw binary file."),
|
| 725 |
|
|
&binary_append_cmdlist, "append binary ",
|
| 726 |
|
|
|
| 727 |
|
|
&append_cmdlist);
|
| 728 |
|
|
|
| 729 |
|
|
add_cmd ("memory", all_commands, dump_srec_memory, _("\
|
| 730 |
|
|
Write contents of memory to an srec file.\n\
|
| 731 |
|
|
Arguments are FILE START STOP. Writes the contents of memory\n\
|
| 732 |
|
|
within the range [START .. STOP) to the specifed FILE in srec format."),
|
| 733 |
|
|
&srec_cmdlist);
|
| 734 |
|
|
|
| 735 |
|
|
add_cmd ("value", all_commands, dump_srec_value, _("\
|
| 736 |
|
|
Write the value of an expression to an srec file.\n\
|
| 737 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
|
| 738 |
|
|
to the specified FILE in srec format."),
|
| 739 |
|
|
&srec_cmdlist);
|
| 740 |
|
|
|
| 741 |
|
|
add_cmd ("memory", all_commands, dump_ihex_memory, _("\
|
| 742 |
|
|
Write contents of memory to an ihex file.\n\
|
| 743 |
|
|
Arguments are FILE START STOP. Writes the contents of memory within\n\
|
| 744 |
|
|
the range [START .. STOP) to the specifed FILE in intel hex format."),
|
| 745 |
|
|
&ihex_cmdlist);
|
| 746 |
|
|
|
| 747 |
|
|
add_cmd ("value", all_commands, dump_ihex_value, _("\
|
| 748 |
|
|
Write the value of an expression to an ihex file.\n\
|
| 749 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
|
| 750 |
|
|
to the specified FILE in intel hex format."),
|
| 751 |
|
|
&ihex_cmdlist);
|
| 752 |
|
|
|
| 753 |
|
|
add_cmd ("memory", all_commands, dump_tekhex_memory, _("\
|
| 754 |
|
|
Write contents of memory to a tekhex file.\n\
|
| 755 |
|
|
Arguments are FILE START STOP. Writes the contents of memory\n\
|
| 756 |
|
|
within the range [START .. STOP) to the specifed FILE in tekhex format."),
|
| 757 |
|
|
&tekhex_cmdlist);
|
| 758 |
|
|
|
| 759 |
|
|
add_cmd ("value", all_commands, dump_tekhex_value, _("\
|
| 760 |
|
|
Write the value of an expression to a tekhex file.\n\
|
| 761 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
|
| 762 |
|
|
to the specified FILE in tekhex format."),
|
| 763 |
|
|
&tekhex_cmdlist);
|
| 764 |
|
|
|
| 765 |
|
|
add_cmd ("memory", all_commands, dump_binary_memory, _("\
|
| 766 |
|
|
Write contents of memory to a raw binary file.\n\
|
| 767 |
|
|
Arguments are FILE START STOP. Writes the contents of memory\n\
|
| 768 |
|
|
within the range [START .. STOP) to the specifed FILE in binary format."),
|
| 769 |
|
|
&binary_dump_cmdlist);
|
| 770 |
|
|
|
| 771 |
|
|
add_cmd ("value", all_commands, dump_binary_value, _("\
|
| 772 |
|
|
Write the value of an expression to a raw binary file.\n\
|
| 773 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
|
| 774 |
|
|
to the specified FILE in raw target ordered bytes."),
|
| 775 |
|
|
&binary_dump_cmdlist);
|
| 776 |
|
|
|
| 777 |
|
|
add_cmd ("memory", all_commands, append_binary_memory, _("\
|
| 778 |
|
|
Append contents of memory to a raw binary file.\n\
|
| 779 |
|
|
Arguments are FILE START STOP. Writes the contents of memory within the\n\
|
| 780 |
|
|
range [START .. STOP) to the specifed FILE in raw target ordered bytes."),
|
| 781 |
|
|
&binary_append_cmdlist);
|
| 782 |
|
|
|
| 783 |
|
|
add_cmd ("value", all_commands, append_binary_value, _("\
|
| 784 |
|
|
Append the value of an expression to a raw binary file.\n\
|
| 785 |
|
|
Arguments are FILE EXPRESSION. Writes the value of EXPRESSION\n\
|
| 786 |
|
|
to the specified FILE in raw target ordered bytes."),
|
| 787 |
|
|
&binary_append_cmdlist);
|
| 788 |
|
|
|
| 789 |
|
|
c = add_com ("restore", class_vars, restore_command, _("\
|
| 790 |
|
|
Restore the contents of FILE to target memory.\n\
|
| 791 |
|
|
Arguments are FILE OFFSET START END where all except FILE are optional.\n\
|
| 792 |
|
|
OFFSET will be added to the base address of the file (default zero).\n\
|
| 793 |
|
|
If START and END are given, only the file contents within that range\n\
|
| 794 |
|
|
(file relative) will be restored to target memory."));
|
| 795 |
|
|
c->completer = filename_completer;
|
| 796 |
|
|
/* FIXME: completers for other commands. */
|
| 797 |
|
|
}
|