Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dm #1182

Merged
merged 86 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
bf842b1
start making tree
MalhotraPulak Jun 1, 2021
a068306
dm./dmd/dmda/dmdw working
MalhotraPulak Jun 1, 2021
df5c418
dm- working
MalhotraPulak Jun 1, 2021
b9d2674
dmh working
MalhotraPulak Jun 1, 2021
87844bb
dm and dm= working
MalhotraPulak Jun 1, 2021
19b2ec0
updated cmd_descs.c/h file
MalhotraPulak Jun 1, 2021
6cedf23
dmm working
MalhotraPulak Jun 1, 2021
db84713
working on `dmi`
MalhotraPulak Jun 1, 2021
1a65e57
`dml` command working
MalhotraPulak Jun 1, 2021
602a2f0
change `input` -> `argv`
MalhotraPulak Jun 1, 2021
946a54c
`dmL` working
MalhotraPulak Jun 1, 2021
8eddeda
`dmS` refactor
MalhotraPulak Jun 1, 2021
22ebde0
fixed formatting of yaml file
MalhotraPulak Jun 2, 2021
32d4422
`dmS` moved from oldinput to regular
MalhotraPulak Jun 2, 2021
92fc05d
`dmq` command refactor
MalhotraPulak Jun 2, 2021
79ffc86
`rz_debug_map_list` -> `rz_debug_map_print` refactor
MalhotraPulak Jun 2, 2021
d5816e7
clang format
MalhotraPulak Jun 2, 2021
62004c0
more clang format
MalhotraPulak Jun 2, 2021
ba15a50
add `rz_debug_list()`
MalhotraPulak Jun 3, 2021
0efb668
Add documentation and move `rz_debug_map_list()` ##doc
MalhotraPulak Jun 4, 2021
a93c165
Remove non standard C
MalhotraPulak May 29, 2021
7c1ff03
Print tcache and bin info in JSON
MalhotraPulak May 29, 2021
c26c37d
Add fd and bk pointers of non empty bins
MalhotraPulak May 29, 2021
e08ba4e
Add more information about chunks
MalhotraPulak May 29, 2021
31b0e6e
Run `dmhd` for different arenas
MalhotraPulak May 30, 2021
0966d80
More info in `dmha` command and JSON version `dmhaj`
MalhotraPulak May 30, 2021
ea42381
clang format
MalhotraPulak May 30, 2021
0bb2cfc
setting up `dmh` refactor
MalhotraPulak Jun 6, 2021
d705a6a
Add the `cmd_heap_glibc.yaml` file
MalhotraPulak Jun 6, 2021
3400d29
`dmh` basic working and `dmhv` converted to `dmhl` ##refactor
MalhotraPulak Jun 6, 2021
32878d0
`dmha` working ##refactor
MalhotraPulak Jun 6, 2021
76d966e
`dmhm` working ##refactor
MalhotraPulak Jun 6, 2021
9d8b23a
`dmhc` working ##refactor
MalhotraPulak Jun 6, 2021
414fd6d
`dmhj` and `dmh*` working ##refactor
MalhotraPulak Jun 6, 2021
c4a7051
`dmhg` working ##refactor
MalhotraPulak Jun 6, 2021
94df29c
`dmhi` working ##refactor
MalhotraPulak Jun 6, 2021
0346528
fix a memory leak
MalhotraPulak Jun 6, 2021
d3525ae
Clang-format
MalhotraPulak Jun 7, 2021
03efc84
`dmht` working ##refactor
MalhotraPulak Jun 7, 2021
6cf83d9
`dmhb` working and more memory leaks fix ##refactor
MalhotraPulak Jun 7, 2021
aaf94e3
update tests ##refactor
MalhotraPulak Jun 7, 2021
6736632
`dmhf`working ##refactor
MalhotraPulak Jun 7, 2021
fad2212
`dmhd`working ##refactor
MalhotraPulak Jun 7, 2021
ed1f05c
Remove old logic and check yamllint ##refactor
MalhotraPulak Jun 7, 2021
d9fe914
Fix a bug in `dmhl`
MalhotraPulak Jun 7, 2021
b1e16db
Add documentation for `get_heap_chunks_list`
MalhotraPulak Jun 7, 2021
c7c64a9
Add documentation for `get_arenas_list` ##doc
MalhotraPulak Jun 7, 2021
ab5ede3
Minor change in documentation ##doc
MalhotraPulak Jun 7, 2021
1b7b30e
Invert ifs to decrease nested ifs ##refactor
MalhotraPulak Jun 7, 2021
9791dc5
Add status for free arena in `dmha` ##refactor
MalhotraPulak Jun 7, 2021
44340cd
Enable windows and jemalloc parsing again, use `dmw` for windows heap…
MalhotraPulak Jun 7, 2021
9476a2b
Separate printing and returning list for contents of a bin ##refactor
MalhotraPulak Jun 7, 2021
0c48695
Separate init and printing of a heap chunk ##refactor
MalhotraPulak Jun 8, 2021
558f4d4
Added headers in rz_core.h ##refactor
MalhotraPulak Jun 8, 2021
2af0415
Separate getting tcache info and printing it ##refactor
MalhotraPulak Jun 8, 2021
539a2fd
Fix bug in `dmht`
MalhotraPulak Jun 8, 2021
1a35fa8
Add corrupted list check and more headers
MalhotraPulak Jun 10, 2021
8e4c626
Fix error after rebase
MalhotraPulak Jun 10, 2021
65b829b
Add more tests
MalhotraPulak Jun 11, 2021
4c4b202
Move some functions to new file `cmd_linux_heap_glibc.c`
MalhotraPulak Jun 11, 2021
0d19e2d
Replaced repetitive code with a macro
MalhotraPulak Jun 11, 2021
22919c0
Removed <addr> argument for some commands
MalhotraPulak Jun 11, 2021
1ea2293
Added debugging enabled check for all `dm` commands
MalhotraPulak Jun 12, 2021
f3c0a21
Update test
MalhotraPulak Jun 12, 2021
bfa7332
Remove `<addr>` argument from `dm-`
MalhotraPulak Jun 12, 2021
0d646ec
Added `dma` command ##refactor
MalhotraPulak Jun 12, 2021
1513d95
Fixed test
MalhotraPulak Jun 13, 2021
45ab74e
Renamed `dma` command to `dm+` ##refactor
MalhotraPulak Jun 13, 2021
e21d79d
Implement suggested changes
MalhotraPulak Jun 14, 2021
d049e91
Implement suggested changes-2
MalhotraPulak Jun 16, 2021
4e21ade
remove unreachable code
MalhotraPulak Jun 16, 2021
f95148a
change documentation style
MalhotraPulak Jun 16, 2021
2b36e2b
Remove `static` keyword and commented code
MalhotraPulak Jun 16, 2021
108c1d6
More documentation fixes
MalhotraPulak Jun 16, 2021
c24e648
Use API instead of cmd
MalhotraPulak Jun 16, 2021
b208a65
Refactor `dmm` and `dmm.` command implementation
MalhotraPulak Jun 16, 2021
f46afbc
minor changes
MalhotraPulak Jun 16, 2021
24a0ed2
Made Heap API naming more consistent
MalhotraPulak Jun 16, 2021
d199448
`dbg->cb_printf` -> `rz_cons_printf`
MalhotraPulak Jun 16, 2021
c5a2ddd
use `ARGV_STATE` in `dm`
MalhotraPulak Jun 16, 2021
de285a0
use `ARGV_STATE` in `dmm`
MalhotraPulak Jun 16, 2021
6d92a03
remove `fastbin` option for dmhd for consistency
MalhotraPulak Jun 16, 2021
1cd104e
use `ARGV_STATE` in `dmh`
MalhotraPulak Jun 16, 2021
6d7ca8e
Replaced repetitive code with macro
MalhotraPulak Jun 17, 2021
fbc5b7e
Use `rz_cmd_state_output_array`
MalhotraPulak Jun 17, 2021
dd4ed65
Merge branch 'dev' into refactor-dm
XVilka Jun 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 244 additions & 2 deletions librz/core/cdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ RZ_IPI bool rz_core_debug_reg_list(RzCore *core, int type, int size, PJ *pj, int
return false;
}
if (rad == 1 || rad == '*') {
dbg->cb_printf("fs+%s\n", RZ_FLAGS_FS_REGISTERS);
rz_cons_printf("fs+%s\n", RZ_FLAGS_FS_REGISTERS);
}
rz_list_foreach (head, iter, item) {
ut64 value;
Expand Down Expand Up @@ -442,7 +442,7 @@ RZ_IPI bool rz_core_debug_reg_list(RzCore *core, int type, int size, PJ *pj, int
n++;
}
if (rad == 1 || rad == '*') {
dbg->cb_printf("fs-\n");
rz_cons_printf("fs-\n");
}
beach:
if (isJson) {
Expand Down Expand Up @@ -639,3 +639,245 @@ RZ_API RzCmdStatus rz_core_debug_plugins_print(RzCore *core, RzCmdStateOutput *s
rz_cmd_state_output_array_end(state);
return RZ_CMD_STATUS_OK;
}

/* Print out the JSON body for memory maps in the passed map region */
static void print_debug_map_json(RzDebugMap *map, PJ *pj) {
pj_o(pj);
if (map->name && *map->name) {
pj_ks(pj, "name", map->name);
}
if (map->file && *map->file) {
pj_ks(pj, "file", map->file);
}
pj_kn(pj, "addr", map->addr);
pj_kn(pj, "addr_end", map->addr_end);
pj_ks(pj, "type", map->user ? "u" : "s");
pj_ks(pj, "perm", rz_str_rwx_i(map->perm));
pj_end(pj);
}

/* Write a single memory map line to the console */
static void print_debug_map_line(RzDebug *dbg, RzDebugMap *map, ut64 addr, RzOutputMode mode) {
char humansz[8];
if (mode == RZ_OUTPUT_MODE_QUIET) { // "dmq"
char *name = (map->name && *map->name)
? rz_str_newf("%s.%s", map->name, rz_str_rwx_i(map->perm))
: rz_str_newf("%08" PFMT64x ".%s", map->addr, rz_str_rwx_i(map->perm));
rz_name_filter(name, 0, true);
rz_num_units(humansz, sizeof(humansz), map->addr_end - map->addr);
rz_cons_printf("0x%016" PFMT64x " - 0x%016" PFMT64x " %6s %5s %s\n",
map->addr,
map->addr_end,
humansz,
rz_str_rwx_i(map->perm),
name);
free(name);
} else {
const char *fmtstr = dbg->bits & RZ_SYS_BITS_64
? "0x%016" PFMT64x " - 0x%016" PFMT64x " %c %s %6s %c %s %s %s%s%s\n"
: "0x%08" PFMT64x " - 0x%08" PFMT64x " %c %s %6s %c %s %s %s%s%s\n";
const char *type = map->shared ? "sys" : "usr";
const char *flagname = dbg->corebind.getName
? dbg->corebind.getName(dbg->corebind.core, map->addr)
: NULL;
if (!flagname) {
flagname = "";
} else if (map->name) {
char *filtered_name = strdup(map->name);
rz_name_filter(filtered_name, 0, true);
if (!strncmp(flagname, "map.", 4) &&
!strcmp(flagname + 4, filtered_name)) {
flagname = "";
}
free(filtered_name);
}
rz_num_units(humansz, sizeof(humansz), map->size);
rz_cons_printf(fmtstr,
map->addr,
map->addr_end,
(addr >= map->addr && addr < map->addr_end) ? '*' : '-',
type,
humansz,
map->user ? 'u' : 's',
rz_str_rwx_i(map->perm),
map->name ? map->name : "?",
map->file ? map->file : "?",
*flagname ? " ; " : "",
flagname);
}
}

RZ_API void rz_debug_map_print(RzDebug *dbg, ut64 addr, RzCmdStateOutput *state) {
int i;
RzListIter *iter;
RzDebugMap *map;
PJ *pj = state->d.pj;
if (!dbg) {
return;
}
RzOutputMode mode = state->mode;
if (mode == RZ_OUTPUT_MODE_JSON) {
if (!pj) {
return;
}
pj_a(pj);
}

for (i = 0; i < 2; i++) { // Iterate over dbg::maps and dbg::maps_user
RzList *maps = rz_debug_map_list(dbg, (bool)i);
rz_list_foreach (maps, iter, map) {
switch (mode) {
case RZ_OUTPUT_MODE_JSON: // "dmj"
print_debug_map_json(map, pj);
break;
case RZ_OUTPUT_MODE_RIZIN: // "dm*"
{
char *name = (map->name && *map->name)
? rz_str_newf("%s.%s", map->name, rz_str_rwx_i(map->perm))
: rz_str_newf("%08" PFMT64x ".%s", map->addr, rz_str_rwx_i(map->perm));
rz_name_filter(name, 0, true);
rz_cons_printf("f map.%s 0x%08" PFMT64x " 0x%08" PFMT64x "\n",
name, map->addr_end - map->addr + 1, map->addr);
free(name);
} break;
case RZ_OUTPUT_MODE_QUIET: // "dmq"
MalhotraPulak marked this conversation as resolved.
Show resolved Hide resolved
print_debug_map_line(dbg, map, addr, mode);
break;
case RZ_OUTPUT_MODE_LONG: // workaround for '.'
if (addr >= map->addr && addr < map->addr_end) {
print_debug_map_line(dbg, map, addr, mode);
}
break;
default:
print_debug_map_line(dbg, map, addr, mode);
break;
}
}
}

if (pj) { // "dmj" add JSON closing array brace
pj_end(pj);
}
}

static int cmp(const void *a, const void *b) {
RzDebugMap *ma = (RzDebugMap *)a;
RzDebugMap *mb = (RzDebugMap *)b;
return ma->addr - mb->addr;
}

/**
* \brief Find the min and max addresses in an RzList of maps.
* \param maps RzList of maps that will be searched through
* \param min Pointer to a ut64 that the min will be stored in
* \param max Pointer to a ut64 that the max will be stored in
* \param skip How many maps to skip at the start of iteration
* \param width Divisor for the return value
* \return (max-min)/width
*
* Used to determine the min & max addresses of maps and
* scale the ascii bar to the width of the terminal
*/
static int findMinMax(RzList *maps, ut64 *min, ut64 *max, int skip, int width) {
RzDebugMap *map;
RzListIter *iter;
*min = UT64_MAX;
*max = 0;
rz_list_foreach (maps, iter, map) {
if (skip > 0) {
skip--;
continue;
}
if (map->addr < *min) {
*min = map->addr;
}
if (map->addr_end > *max) {
*max = map->addr_end;
}
}
return (int)(*max - *min) / width;
}

static void print_debug_maps_ascii_art(RzDebug *dbg, RzList *maps, ut64 addr, int colors) {
ut64 mul; // The amount of address space a single console column will represent in bar graph
ut64 min = -1, max = 0;
int width = rz_cons_get_size(NULL) - 90;
RzListIter *iter;
RzDebugMap *map;
RzConsPrintablePalette *pal = &rz_cons_singleton()->context->pal;
if (width < 1) {
width = 30;
}
rz_list_sort(maps, cmp);
mul = findMinMax(maps, &min, &max, 0, width);
ut64 last = min;
if (min != -1 && mul != 0) {
const char *color_prefix = ""; // Color escape code prefixed to string (address coloring)
const char *color_suffix = ""; // Color escape code appended to end of string
const char *fmtstr;
char humansz[8]; // Holds the human formatted size string [124K]
int skip = 0; // Number of maps to skip when re-calculating the minmax
rz_list_foreach (maps, iter, map) {
rz_num_units(humansz, sizeof(humansz), map->size); // Convert map size to human readable string
if (colors) {
color_suffix = Color_RESET;
if ((map->perm & 2) && (map->perm & 1)) { // Writable & Executable
color_prefix = pal->widget_sel;
} else if (map->perm & 2) { // Writable
color_prefix = pal->graph_false;
} else if (map->perm & 1) { // Executable
color_prefix = pal->graph_true;
} else {
color_prefix = "";
color_suffix = "";
}
} else {
color_prefix = "";
color_suffix = "";
}
if ((map->addr - last) > UT32_MAX) { // TODO: Comment what this is for
mul = findMinMax(maps, &min, &max, skip, width); // Recalculate minmax
}
skip++;
fmtstr = dbg->bits & RZ_SYS_BITS_64 // Prefix formatting string (before bar)
? "map %4.8s %c %s0x%016" PFMT64x "%s |"
: "map %4.8s %c %s0x%08" PFMT64x "%s |";
rz_cons_printf(fmtstr, humansz,
(addr >= map->addr &&
addr < map->addr_end)
? '*'
: '-',
color_prefix, map->addr, color_suffix); // * indicates map is within our current sought offset
int col;
for (col = 0; col < width; col++) { // Iterate over the available width/columns for bar graph
ut64 pos = min + (col * mul); // Current address space to check
ut64 npos = min + ((col + 1) * mul); // Next address space to check
if (map->addr < npos && map->addr_end > pos) {
rz_cons_printf("#"); // TODO: Comment what a # represents
} else {
rz_cons_printf("-");
}
}
fmtstr = dbg->bits & RZ_SYS_BITS_64 ? // Suffix formatting string (after bar)
"| %s0x%016" PFMT64x "%s %s %s\n"
: "| %s0x%08" PFMT64x "%s %s %s\n";
rz_cons_printf(fmtstr, color_prefix, map->addr_end, color_suffix,
rz_str_rwx_i(map->perm), map->name);
last = map->addr;
}
}
}

RZ_API void rz_debug_map_list_visual(RzDebug *dbg, ut64 addr, const char *input, int colors) {
if (!dbg) {
return;
}
int i;
for (i = 0; i < 2; i++) { // Iterate over dbg::maps and dbg::maps_user
RzList *maps = rz_debug_map_list(dbg, (bool)i);
if (!maps) {
continue;
}
print_debug_maps_ascii_art(dbg, maps, addr, colors);
}
}
1 change: 1 addition & 0 deletions librz/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static bool lastcmd_repeat(RzCore *core, int next);
#include "cmd_help.c"
#include "cmd_remote.c"
#include "cmd_tasks.c"
#include "cmd_linux_heap_glibc.c"

static const char *help_msg_dollar[] = {
"Usage:", "$alias[=cmd] [args...]", "Alias commands and strings (See ?$? for help on $variables)",
Expand Down
Loading