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

Refactor dm #1182

merged 86 commits into from
Jun 17, 2021

Conversation

MalhotraPulak
Copy link
Member

@MalhotraPulak MalhotraPulak commented Jun 1, 2021

Your checklist for this pull request

  • I've read the guidelines for contributing to this repository
  • I made sure to follow the project's coding style
  • I've added tests that prove my fix is effective or that my feature works (if possible)
  • I've updated the documentation and the rizin book with the relevant information (if needed)

Detailed description
Plan:

  • Refactor dm to use RZ_API
  • Refactor dmh to use RZ_API
  • Make heap viewer in Cutter using the refactored commands

Currently:

  • Add more tests for the commands
  • Expose API to cutter

Test plan

CI is green

...

Closing issues

...

@MalhotraPulak MalhotraPulak changed the title Refactor dm [WIP] Refactor dm Jun 1, 2021
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
@XVilka
Copy link
Member

XVilka commented Jun 1, 2021

I suggest to port that JSON PR on top of this one once it's finished.

@XVilka XVilka requested review from ret2libc and yossizap June 1, 2021 11:47
@XVilka XVilka added shell refactor Refactoring requests labels Jun 1, 2021
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
@MalhotraPulak
Copy link
Member Author

MalhotraPulak commented Jun 2, 2021

@XVilka I found dmq and dmq. command in the codebase which were not mentioned in the help menu. I implemented that in the new shell. Can you tell what summary should I write for them?
Also, in the help menu there are dms and dms- command mentioned but not implemented anywhere. Should I add them to new shell as commands that do nothing or leave them?

@XVilka
Copy link
Member

XVilka commented Jun 2, 2021

@PulakIIIT for

  • dms - remove them
  • dmq means quiet. Judging by the code it's the same as dm but with less verbose information. Just add RZ_OUTPUT_MODE_QUIET in dm YAML and call it from here.

Moreover, I checked the code of rz_debug_map_list(). It is wrong nowadays because:

  • Generally, when we have function ending in _list() it means it returns list of something, e.g. as RzList *. When the function prints something, we call it _print() instead.
  • Generally, we prefer to avoid RzCons usage or printing outside of the librz/core.

Thus, I suggest to do the following:

  • Add function rz_debug_map_print() in librz/core/cdebug.c
  • Make function rz_debug_map_list() return RzList *
  • Move all print helpers of old rz_debug_map_list() together with it into librz/core/cdebug.c
  • Move rz_debug_map_list_visual() with helpers also into librz/core/cdebug.c
  • Also I suggest to use RzOutputMode instead of char in that funciton like described in Refactor output modes to use enum #489

@MalhotraPulak
Copy link
Member Author

MalhotraPulak commented Jun 2, 2021

@XVilka I have moved the function rz_debug_map_list and related functions from dmap.c to librz/core/cdebug.c and renamed rz_debug_map_list to rz_debug_map_print since it mostly focuses on printing. I also refactored rz_debug_map_print to use RzOutputMode. Since I renamed that function at the moment there is no function rz_debug_map_list which returns an RzList * as you mentioned in your first point. Is it necessary to have rz_debug_map_list in the codebase?

librz/core/cdebug.c Outdated Show resolved Hide resolved
librz/core/cdebug.c Outdated Show resolved Hide resolved
@XVilka
Copy link
Member

XVilka commented Jun 3, 2021

Since I renamed that function at the moment there is no function rz_debug_map_list which returns an RzList * as you mentioned in your first point. Is it necessary to have rz_debug_map_list in the codebase?

Please add, you will need it for Cutter anyway.

librz/core/cdebug.c Outdated Show resolved Hide resolved
@XVilka XVilka added this to the 0.3.0 milestone Jun 4, 2021
@MalhotraPulak MalhotraPulak marked this pull request as ready for review June 10, 2021 06:28
@XVilka
Copy link
Member

XVilka commented Jun 10, 2021

Please also rebase on top of the latest dev to resolve the conflicts.

librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Show resolved Hide resolved
librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_heap_glibc.yaml Show resolved Hide resolved
librz/include/rz_core.h Show resolved Hide resolved
Copy link
Member

@XVilka XVilka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, there are some minor changes required though before being ready to merge.

librz/core/cdebug.c Show resolved Hide resolved
librz/core/cdebug.c Outdated Show resolved Hide resolved
librz/core/cdebug.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
/* TODO: do not spawn. use RzBin API */
if (sectname) {
char *sect = rz_str_escape(sectname);
res = rz_sys_cmd_strf("env RZ_BIN_PREFIX=\"%s\" rz-bin %s-B 0x%08" PFMT64x " -S \"%s\" | grep \"%s\"", name, mode, baddr, filesc, sect);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thestr4ng3r could you please suggest a proper API as a substitute for this command?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not ok.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we can change this in another PR

librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/core/linux_heap_glibc.c Outdated Show resolved Hide resolved
librz/debug/dmap.c Outdated Show resolved Hide resolved
librz/include/rz_core.h Show resolved Hide resolved
librz/include/rz_heap_glibc.h Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
Copy link
Member

@XVilka XVilka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, except if there is a quick API to substitute that rz-bin spawn. Lets see what @ret2libc and @yossizap think.

@XVilka XVilka requested a review from ret2libc June 16, 2021 09:55
librz/core/cmd_descs/cmd_debug.yaml Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Outdated Show resolved Hide resolved
librz/core/cmd_descs/cmd_debug.yaml Show resolved Hide resolved
librz/core/linux_heap_glibc.c Show resolved Hide resolved
Copy link
Member

@XVilka XVilka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still "WIP"? Looks ready to me.

@MalhotraPulak MalhotraPulak changed the title [WIP] Refactor dm Refactor dm Jun 16, 2021
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
librz/core/cmd_debug.c Outdated Show resolved Hide resolved
@@ -1443,376 +1357,462 @@ static bool get_bin_info(RzCore *core, const char *file, ut64 baseaddr, PJ *pj,
return true;
}

static int cmd_debug_map(RzCore *core, const char *input) {
// dm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to remove these comments... They will likely become wrong at some point. I'd prefer to focus on #691 to correctly find the right handler.

librz/core/cmd_debug.c Outdated Show resolved Hide resolved
rz_debug_map_sync(core->dbg); // update process memory maps
RzList *list = rz_debug_modules_list(core->dbg);
rz_list_foreach (list, iter, map) {
if ((!libname ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very very long if. Not sure if it comes from old code, but a separate function with a meaningful name or something else would be better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this if comes from the old code indeed

librz/core/cmd_debug.c Outdated Show resolved Hide resolved
@ret2libc
Copy link
Member

Overall looks good anyway! Great work ;)

@XVilka XVilka requested a review from ret2libc June 17, 2021 09:15
Copy link
Member

@ret2libc ret2libc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me, but since you are touching a lot of debug code (which is not tested too much) please do a round of manual testing with a debugging session. I did it already a couple of versions ago, but better be sure ;)

Copy link
Member

@yossizap yossizap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit late for this review so I'll just leave the comment regarding the API for a future PR. LGTM other than that.

RZ_API RzList *rz_heap_heap_chunks_list_32(RzCore *core, MallocState *main_arena, ut32 m_arena, ut32 m_state);
RZ_API bool rz_heap_resolve_main_arena_32(RzCore *core, ut32 *m_arena);
RZ_API bool rz_heap_update_main_arena_32(RzCore *core, ut32 m_arena, MallocState *main_arena);
RZ_API RzList *rz_heap_tcache_list_32(RzCore *core, ut32 m_arena, MallocState *main_arena, bool main_thread_only);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are planning to use the API here for Cutter's HeapWidget frontend the _32/_64 variations in the API should be made internal and replaced with wrapper functions that will use a bool/bitness var to differentiate architectures.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PulakIIIT I think this can be done in a separate PR. Let's merge this one.

@XVilka XVilka merged commit 6fabb6d into dev Jun 17, 2021
@XVilka XVilka deleted the refactor-dm branch June 17, 2021 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

5 participants