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 native debugger #4604

Merged
merged 5 commits into from
Sep 4, 2024
Merged

Conversation

tushar3q34
Copy link
Contributor

@tushar3q34 tushar3q34 commented Sep 2, 2024

SQUASH ME

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 documented or updated the documentation of every function and struct this PR changes. If not so I've explained why.
  • I've added tests that prove my fix is effective or that my feature works (if possible)
  • I've updated the rizin book with the relevant information (if needed)

Detailed description

Refactored debug_native.c into OS and arch specific files. Each file contains only the functions that are necessary and with minimal amounts of #ifdef .

Test plan

Closing issues

closes #4581

@tushar3q34
Copy link
Contributor Author

tushar3q34 commented Sep 2, 2024

For the build fail in AppVeyor, it runs Visual Studio 2017 and I think it does not support #warning preprocessor command. The error from logs is : fatal error C1021: invalid preprocessor command 'warning'. I think preprocessor command like pragma should work for outdated compilers. Should I change it or let it be ?

Copy link
Member

@wargio wargio left a comment

Choose a reason for hiding this comment

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

Awesome job! Please create a new folder called native and move these new files under it.
This is to ensure we know it is related to the native debugger.

Also change the uppercase names into lowercase (i.e. librz/debug/p/DragonFly.c to librz/debug/p/dragonfly.c

@wargio
Copy link
Member

wargio commented Sep 3, 2024

For the build fail in AppVeyor, it runs Visual Studio 2017 and I think it does not support #warning preprocessor command. The error from logs is : fatal error C1021: invalid preprocessor command 'warning'. I think preprocessor command like pragma should work for outdated compilers. Should I change it or let it be ?

We never had this issue before, so you probably have changed some code-paths.

Also librz/debug/p/debug_native.c needs to do #include "dragonfly.c" only if the OS where rizin is building is dragonfly, we cannot have all of these included.

We can also do the opposite and have a big #ifdef #endif at the beginning/end of each new .c files and just #include... them in the debug_native.c

@tushar3q34
Copy link
Contributor Author

tushar3q34 commented Sep 3, 2024

Awesome job! Please create a new folder called native and move these new files under it. This is to ensure we know it is related to the native debugger.

Also change the uppercase names into lowercase (i.e. librz/debug/p/DragonFly.c to librz/debug/p/dragonfly.c

Thanks. I went through the code and logs once and found the following issues :

  1. linux_x86_64.c file included native/reg.c file which had a function redefined causing errors.
  2. The __WINDOWS__ macro had a typo in one location which is probably why it did not build in AppVeyor.

I will go through everything once more and look for more errors. I will also implement the changes suggested by you and will make a new commit in a few hours.

@tushar3q34
Copy link
Contributor Author

Hello @wargio I corrected the formatting. Now its passing all except this one test case and my code passed for this particular test last time. I did not change any file that should affect native debugger.
Can you test the code on that test again ? Also I cannot see any errors in the console or log of the test so I'm not sure where to do the corrections if the code requires so.

@wargio
Copy link
Member

wargio commented Sep 3, 2024

@brightprogrammer FYI

@wargio
Copy link
Member

wargio commented Sep 3, 2024

Hello @wargio I corrected the formatting. Now its passing all except this one test case and my code passed for this particular test last time. I did not change any file that should affect native debugger. Can you test the code on that test again ? Also I cannot see any errors in the console or log of the test so I'm not sure where to do the corrections if the code requires so.

The error in the macos CI is unrelated to this PR. sometimes they fails. we have not yet discovered why.

Copy link
Member

@wargio wargio left a comment

Choose a reason for hiding this comment

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

LGTM

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.

I don't have any objections since it's purely code moving, but this code should be seriously (but carefully, very carefully) cleaned up.

// SPDX-License-Identifier: LGPL-3.0-only

#include <errno.h>
#if !defined(__HAIKU__) && !defined(__sun)
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit out of place, but could be done in a separate PR.

#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS 0x20
#endif
snprintf(code, sizeof(code),
Copy link
Member

Choose a reason for hiding this comment

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

We should definitely rewrite/fix this part. And it doesn't belong to debugger, IMHO.

Copy link
Member

Choose a reason for hiding this comment

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

the task was to split this, so we can refactor for each platform so we can be sure to avoid breaking stuff and remove code that should definitely not be there.

};
int num = rz_syscall_get_num(dbg->analysis->syscall, "munmap");

snprintf(code, sizeof(code),
Copy link
Member

Choose a reason for hiding this comment

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

Same

RZ_API RZ_OWN RzList /*<RzDebugPid *>*/ *rz_debug_native_threads(RzDebug *dbg, int pid) {
RzList *list = rz_list_new();
if (!list) {
eprintf("No list?\n");
Copy link
Member

Choose a reason for hiding this comment

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

Could be removed probably.

@XVilka
Copy link
Member

XVilka commented Sep 3, 2024

@tushar3q34 do you want to try this issue? #3072 - it would help cleaning up and refactoring the native debugger for BSD, since it will ensure there are no regressions and allow us test debugger automatically on these platforms.

@XVilka
Copy link
Member

XVilka commented Sep 3, 2024

Hmm, I remember it was fixed before, not sure why it happens again:

librz/util/meson.build:176:19: ERROR: Tried to tied to mix a host machine library ("softfloat") with a build machine target "rz_util_native" This is not possible in a cross build.

cc @DMaroo @wargio

@tushar3q34
Copy link
Contributor Author

@tushar3q34 do you want to try this issue? #3072 - it would help cleaning up and refactoring the native debugger for BSD, since it will ensure there are no regressions and allow us test debugger automatically on these platforms.

Yeah sure I will look into the issue and try my best to resolve it. As for this PR, what else am I supposed to do ?

@XVilka

This comment was marked as resolved.

@wargio

This comment was marked as resolved.

@DMaroo

This comment was marked as 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.

Good we waited for the Android buids to be fixed. This change breaks the build:

/usr/local/lib/android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android33-clang -Ilibrz/debug/librz_debug.a.p -I. -I.. -Ilibrz -I../librz -Ilibrz/include -I../librz/include -I../librz/bin/format/elf -I../librz/bin/format/dmp -I../librz/bin/format/mdmp -I../librz/bin/format/pe -I../subprojects/rzgdb/include -I../subprojects/rzgdb/include/gdbclient -I../subprojects/rzgdb/include/gdbserver -Isubprojects/rzwinkd -I../subprojects/rzwinkd -Ilibrz/util/sdb/src -I../librz/util/sdb/src -I../librz/bin/format -I../librz/arch/isa -I../librz/arch/isa_gnu -Ilibrz/arch -I../librz/arch -I../librz/type/parser -I../subprojects/rzqnx/include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O3 -DRZ_PLUGIN_INCORE=1 -DSUPPORTS_PCRE2_JIT -D_GNU_SOURCE --std=gnu99 -Werror=sizeof-pointer-memaccess -fPIC -MD -MQ librz/debug/librz_debug.a.p/p_debug_native.c.o -MF librz/debug/librz_debug.a.p/p_debug_native.c.o.d -o librz/debug/librz_debug.a.p/p_debug_native.c.o -c ../librz/debug/p/debug_native.c
In file included from ../librz/debug/p/debug_native.c:33:
In file included from ../librz/debug/p/native/android_x86_64.c:44:
../librz/debug/p/native/reg.c:7:14: error: redefinition of 'rz_debug_native_reg_profile'
    7 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
../librz/debug/p/native/android_x86_64.c:40:14: note: previous definition is here
   40 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
In file included from ../librz/debug/p/debug_native.c:33:
In file included from ../librz/debug/p/native/android_x86_64.c:44:
../librz/debug/p/native/reg.c:68:2: warning: Unsupported Kernel [-W#warnings]
   68 | #warning Unsupported Kernel
      |  ^
1 warning and 1 error generated.
[2247/2625] Compiling C object librz/debug/librz_debug.a.p/p_debug_gdb.c.o
[2248/2625] Compiling C object librz/debug/librz_debug.a.p/p_native_procfs.c.o
[2249/2625] Compiling C object librz/debug/librz_debug.a.p/p_debug_dmp.c.o
[2250/2625] Compiling C object librz/debug/librz_debug.a.p/p_native_linux_linux_debug.c.o
../librz/debug/p/native/linux/linux_debug.c:1183:2: warning: Android X86 does not support DRX [-W#warnings]
 1183 | #warning Android X86 does not support DRX
      |  ^
1 warning generated.
/usr/local/lib/android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi33-clang -Ilibrz/debug/librz_debug.a.p -I. -I.. -Ilibrz -I../librz -Ilibrz/include -I../librz/include -I../librz/bin/format/elf -I../librz/bin/format/dmp -I../librz/bin/format/mdmp -I../librz/bin/format/pe -I../subprojects/rzgdb/include -I../subprojects/rzgdb/include/gdbclient -I../subprojects/rzgdb/include/gdbserver -Isubprojects/rzwinkd -I../subprojects/rzwinkd -Ilibrz/util/sdb/src -I../librz/util/sdb/src -I../librz/bin/format -I../librz/arch/isa -I../librz/arch/isa_gnu -Ilibrz/arch -I../librz/arch -I../librz/type/parser -I../subprojects/rzqnx/include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O3 -DRZ_PLUGIN_INCORE=1 -DSUPPORTS_PCRE2_JIT -D_GNU_SOURCE --std=gnu99 -Werror=sizeof-pointer-memaccess -fPIC -MD -MQ librz/debug/librz_debug.a.p/p_debug_native.c.o -MF librz/debug/librz_debug.a.p/p_debug_native.c.o.d -o librz/debug/librz_debug.a.p/p_debug_native.c.o -c ../librz/debug/p/debug_native.c
In file included from ../librz/debug/p/debug_native.c:35:
In file included from ../librz/debug/p/native/android_arm.c:44:
../librz/debug/p/native/reg.c:7:14: error: redefinition of 'rz_debug_native_reg_profile'
    7 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
../librz/debug/p/native/android_arm.c:40:14: note: previous definition is here
   40 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
In file included from ../librz/debug/p/debug_native.c:35:
In file included from ../librz/debug/p/native/android_arm.c:44:
../librz/debug/p/native/reg.c:68:2: warning: Unsupported Kernel [-W#warnings]
   68 | #warning Unsupported Kernel
      |  ^
1 warning and 1 error generated.
[2250/2625] Compiling C object librz/debug/librz_debug.a.p/p_native_linux_linux_debug.c.o
../librz/debug/p/native/linux/linux_debug.c:1146:2: warning: print_fpu not implemented for this platform [-W#warnings]
 1146 | #warning print_fpu not implemented for this platform
      |  ^
../librz/debug/p/native/linux/linux_debug.c:1250:2: warning: getfpregs not implemented for this platform [-W#warnings]
 1250 | #warning getfpregs not implemented for this platform
      |  ^
../librz/debug/p/native/linux/linux_debug.c:1151:7: warning: variable 'showfpu' set but not used [-Wunused-but-set-variable]
 1151 |         bool showfpu = false;
      |              ^
../librz/debug/p/native/linux/linux_debug.c:1057:13: warning: unused function 'print_fpu' [-Wunused-function]
 1057 | static void print_fpu(void *f) {
      |             ^~~~~~~~~
4 warnings generated.
/usr/local/lib/android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang -Ilibrz/debug/librz_debug.a.p -I. -I.. -Ilibrz -I../librz -Ilibrz/include -I../librz/include -I../librz/bin/format/elf -I../librz/bin/format/dmp -I../librz/bin/format/mdmp -I../librz/bin/format/pe -I../subprojects/rzgdb/include -I../subprojects/rzgdb/include/gdbclient -I../subprojects/rzgdb/include/gdbserver -Isubprojects/rzwinkd -I../subprojects/rzwinkd -Ilibrz/util/sdb/src -I../librz/util/sdb/src -I../librz/bin/format -I../librz/arch/isa -I../librz/arch/isa_gnu -Ilibrz/arch -I../librz/arch -I../librz/type/parser -I../subprojects/rzqnx/include -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O3 -DRZ_PLUGIN_INCORE=1 -DSUPPORTS_PCRE2_JIT -D_GNU_SOURCE --std=gnu99 -Werror=sizeof-pointer-memaccess -fPIC -MD -MQ librz/debug/librz_debug.a.p/p_debug_native.c.o -MF librz/debug/librz_debug.a.p/p_debug_native.c.o.d -o librz/debug/librz_debug.a.p/p_debug_native.c.o -c ../librz/debug/p/debug_native.c
In file included from ../librz/debug/p/debug_native.c:37:
In file included from ../librz/debug/p/native/android_arm64.c:44:
../librz/debug/p/native/reg.c:7:14: error: redefinition of 'rz_debug_native_reg_profile'
    7 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
../librz/debug/p/native/android_arm64.c:40:14: note: previous definition is here
   40 | static char *rz_debug_native_reg_profile(RzDebug *dbg) {
      |              ^
In file included from ../librz/debug/p/debug_native.c:37:
In file included from ../librz/debug/p/native/android_arm64.c:44:
../librz/debug/p/native/reg.c:68:2: warning: Unsupported Kernel [-W#warnings]
   68 | #warning Unsupported Kernel
      |  ^
1 warning and 1 error generated.
[2247/2625] Compiling C object librz/debug/librz_debug.a.p/p_debug_qnx.c.o
[2248/2625] Compiling C object librz/debug/librz_debug.a.p/p_debug_dmp.c.o
[2249/2625] Compiling C object librz/debug/librz_debug.a.p/p_native_procfs.c.o
[2250/2625] Compiling C object librz/debug/librz_debug.a.p/p_debug_winkd.c.o
[2251/2625] Compiling C object librz/debug/librz_debug.a.p/p_native_linux_linux_debug.c.o
../librz/debug/p/native/linux/linux_debug.c:1146:2: warning: print_fpu not implemented for this platform [-W#warnings]
 1146 | #warning print_fpu not implemented for this platform
      |  ^
../librz/debug/p/native/linux/linux_debug.c:1250:2: warning: getfpregs not implemented for this platform [-W#warnings]
 1250 | #warning getfpregs not implemented for this platform
      |  ^
../librz/debug/p/native/linux/linux_debug.c:1151:7: warning: variable 'showfpu' set but not used [-Wunused-but-set-variable]
 1151 |         bool showfpu = false;
      |              ^
../librz/debug/p/native/linux/linux_debug.c:1057:13: warning: unused function 'print_fpu' [-Wunused-function]
 1057 | static void print_fpu(void *f) {
      |             ^~~~~~~~~
4 warnings generated.

@tushar3q34
Copy link
Contributor Author

tushar3q34 commented Sep 4, 2024

Yeah the same issue as linux builds. I will commit the changes asap.

@XVilka XVilka merged commit 1057cf6 into rizinorg:dev Sep 4, 2024
51 checks passed
@tushar3q34 tushar3q34 deleted the dist-refactor-native-debug branch September 12, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor native debugger
4 participants