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

Publish GDB Plugin as a standard python package #14904

Merged
merged 32 commits into from
Nov 23, 2024

Conversation

XuNeo
Copy link
Contributor

@XuNeo XuNeo commented Nov 22, 2024

Note: Please adhere to Contributing Guidelines.

Summary

In this PR, I have included several useful small tools.

  1. deadlock same as device side deadlock detection.
  2. addr2line convert values to backtrace, similar to addr2line on host
  3. profile this tools is used to profiler python commands, so we can improve the performance of the tools itself.
  4. diagnose This tool automatically search available commands and generate a json report.
  5. dmesg: show the RAM log

Several minor bug fixes are also included here to minimize merge confections.

From this PR on, the nuttxgdb tool can be published as a standard python package.
We can now use py import nuttxgdb or source nuttx/tools/gdb/gdbinit.py which add nuttx/tools/gdb to path and import nuttxgdb.

Impact

__init__.py is replaced by gdbinit.py. __init__.py is used to mark a python package

Testing

Update this section with details on how did you verify the change,
what Host was used for build (OS, CPU, compiler, ..), what Target was
used for verification (arch, board:config, ..), etc. Providing build
and runtime logs from before and after change is highly appreciated.

Tested on qemu: lm3s6965-ek:qemu-flat

Config:

+CONFIG_DEBUG_SYMBOLS_LEVEL="-g3"
+CONFIG_RAMLOG=y
+CONFIG_RAMLOG_SYSLOG=y
+CONFIG_SYSLOG_DEVPATH="/dev/ttyS1"
+CONFIG_SYSLOG_MAX_CHANNELS=2

Run:

qemu-system-arm -semihosting \
        -M lm3s6965evb \
        -device loader,file=nuttx.bin,addr=0x00000000 \
        -netdev user,id=user0 \
        -nic user,id=user1 \
        -serial mon:stdio -nographic -s

deadlock use the same test case in #13899

(gdb) deadlock
Thread 6 "" has deadlocked!
  holders: 6->7->8->9
(gdb)

diag report will generate a json of some of the command results .

(gdb) diag report
Run command: Memleak
Run command: DeadLock
Run command: Dmesg
Write report to /home/neo/projects/nuttx/nuttx/nuttx.diagnostics.json
(gdb) shell cat /home/neo/projects/nuttx/nuttx/nuttx.diagnostics.json
[
    {
        "title": "Memory Leak Report",
        "command": "memleak",
        "details": "Need to set CONFIG_MM_BACKTRACE to 8 or 16 better.\n"
    },
    {
        "title": "Deadlock Report",
        "summary": "1 deadlocks",
        "command": "deadlock",
        "deadlocks": {
            "6": [
                "7",
                "8",
                "9"
            ]
        }
    },
    {
        "title": "RAM log",
        "summary": "Buffer not available",
        "result": "info",
        "command": "dmesg",
        "message": ""
    }
](gdb)

dmesg can be tested by generate some syslog on device by echo 123 > /dev/ttyS1
and verify on gdb:

(gdb) dmesg
123

(gdb)

XuNeo and others added 23 commits November 22, 2024 16:17
For LTO optimization, we may not be able to parse the macro value. Parse .debug_macro section from elf manually.

Signed-off-by: xuxingliang <[email protected]>
ta_argv is removed, we use pointer to the TLS instead

Signed-off-by: Gao Jiawei <[email protected]>
(gdb) info threads
  Id   Target Id         Frame
* 1.1  Thread 1 (Name: CPU0 IDLE, State: 3, Pri: 0, Stack: 41eccfec, Size: 3976)                       0x402cd586 in up_idle () at chip/r528_idle.c:80
  1.2  Thread 2 (Name: CPU1 IDLE, State: 4, Pri: 0, Stack: 4194bb78, Size: 3976)                       0x402cd586 in up_idle () at chip/r528_idle.c:80

(gdb) info nxthreads
Index Tid  Pid  Cpu  Thread                Info                                                                             Frame
 0    0    0    0 '\000' Thread 0x419633b8     (Name: CPU0 IDLE, State: Assigned, Priority: 0, Stack: 3976) 0x402cd586  up_idle() at chip/r528_idle.c:80
*1    1    1    1 '\001' Thread 0x41963498     (Name: CPU1 IDLE, State: Running, Priority: 0, Stack: 3976)  0x402cd586  up_idle() at chip/r528_idle.c:80
(gdb)

Signed-off-by: xuxingliang <[email protected]>
Some version of gdb does not support subprocess

Signed-off-by: xuxingliang <[email protected]>
Make sure elf exists before registering our commands. If no elf at the moment, register event to GDB to get notified when user adds the first object file.

Signed-off-by: xuxingliang <[email protected]>
Prebuilt arm-none-eabi-gdb may have not socket module available.

Signed-off-by: xuxingliang <[email protected]>
Usage: addr2line address1 address2 expression1
Example: addr2line 0x1234 0x5678
         addr2line "0x1234 + pointer->abc" &var var->field function_name var
         addr2line $pc $r1 "$r2 + var"
         addr2line [24/08/29 20:51:02] [CPU1] [209] [ap] sched_dumpstack: backtrace| 0: 0x402cd484 0x4028357e

Signed-off-by: xuxingliang <[email protected]>
After check version using inferior 2, need to switch back for normal operation

Signed-off-by: xuxingliang <[email protected]>
GDB uses remote-register to describe g/G packet. Whatever the target is,
we can always find out the register offset in packet by `maint print
remote-registers`.

In NuttX, we follow the same standard to prepare the packet, in both
coredump and GDB stub.

For example, the CPSR for arm-v7a is at register number of 25, byte
offset of 164. So we define the CPSR in g_reg_offs (164 /4 = 41)th
elementent.

If GDB stub supports qXfer feature, then GDB will ask stub for target
descriptor, which is a standard xml file. If this file is provided, then
the register order is also changed. It's also reflected in 'maint print
remote-registers' output. JLink GDB server supports it for example.

We always use manually added second inferiort to query the original
remote register layout. So it can match with tcbinfo.

Signed-off-by: xuxingliang <[email protected]>
Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@github-actions github-actions bot added the Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. label Nov 22, 2024
Now the GDB tool can be built with python -m build . to generate a
package.

Signed-off-by: xuxingliang <[email protected]>
Run diagnostic related commands anytime to generate report.

E.g `diag report -o systemreport.json`

Signed-off-by: xuxingliang <[email protected]>
XuNeo and others added 5 commits November 22, 2024 16:53
Now the dmesg output log in correct time order, from oldest to latest.
The NULL strings are also stripped, if the buffer is never get fully
filled.

Signed-off-by: xuxingliang <[email protected]>
@XuNeo XuNeo force-pushed the gdb-plugin-misc-fix-rename branch from a461f84 to bea511e Compare November 22, 2024 08:55
Copy link

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

@xiaoxiang781216 xiaoxiang781216 merged commit 1485ecd into apache:master Nov 23, 2024
27 checks passed
@XuNeo XuNeo deleted the gdb-plugin-misc-fix-rename branch November 23, 2024 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Tooling Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants