OpenCores
URL https://opencores.org/ocsvn/riscv_vhdl/riscv_vhdl/trunk

Subversion Repositories riscv_vhdl

[/] [riscv_vhdl/] [trunk/] [debugger/] [src/] [libdbg64g/] [services/] [exec/] [cmd/] [cmd_memdump.cpp] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sergeykhbr
/**
2
 * @file
3
 * @copyright  Copyright 2016 GNSS Sensor Ltd. All right reserved.
4
 * @author     Sergey Khabarov - sergeykhbr@gmail.com
5
 * @brief      Dump memory range into file.
6
 */
7
 
8
#include "cmd_memdump.h"
9
#include <string>
10
 
11
namespace debugger {
12
 
13
CmdMemDump::CmdMemDump(ITap *tap, ISocInfo *info)
14
    : ICommand ("memdump", tap, info) {
15
 
16
    briefDescr_.make_string("Dump memory to file");
17
    detailedDescr_.make_string(
18
        "Description:\n"
19
        "    Dump memory to file (default in Binary format).\n"
20
        "Usage:\n"
21
        "    memdump <addr> <bytes> [filepath] [bin|hex]\n"
22
        "Example:\n"
23
        "    memdump 0x0 8192 dump.bin\n"
24
        "    memdump 0x40000000 524288 dump.hex hex\n"
25
        "    memdump 0x10000000 128 \"c:/My Documents/dump.bin\"\n");
26
}
27
 
28
bool CmdMemDump::isValid(AttributeType *args) {
29
    if ((*args)[0u].is_equal(cmdName_.to_string())
30
     && (args->size() == 4 || args->size() == 5)) {
31
        return CMD_VALID;
32
    }
33
    return CMD_INVALID;
34
}
35
 
36
void CmdMemDump::exec(AttributeType *args, AttributeType *res) {
37
    res->make_nil();
38
    if (!isValid(args)) {
39
        generateError(res, "Wrong argument list");
40
        return;
41
    }
42
 
43
    const char *filename = (*args)[3].to_string();
44
    FILE *fd = fopen(filename, "w");
45
    if (fd == NULL) {
46
        char tst[256];
47
        RISCV_sprintf(tst, sizeof(tst), "Can't open '%s' file", filename);
48
        generateError(res, tst);
49
        return;
50
    }
51
    uint64_t addr = (*args)[1].to_uint64();
52
    int len = static_cast<int>((*args)[2].to_uint64());
53
    res->make_data(len);
54
 
55
    tap_->read(addr, len, res->data());
56
    uint8_t *dumpbuf = res->data();
57
 
58
    if (args->size() == 5 && (*args)[4].is_equal("hex")) {
59
        char t1[256];
60
        int t1_cnt = 0;
61
        int idx;
62
        for (int i = 0; i < ((len + 0xf) & ~0xf); i++) {
63
            idx = (i & ~0xf) | (0xf - (i & 0xf));
64
            if (idx > len) {
65
                t1[t1_cnt++] = ' ';
66
                t1[t1_cnt++] = ' ';
67
                continue;
68
            }
69
            t1_cnt += RISCV_sprintf(&t1[t1_cnt], sizeof(t1) - t1_cnt, "%02x",
70
                                    dumpbuf[idx]);
71
            if ((i & 0xf) != 0xf) {
72
                continue;
73
            }
74
            t1_cnt += RISCV_sprintf(&t1[t1_cnt], sizeof(t1) - t1_cnt, "\n");
75
            fwrite(t1, t1_cnt, 1, fd);
76
            t1_cnt = 0;
77
        }
78
    } else {
79
        fwrite(dumpbuf, len, 1, fd);
80
    }
81
    fclose(fd);
82
}
83
 
84
}  // namespace debugger

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.