Commit graph

156 commits

Author SHA1 Message Date
xuxingliang
08139fb1db nxgdb/thread: fix ps command
Fix ps command reports wrong running stack corruption.
To get running tcb registers, need to read from frame register not the tcb context.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-12-02 13:43:52 +08:00
xuxingliang
1d55f111f3 gdb: check if pid is valid
Add check to make sure pid is valid.
Remove stack check for interrupt context.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-12-02 13:43:52 +08:00
xuxingliang
1f2435641d nuttxgdb: fix memdump busyloop when nodesize is 0
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-12-02 13:43:52 +08:00
xuxingliang
659e3f87a0 gdb/mmleak: add diagnose API to report details of the leak
Example of json dump:

    {
        "title": "Memory Leak Report",
        "summary": "Total 105 blks, 22768 bytes leaked",
        "result": "fail",
        "command": "mm leak",
        "data": [
            {
                "count": 1,
                "pid": 0,
                "size": 48,
                "address": 1100447608,
                "seqno": 0,
                "alive": true,
                "backtrace": []
            },
            {
                "count": 1,
                "pid": 4,
                "size": 368,
                "address": 1100549056,
                "seqno": 1041,
                "alive": false,
                "backtrace": [
                    {
                        "address": 1076428472,
                        "function": "<mm_zalloc+8>",
                        "source": "mm_heap/mm_zalloc.c:45"
                    },
                    {
                        "address": 1076424018,
                        "function": "<mm_calloc+18>",
                        "source": "mm_heap/mm_calloc.c:57"
                    },
                    {
                        "address": 1076419750,
                        "function": "<calloc+22>",
                        "source": "umm_heap/umm_calloc.c:73"
                    },
                    ....
                 ]
            },
  }

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-12-02 13:43:52 +08:00
Zhe Weng
3ad53656b9 tools/gdb: Fix gdbinit to unload all nuttxgdb related modules
There are many modules like nuttxgdb.* that we need to unload

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2024-12-02 13:43:52 +08:00
yangao1
968bb179fe nuttxgdb/rpmsg.py:dump rpmsg_service message
(gdb)rpmsg_service
g_rpmsg_cb:
rpmsg_cb_s at        ns_match                                 ns_bind
-------------        --------                                 -------
0xf4e00ac0           0x44708225 <rpmsg_rtc_server_ns_match>   0x4470874c <rpmsg_rtc_server_ns_bind>
0xf4e27b20           0x440c8fd6 <syslog_rpmsg_ns_match>       0x440c966a <syslog_rpmsg_ns_bind>
0xf4e27af0           0x0                                      0x0
g_rpmsg:
Endpoint at          Name                 local Addr   dest Addr    cb                                       ns_bound_cb                              ns_unbind_cb
-----------          ----                 ----------   ---------    --                                       -----------                              ------------
0xf2302ac0           NS                   53           53           0x44699520 <rpmsg_virtio_ns_callback>    0x0                                      0x0
0xf3c0f920           rpmsg-ttysensor      1025         1028         0x44704457 <uart_rpmsg_ept_cb>           0x0                                      0x0
0xf301d058           rpmsg-sensor         1026         1029         0x440c1680 <sensor_rpmsg_ept_cb>         0x440c1871 <sensor_rpmsg_device_ns_bound> 0x44693a18 <rpmsg_destroy_ept>
0xf2302a6c           rpmsg-ping           1027         1036         0x440a10d2 <rpmsg_ping_ept_cb>           0x0                                      0x0

List update: rpmsg dump contains none initialized list, need to take care of it in
NxList.

Signed-off-by: yangao1 <yangao1@xiaomi.com>
2024-11-28 20:40:04 +08:00
yinshengkai
e69903c939 tools/gdb: Add a singleton class for macros
Usage:
    macro = Macro("nuttx/nuttx")
    print(macro.CONFIG_MM_BACKTRACE)
    if macro.CONFIG_MM_BACKTRACE:
        print("mm backtrace is enabled")
    else:
        print("mm backtrace is disabled")

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-11-25 22:58:53 +08:00
yinshengkai
9eaec7ba49 tools/gdb: move the macros cache files to a temporary directory
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-11-25 22:58:53 +08:00
yinshengkai
7aa2dc24cd tools/gdb: fix checkpatch warning
Use Python raw strings to avoid escaping:
SyntaxWarning: invalid escape sequence '\?'

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-11-25 22:58:53 +08:00
xuxingliang
8031c9c1de gdb/mm: cache global variables to save time of memleak check
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:42 +08:00
xuxingliang
a0e9b82a79 tools/gdb: always use name to read register
For unknown reason, for some cases, GDB refuses to read register using descriptor. Let's stick to the register name.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:42 +08:00
xuxingliang
9881fbae28 tools/gdb: fix thread command not working
`thread` is a GDB prefix command. Use `define` can only change it to a
user prefix command. In this case, `thread 3` is unable to pass
the argument 3 to python.

Use python code to register command to overwrite this behavior. It may
not work with future GDB, but all is good for now.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:42 +08:00
xuxingliang
efb890abd3 gdb/diagnose: use unified message for command error
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:42 +08:00
xuxingliang
5b477aacca gdb/diag: catch gdb.error in diagnositics
gdb.error could report if memory is corrupted. Save and report the exception and continue to next command.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:42 +08:00
xuxingliang
191dbcc4f1 gdb/debug: add debugpy to debug python code
Usage:
(gdb) debugpy
Waiting for connection at localhost:5678

In VSCode, add new configuration in launch.
{
  "name": "Python Debugger: Remote Attach",
  "type": "debugpy",
  "request": "attach",
  "connect": {
    "host": "localhost",
    "port": 5678
  }
}

and launch.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:30 +08:00
xuxingliang
6119a5ea63 gdb: print trace back when some modules failed to load
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:19 +08:00
yangao1
400790d40b tools/gdb/gdbinit.py:add hint
Signed-off-by: yangao1 <yangao1@xiaomi.com>
2024-11-25 13:37:19 +08:00
xuxingliang
db9b5c4cee tools/gdb: register commands gracefully
Report any error and continue to try to register next command.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:19 +08:00
xuxingliang
65a93e972c tools/gdb: guide user for correct usage of nuttxgdb
Should use gdbinit.py instead of package __init__.py. Report the correct usage if user try to source the package directly.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 13:37:19 +08:00
xuxingliang
358261af19 tools/gdb: avoid direct access to tcb['name']
It could be disabled at compile time. Use utils.get_task_name to handle it

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
41c4eb08c0 gdb/mm: fix prev free judging and region search
1. The prev free flag should use field size instead of preceding to compare.
2. If not prev-free, then prevnode should be None.
3. Cast type to mm_freenode_s in order to access flink, blink when node is free.
4. Heap itself is also included in first region.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
8667f911bc gdb/mm: fix memdump when mempool backtrace is disabled
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
ee9673f62e gdb/memdump: add option to exclude nodes from specific PID
This is useful to exclude memory nodes for mempool.

E.g.

(gdb) mm dump --nob --top 5 --sort size --nop --no-pid -1
  Pool          CNT          PID         Size  Overhead          Seqno            Address Backtrace
     H            1            0     16777264        44             13         0x41a79010
     H            2          160      1536048        44     1129827490         0x41d6aad0
     H            1        45374      1536176        44     1129829080         0x45ef1010
     H            3          141       408440        44         145817         0x43905bf0
     H            7          158       131120        44          32955         0x4308d090
Total 14 blks, 23528696 bytes
(gdb) mm dump --nob --top 5 --sort size --nop
  Pool          CNT          PID         Size  Overhead          Seqno            Address Backtrace
     H            1            0     16777264        44             13         0x41a79010
*    H            6           -1      1048640        44         116954         0x436a0fd0
*    H            5           -1      1048640        44         146821         0x43b24fd0
*    H            3           -1      1048640        44          66161         0x4335bfd0
*    H            3           -1      1048640        44         156337         0x43d3ffd0
Total 18 blks, 16777264 bytes

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
f849092ede gdb/memdump: add option to parse memdump log from device
igned-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
06a696076a gdb/memdump: reorganize the memdump parameters
Make the dump_nodes etc more common to use.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
6d2ebfdec6 mm: dump node overheadp during memdump
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
797f7a043d gdb/memdump: show prev and next node in address finding mode
This will make it easier to debug memory corruption when agacent node is corrupted.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
dd7203aff7 gdb/mm: move memleak to standalone file
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
Xu Xingliang
412644fcf2 gdb: optimize memory commands
Tested on a complex project, results are promising.

Command                 Time cost(s)           Time saving(s)   Peformance Boost
                        Before      After
memleak                 39.494172   22.366415   17.127757       1.8
memdump                 41.872441   26.458386   15.414055       1.6
memdump -a 0x1234       28.116294   1.114119    27.002175       25.2
memdump --no-backtrace    N/A       1.114119
memmap                  7.973809    6.836468    1.137341        1.2

Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
2024-11-25 00:42:34 +08:00
xuxingliang
9f9cc7eceb gdb/fs: use Value and Protocol for easy access and hinting
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 17:52:42 +08:00
xuxingliang
a59a28de5b gdb/utils: enhance utils.Value
Added __format__ method to support format spec like {:>10} that gdb.Value doesn't support. For such case, gdb.Value is converted to python value firstly and then format natively.
Override all methods/attributes could return gdb.Value to return utils.Value instead.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 17:52:42 +08:00
xuxingliang
03a38fa633 gdb/fs: handle special inode of epoll, eventfd etc.
Those inodes have no name, handle them so the path can correctly show.
Add FILEP output so we can investigate furthure manually.
Change OFLAGS to hex output and increase word space.

E.g.
(gdb) fdinfo -p 120
PID: 120
FD  FILEP       OFLAGS      POS       PATH                                            BACKTRACE
0   0x40807f08  0x3         0         /dev/console
1   0x40807f30  0x3         0         /dev/console
2   0x40807f58  0x3         0         /dev/console
3   0x40807f80  0x400       0         epoll
4   0x40807fa8  0x443       0         eventfd
5   0x40807fd0  0x403       0         sock
6   0x40807ff8  0x403       0         sock
7   0x40808020  0x403       0         sock
8   0x40892520  0x403       0         sock
9   0x40892548  0x403       0         sock
10  0x40892570  0x403       0         sock
11  0x40892598  0x403       0         sock
12  0x408925c0  0x403       0         sock
13  0x408925e8  0x403       0         sock
14  0x40892610  0x403       0         sock
15  0x40892638  0x403       0         sock
16  0x40942a20  0x403       0         sock

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 17:52:42 +08:00
xuxingliang
5b43a8cb9b gdb/fs: use python iterator instead of C style callbacks
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 17:52:42 +08:00
yangao1
502377034a nuttxgdb/fs.py:add diagnose api
Signed-off-by: yangao1 <yangao1@xiaomi.com>
2024-11-24 17:52:42 +08:00
xuxingliang
c013648617 tools/gdb: improve nxgcore speed
Let GDB to read from ELF for readonly data.
Disable this feature by nxgcore --no-trust-readonly.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
3a2d4584c7 gdb/utils: cache backtrace result for better performance
GDB is slow to look up symbols, cache the result to speed up commands like memdump etc.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
f5371e92f3 gdb/backtrace: optimize backtrace formatting
Use a class Bactrace to support convert address to backtrace, format to string, accessing element and iterate though them.
Adjust usage in fs and addr2line to make full use of it.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
38b5dcd42c gdb/register: register name sp, pc are always available
GDB provides four “standard” register names sp, pc, fp and ps. Those can be used in most of the cases.

See https://sourceware.org/gdb/current/onlinedocs/gdb.html/Registers.html

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
73467989c5 gdb: move profiling commands to profile.py
Add simple time command to test time cost of a command.
Usage:
(gdb) time memleak
...
(gdb) Time elapsed: 1.23456s

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
Xu Xingliang
6949ff6a7d gdb: add sizeof helper
Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
84c66505aa tools/gdb: add function to convert C enum to python Enum class
Usage:
(gdb) py NX_INITSTATE = utils.enum("enum nx_initstate_e")
(gdb) py print(list(NX_INITSTATE))
[<NX_INITSTATE_E.POWERUP: 0>, <NX_INITSTATE_E.BOOT: 1>, <NX_INITSTATE_E.TASKLISTS: 2>, <NX_INITSTATE_E.MEMORY: 3>, <NX_INITSTATE_E.HARDWARE: 4>, <NX_INITSTATE_E.OSREADY: 5>, <NX_INITSTATE_E.IDLELOOP: 6>, <NX_INITSTATE_E.PANIC: 7>]
(gdb) py print(NX_INITSTATE.POWERUP)
NX_INITSTATE_E.POWERUP
(gdb) py print(NX_INITSTATE.POWERUP.value)
0
(gdb) py print(NX_INITSTATE(1))

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
379eee2f4e tools/gdb: fix hexdump expression parse
Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
yangao1
40efe1af11 nuttxgdb/utils.py:add ArrayIterator reverse iterate.
Signed-off-by: yangao1 <yangao1@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
e3b3da3bd1 tools/gdb: support string type in offset_of
1. Support any type of value as pointer address in container_of
2. Support string as type in offset_of
3. Make sure type is a pointer in container_of, and not a pointer in
   offset_of

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
d02dc3893c tools/gdb: decode backtrace of crashlog
Now crash log can be directly pass to addr2line tool to get all backtraces.

E.g.
(gdb) addr2line -f crash.log -p 485
Address              Symbol                           Source

Backtrace of 485
0x402cc0ac           <up_switch_context+84>           /home/work/ssd1/workspace/MiRTOS-X4b-Stable-Build/nuttx/include/arch/syscall.h:179
0x40291276           <nxsig_timedwait+386>            signal/sig_timedwait.c:365
0x4028fc7e           <nxsig_nanosleep+106>            signal/sig_nanosleep.c:141
0x4028fdba           <clock_nanosleep+26>             signal/sig_nanosleep.c:333
0x402c3736           <usleep+62>                      unistd/lib_usleep.c:108
0x415018c0           <cs2p2p_mSecSleep+24>            Src/PPPP_Common.c:1139
0x414fabde           <cs2p2p_Run_send_DRW+258>        Src/PPPP_API.c:5577
0x414fac62           <cs2p2p_PPPP_thread_send_DRW+18> Src/PPPP_API.c:5616
0x40446f62           <pthread_startup+10>             pthread/pthread_create.c:59
0x41094f4a           <pthread_start+106>              pthread/pthread_create.c:139

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
huangyin5
1e265af8eb tools/gdb: add utils.get_tid(tcb)
Signed-off-by: huangyin5 <huangyin5@xiaomi.com>
2024-11-24 03:03:40 +08:00
xuxingliang
69613d5199 tools/gdb: add array iterator
Use array iterator where possible.

Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
2024-11-24 03:03:40 +08:00
Zhe Weng
0cda6278d3 tools/gdb: Use ptr's value instead of ptr's adderess in container_of
We're using `ptr.cast(get_long_type())` a week ago to get the pointer's value, it's alright, but `ptr.address` is not, the `ptr.address` will return **the address of the pointer**.

These values are the address of first element in the queue:
    - `int(g_sigfreeaction["head"])`
    - `g_sigfreeaction["head"].cast(get_long_type())`
    - `g_sigfreeaction["head"].dereference().address`
But:
    `int(g_sigfreeaction["head"].address)` is the address of the "head" member, which equals to the address of `g_sigfreeaction`

It's happening in NxSQueue:
    g_sigfreeaction = gdb.parse_and_eval("g_sigfreeaction")
    print(["%x" % (node) for node in NxSQueue(g_sigfreeaction)])
    print(["%x" % (node) for node in NxSQueue(g_sigfreeaction, "sigactq_t", "flink")])
Without this patch:
    ['f3c0aa10', 'f3c0aa2c', 'f3c0aa48']
    ['55db90a0', 'f3c0aa10', 'f3c0aa2c']
With this patch:
    ['f3c0aa10', 'f3c0aa2c', 'f3c0aa48']
    ['f3c0aa10', 'f3c0aa2c', 'f3c0aa48']

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2024-11-24 03:03:40 +08:00
Zhe Weng
a4e5689c7f tools/gdb: Allow utils.container_of with str input
After we introduced NxDQueue and NxSQueue, we're using them like `NxDQueue(g_active_connections, "struct socket_conn_s", "node")` and leads to `utils.container_of(ptr, str, str)`, so maybe we need to allow str input for `utils.container_of`

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2024-11-24 03:03:40 +08:00
buxiasen
bb461b532b nuttxgdb: fix container_of pointer offset calc problem
Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-11-24 03:03:40 +08:00