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

Update smali debug .line info from JADX output for better debugging #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

e0x70i
Copy link

@e0x70i e0x70i commented Mar 18, 2023

When debugging decompiled source there are challenges and various annoyances due to discrepancies between the line numbers in the "debug_info_item" structures of the Dex files and those in the decompiled source code. This discrepancy arises because the process of decompilation usually does not produce an accurate representation of the original source code. These inconsistencies can make it difficult to map the source code to the bytecode, leading to misleading breakpoints, inaccurate step-by-step execution, and confusion in interpreting variable values and exception handling.

This PR adds a feature where the .line debug information in the smali files is updated to match the lines of JADX decompiled source code, making it easier to trace the execution flow of the application and understand its behavior. This significantly improves the debugging process when working with APKs where the original source code is not available.

This is enabled by the JADX '--add-debug-lines' flag which includes original source line information from the dex as comments in the decompiled code. This extension will parse these debug sourcelinie comments, map them to the line they appear in the decompiled source file, and apply these mappings by updatingthe associated smali files.

The core function of the fork is implemented in the updateSmaliDebugLines function, which is triggered from the context menu when right-clicking the apktool.yml file. The function starts by determining the project directory and the java decompiled sources directory. If the java source directory does not exist, the task aborts. Otherwise, the function proceeds to back up the original smali files, collect all source and smali files, and create mappings between them based on the line comments in the decompiled code from JADX. Finally, it iterates through the mappings and updates the smali file line information according to the JADX decompiled source code.

…rce line locations to enable working linebreak debugging in the rebuilt APK

fix issues identified by linter

fix formatting by running prettier
@Surendrajat
Copy link
Member

Thanks for the PR. This seems like a useful feature. But before I look at code more closely, can you tell me the debug process you're following, so I can test it out?

@Surendrajat Surendrajat changed the title Function to match smali debug .line information to JADX Decompiled sourcelines for better debugging of decompiled and rebuilt APKs Update smali debug .line info from JADX Decompiled sourcelines for better debugging Mar 19, 2023
@Surendrajat Surendrajat changed the title Update smali debug .line info from JADX Decompiled sourcelines for better debugging Update smali debug .line info from JADX output for better debugging Mar 19, 2023
@Surendrajat Surendrajat added feature New feature or request jadx Jadx related issues or PR debug labels Mar 19, 2023
@e0x70i
Copy link
Author

e0x70i commented Mar 28, 2023

Sure thing, here is a walkthrough of the flow:

  1. Run the open apk command and make sure "decompile_java" is checked:
    image
    . Currently this PR just adds the '--add-debug-lines' flag to the JADX args, may want to have a toggle for this since the line number comments may be undesirable to some.
  2. Once the APK has loaded and been decompiled, right click on apktool.yml and click "APKLab: Update Smali Lines from Decompiled Source"
    image. This will backup the original smalis, and perform the updates to line numbers as described above. This function does check if the source files exist, but does not check if the debug line comments are present, it assumes they are.
  3. Lastly, right click apktool.yml and rebuild the APK
    image

It is now ready to debug with aligned debug line information.

@e0x70i
Copy link
Author

e0x70i commented Mar 28, 2023

To debug the resulting APK:

  1. Open android studio and select "Profile or Debug APK"
    image
  2. Select the rebuilt APK exported from APKLab
    image
  3. Open one of the .smali files decompiled by Android Studio (NOT the APKLab smali files), in Android Studio and select "Attach Kotlin/Java Sources". These smali files will show up in the java (classes.dex) folder in the project section of android studio.
    image
    image
  4. Open the java_src directory that was generated by APKLab's decompiliation step
    image
  5. Set breakpoints as you see fit, and debug the app on the simulator or test phone
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debug feature New feature or request jadx Jadx related issues or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants