weargdb 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- weargdb-0.0.1/.editorconfig +24 -0
- weargdb-0.0.1/.github/workflows/ci.yml +57 -0
- weargdb-0.0.1/.gitignore +40 -0
- weargdb-0.0.1/LICENSE +202 -0
- weargdb-0.0.1/PKG-INFO +312 -0
- weargdb-0.0.1/README.md +287 -0
- weargdb-0.0.1/pyproject.toml +52 -0
- weargdb-0.0.1/src/weargdb/__init__.py +32 -0
- weargdb-0.0.1/src/weargdb/commands.py +201 -0
- weargdb-0.0.1/tests/test_hexdump.py +57 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright (c) 2026 Junbo Zheng
|
|
3
|
+
|
|
4
|
+
root = true
|
|
5
|
+
|
|
6
|
+
[*]
|
|
7
|
+
charset = utf-8
|
|
8
|
+
end_of_line = lf
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
trim_trailing_whitespace = true
|
|
11
|
+
indent_style = space
|
|
12
|
+
indent_size = 4
|
|
13
|
+
|
|
14
|
+
[*.{md,markdown}]
|
|
15
|
+
trim_trailing_whitespace = false
|
|
16
|
+
|
|
17
|
+
[Makefile]
|
|
18
|
+
indent_style = tab
|
|
19
|
+
|
|
20
|
+
[*.{yml,yaml}]
|
|
21
|
+
indent_size = 2
|
|
22
|
+
|
|
23
|
+
[*.{json,toml}]
|
|
24
|
+
indent_size = 2
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
|
12
|
+
|
|
13
|
+
permissions:
|
|
14
|
+
contents: read
|
|
15
|
+
|
|
16
|
+
jobs:
|
|
17
|
+
lint:
|
|
18
|
+
runs-on: ubuntu-latest
|
|
19
|
+
timeout-minutes: 10
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
- uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: '3.12'
|
|
25
|
+
cache: pip
|
|
26
|
+
- run: pip install black
|
|
27
|
+
- run: black --check .
|
|
28
|
+
|
|
29
|
+
test:
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
timeout-minutes: 20
|
|
32
|
+
strategy:
|
|
33
|
+
fail-fast: false
|
|
34
|
+
matrix:
|
|
35
|
+
python-version: ['3.10', '3.11', '3.12']
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/checkout@v4
|
|
38
|
+
- uses: actions/setup-python@v5
|
|
39
|
+
with:
|
|
40
|
+
python-version: ${{ matrix.python-version }}
|
|
41
|
+
cache: pip
|
|
42
|
+
cache-dependency-path: pyproject.toml
|
|
43
|
+
- run: pip install -e '.[dev]'
|
|
44
|
+
- run: pytest -q
|
|
45
|
+
|
|
46
|
+
build:
|
|
47
|
+
runs-on: ubuntu-latest
|
|
48
|
+
timeout-minutes: 10
|
|
49
|
+
steps:
|
|
50
|
+
- uses: actions/checkout@v4
|
|
51
|
+
- uses: actions/setup-python@v5
|
|
52
|
+
with:
|
|
53
|
+
python-version: '3.12'
|
|
54
|
+
cache: pip
|
|
55
|
+
- run: pip install build twine
|
|
56
|
+
- run: python -m build
|
|
57
|
+
- run: python -m twine check dist/*
|
weargdb-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Byte-compiled / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Virtual envs
|
|
7
|
+
.venv/
|
|
8
|
+
venv/
|
|
9
|
+
env/
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
build/
|
|
13
|
+
dist/
|
|
14
|
+
*.egg-info/
|
|
15
|
+
*.egg
|
|
16
|
+
|
|
17
|
+
# Test / coverage / type-check caches
|
|
18
|
+
.pytest_cache/
|
|
19
|
+
.mypy_cache/
|
|
20
|
+
.ruff_cache/
|
|
21
|
+
.coverage
|
|
22
|
+
coverage.xml
|
|
23
|
+
htmlcov/
|
|
24
|
+
|
|
25
|
+
# Editor / IDE
|
|
26
|
+
.vscode/
|
|
27
|
+
.idea/
|
|
28
|
+
*.swp
|
|
29
|
+
*.swo
|
|
30
|
+
*~
|
|
31
|
+
|
|
32
|
+
# OS noise
|
|
33
|
+
.DS_Store
|
|
34
|
+
Thumbs.db
|
|
35
|
+
|
|
36
|
+
# Secrets — never commit
|
|
37
|
+
.env
|
|
38
|
+
credentials.json
|
|
39
|
+
*.pem
|
|
40
|
+
*.key
|
weargdb-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
weargdb-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: weargdb
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A pip-installable GDB extension that registers custom GDB commands
|
|
5
|
+
Project-URL: Homepage, https://github.com/Junbo-Zheng/weargdb
|
|
6
|
+
Project-URL: Issues, https://github.com/Junbo-Zheng/weargdb/issues
|
|
7
|
+
Author-email: Junbo Zheng <3273070@qq.com>
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: debugging,gdb,gdb-extension
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: black; extra == 'dev'
|
|
21
|
+
Requires-Dist: build; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
23
|
+
Requires-Dist: twine; extra == 'dev'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
<!-- SPDX-License-Identifier: Apache-2.0 -->
|
|
27
|
+
<!-- Copyright (c) 2026 Junbo Zheng -->
|
|
28
|
+
|
|
29
|
+
<div align="center">
|
|
30
|
+
|
|
31
|
+
# weargdb
|
|
32
|
+
|
|
33
|
+
_A pip-installable GDB extension that registers custom GDB commands for embedded debugging._
|
|
34
|
+
|
|
35
|
+
[](LICENSE)
|
|
36
|
+
[](pyproject.toml)
|
|
37
|
+
|
|
38
|
+
[How it works](#how-it-works) • [Install](#installation) • [Commands](#commands) • [Usage](#usage) • [Extend](#adding-your-own-command) • [Troubleshooting](#troubleshooting)
|
|
39
|
+
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
After installation, a single `py import weargdb` inside GDB makes every bundled
|
|
43
|
+
command available at the `(gdb)` prompt. The commands read symbols, sections,
|
|
44
|
+
and memory straight out of the loaded ELF or coredump using GDB's Python API —
|
|
45
|
+
handy for inspecting NuttX/Vela firmware crash dumps, but tied to nothing
|
|
46
|
+
specific, so they work against any ELF.
|
|
47
|
+
|
|
48
|
+
## How it works
|
|
49
|
+
|
|
50
|
+
GDB ships with an **embedded Python interpreter** and exposes a built-in `gdb`
|
|
51
|
+
module to it. A package becomes a "GDB extension" (rather than an ordinary
|
|
52
|
+
command-line tool) when it does two things:
|
|
53
|
+
|
|
54
|
+
1. `import gdb` — only resolvable inside GDB's embedded Python.
|
|
55
|
+
2. Subclass `gdb.Command` and **instantiate** the subclass — instantiation is
|
|
56
|
+
what registers the command into GDB's command table.
|
|
57
|
+
|
|
58
|
+
`weargdb` does both on import, so `py import weargdb` is all you need.
|
|
59
|
+
|
|
60
|
+
> [!NOTE]
|
|
61
|
+
> `weargdb` cannot be imported by the plain system Python — there is no `gdb`
|
|
62
|
+
> module there. Importing it outside GDB raises a clear `ImportError` telling
|
|
63
|
+
> you to run it inside GDB. This is expected, not a bug.
|
|
64
|
+
|
|
65
|
+
## Why `import`, not `source`
|
|
66
|
+
|
|
67
|
+
GDB offers two ways to run a Python file, and for a package like `weargdb` they
|
|
68
|
+
are **not** interchangeable:
|
|
69
|
+
|
|
70
|
+
| Command | GDB runs the file as | `__name__` becomes |
|
|
71
|
+
|---|---|---|
|
|
72
|
+
| `py import weargdb` | a **module** (package import) | `"weargdb"` |
|
|
73
|
+
| `source path/to/file.py` | a **top-level script** | `"__main__"` |
|
|
74
|
+
|
|
75
|
+
`weargdb` is a *package*: its `__init__.py` does `from .commands import
|
|
76
|
+
register_all`, and `commands.py` does `from . import __version__`. These are
|
|
77
|
+
**relative imports** (note the leading dot), and a relative import only resolves
|
|
78
|
+
when the file is loaded as part of a package — that is, via `import`. If you
|
|
79
|
+
instead `source src/weargdb/__init__.py`, Python runs it as a standalone script
|
|
80
|
+
with no parent package, and the relative import fails immediately:
|
|
81
|
+
|
|
82
|
+
```text
|
|
83
|
+
ImportError: attempted relative import with no known parent package
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
That is why **`source` cannot load `weargdb`** — you must `import` it. A single
|
|
87
|
+
`py import weargdb` does both jobs at once: it resolves the relative imports
|
|
88
|
+
*and* runs `register_all()`, which registers every command into GDB.
|
|
89
|
+
|
|
90
|
+
`source` is only the right tool for a self-contained single file with no
|
|
91
|
+
relative imports. `weargdb` is deliberately a package — one import entry point,
|
|
92
|
+
a clean `_COMMANDS` registry, and room to grow into submodules.
|
|
93
|
+
|
|
94
|
+
## Installation
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
# From a local checkout (editable -- code changes take effect on next GDB start)
|
|
98
|
+
pip install -e .
|
|
99
|
+
|
|
100
|
+
# Or a regular install
|
|
101
|
+
pip install .
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The `gdb` module is provided by GDB at runtime and is intentionally **not** a
|
|
105
|
+
PyPI dependency, so `pip` never tries to fetch it.
|
|
106
|
+
|
|
107
|
+
> [!IMPORTANT]
|
|
108
|
+
> `pip install` puts `weargdb` on the **system** Python's path. GDB's embedded
|
|
109
|
+
> Python must share that path for `py import weargdb` to resolve. This works out
|
|
110
|
+
> of the box when GDB is linked against the same Python that ran `pip`. If
|
|
111
|
+
> `py import weargdb` reports `No module named 'weargdb'`, see
|
|
112
|
+
> [Troubleshooting](#troubleshooting).
|
|
113
|
+
|
|
114
|
+
## Commands
|
|
115
|
+
|
|
116
|
+
| Command | What it does | Needs a core? |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `wear_hello [args]` | Demo command that echoes its arguments | No |
|
|
119
|
+
| `wear_ver` | Print the weargdb extension version | No |
|
|
120
|
+
| `wear_sym <name>` | Resolve a symbol to its type and address | No (reads ELF) |
|
|
121
|
+
| `wear_sections` | List ELF sections with load addresses and sizes | No (reads ELF) |
|
|
122
|
+
| `wear_dump <expr> [nbytes]` | Hex-dump raw memory at a C expression's address | Yes (reads memory) |
|
|
123
|
+
| `wear_buildinfo` | Print a build-info string baked into the firmware | No (reads `.rodata`) |
|
|
124
|
+
|
|
125
|
+
## Usage
|
|
126
|
+
|
|
127
|
+
```text
|
|
128
|
+
$ gdb-multiarch -q
|
|
129
|
+
(gdb) py import weargdb
|
|
130
|
+
[weargdb] loaded GDB commands: wear_hello, wear_ver, wear_sym, wear_sections, wear_dump, wear_buildinfo
|
|
131
|
+
|
|
132
|
+
(gdb) wear_ver
|
|
133
|
+
weargdb GDB extension v0.0.1
|
|
134
|
+
|
|
135
|
+
(gdb) help user-defined # lists every command weargdb registered
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Inspecting the loaded ELF
|
|
139
|
+
|
|
140
|
+
These commands read the ELF that GDB has loaded (`gdb a.out` or
|
|
141
|
+
`(gdb) file a.out`) — no running inferior or coredump required:
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
(gdb) wear_sections # list ELF sections with load addresses + sizes
|
|
145
|
+
|
|
146
|
+
(gdb) wear_sym nx_start # function symbol -> its type and address
|
|
147
|
+
nx_start: type=void (void), address=0x...
|
|
148
|
+
|
|
149
|
+
(gdb) wear_sym g_some_global # data symbol -> type, address, and ELF value
|
|
150
|
+
g_some_global: type=int, address=0x...
|
|
151
|
+
value = 0
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
> [!WARNING]
|
|
155
|
+
> For a data symbol, `wear_sym` prints the initializer baked into the ELF's
|
|
156
|
+
> `.data`/`.rodata` — *not* the runtime value. To see the value at crash time,
|
|
157
|
+
> load a coredump first (`target core x.core` / `target nxstub`), then query the
|
|
158
|
+
> symbol.
|
|
159
|
+
|
|
160
|
+
### Dumping memory at a symbol or address
|
|
161
|
+
|
|
162
|
+
`wear_dump <expr> [nbytes]` evaluates a C expression to an address and hex-dumps
|
|
163
|
+
the bytes there (default 64). Unlike the ELF-only commands above, this reads
|
|
164
|
+
**live/core memory**, so it needs a running inferior or a loaded coredump:
|
|
165
|
+
|
|
166
|
+
```text
|
|
167
|
+
(gdb) wear_dump &g_some_global 32 # dump 32 bytes at the variable's address
|
|
168
|
+
0x20001000 01 00 00 00 2a 00 00 00 ... ....*...
|
|
169
|
+
(gdb) wear_dump 0x20001000 # a bare address works too (default 64 B)
|
|
170
|
+
(gdb) wear_dump g_tcb->stack_alloc # any C expression GDB can evaluate
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
It chains four of the most-used GDB Python APIs end to end:
|
|
174
|
+
`gdb.string_to_argv` (split args), `gdb.parse_and_eval` (expr -> `gdb.Value`),
|
|
175
|
+
`int(value)` / `value.address` (get the address), and
|
|
176
|
+
`gdb.selected_inferior().read_memory` (read raw bytes).
|
|
177
|
+
|
|
178
|
+
### Reading a build-info string baked into the firmware
|
|
179
|
+
|
|
180
|
+
`wear_buildinfo` reads a single global string the firmware exports at compile
|
|
181
|
+
time, the same way NuttX's own `uname` reads `g_version` out of the ELF. The C
|
|
182
|
+
side and this command are coupled **only** by the symbol name `g_build_info` —
|
|
183
|
+
keep them in sync. Because the string lives in `.rodata`, a bare ELF is enough
|
|
184
|
+
(no coredump needed):
|
|
185
|
+
|
|
186
|
+
```text
|
|
187
|
+
(gdb) wear_buildinfo
|
|
188
|
+
Jun 1 2026 12:00:00 bt
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
To export the symbol, define one global string in your firmware (compile-time
|
|
192
|
+
values, no runtime code):
|
|
193
|
+
|
|
194
|
+
```c
|
|
195
|
+
/* Pick the variant from whatever build macro distinguishes your targets. */
|
|
196
|
+
#ifdef CONFIG_TELEPHONY
|
|
197
|
+
# define BUILD_VARIANT "esim"
|
|
198
|
+
#else
|
|
199
|
+
# define BUILD_VARIANT "bt"
|
|
200
|
+
#endif
|
|
201
|
+
|
|
202
|
+
/* __attribute__((used)) stops LTO from dropping it when nothing references it
|
|
203
|
+
-- otherwise the symbol may be optimized out and the command finds nothing. */
|
|
204
|
+
const char g_build_info[] __attribute__((used)) =
|
|
205
|
+
__DATE__ " " __TIME__ " " BUILD_VARIANT;
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The command uses `gdb.lookup_global_symbol` to find the symbol and
|
|
209
|
+
`gdb.Value.string()` to read the NUL-terminated `char[]` as a Python string.
|
|
210
|
+
|
|
211
|
+
### Load automatically on every GDB start
|
|
212
|
+
|
|
213
|
+
Add to `~/.gdbinit`:
|
|
214
|
+
|
|
215
|
+
```text
|
|
216
|
+
python
|
|
217
|
+
import weargdb
|
|
218
|
+
end
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Adding your own command
|
|
222
|
+
|
|
223
|
+
Edit `src/weargdb/commands.py`:
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
class MyCmd(gdb.Command):
|
|
227
|
+
"""my_cmd -- one-line description."""
|
|
228
|
+
|
|
229
|
+
def __init__(self):
|
|
230
|
+
super().__init__("my_cmd", gdb.COMMAND_USER)
|
|
231
|
+
|
|
232
|
+
def invoke(self, arg, from_tty):
|
|
233
|
+
gdb.write("hello from my_cmd\n")
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Then append `MyCmd` to the `_COMMANDS` tuple at the bottom of the file —
|
|
237
|
+
`register_all()` instantiates every entry in the tuple on import.
|
|
238
|
+
|
|
239
|
+
> [!TIP]
|
|
240
|
+
> After editing, the simplest way to pick up the change is to **restart GDB**.
|
|
241
|
+
> Within a running session, re-running `py import weargdb` is a no-op because
|
|
242
|
+
> Python caches the module. To force a reload without restarting, clear the
|
|
243
|
+
> cache first:
|
|
244
|
+
>
|
|
245
|
+
> ```text
|
|
246
|
+
> (gdb) python import sys; [sys.modules.pop(m) for m in list(sys.modules) if m.startswith("weargdb")]
|
|
247
|
+
> (gdb) py import weargdb
|
|
248
|
+
> ```
|
|
249
|
+
>
|
|
250
|
+
> `source src/weargdb/commands.py` does **not** work — it runs the file as a
|
|
251
|
+
> script, so the relative `from . import __version__` fails. See
|
|
252
|
+
> [Why `import`, not `source`](#why-import-not-source).
|
|
253
|
+
|
|
254
|
+
### GDB Python API cheat sheet
|
|
255
|
+
|
|
256
|
+
The APIs the bundled commands use, plus the ones you will most likely reach for
|
|
257
|
+
when writing your own:
|
|
258
|
+
|
|
259
|
+
| API | Purpose |
|
|
260
|
+
|---|---|
|
|
261
|
+
| `gdb.Command` | Base class for a custom command; instantiating a subclass registers it |
|
|
262
|
+
| `Command.invoke(self, arg, from_tty)` | Called when the command runs; `arg` is the raw argument string |
|
|
263
|
+
| `gdb.write(s)` | Print to GDB's output stream (use instead of `print()`) |
|
|
264
|
+
| `gdb.string_to_argv(arg)` | Split an arg string into a list the way GDB does (honours quoting) |
|
|
265
|
+
| `gdb.execute(cmd, to_string=True)` | Run a GDB command; capture its output as a string |
|
|
266
|
+
| `gdb.lookup_global_symbol(name)` | Look up a global symbol in the ELF; returns `gdb.Symbol` or `None` |
|
|
267
|
+
| `gdb.parse_and_eval(expr)` | Evaluate any C expression to a `gdb.Value` (e.g. `"g_foo->bar"`) |
|
|
268
|
+
| `gdb.selected_inferior().read_memory(addr, n)` | Read `n` raw bytes (needs a live inferior or core) |
|
|
269
|
+
| `gdb.Symbol.value()` / `.type` / `.is_function` | A symbol's value (`gdb.Value`), its type, whether it is a function |
|
|
270
|
+
| `gdb.Value.address` / `int(val)` / `str(val)` | The value's address; convert a `gdb.Value` to Python `int` / `str` |
|
|
271
|
+
| `gdb.Value.type.code` | The type's kind, compared against `gdb.TYPE_CODE_PTR` / `_ARRAY` / `_FUNC` / ... |
|
|
272
|
+
| `gdb.lookup_type("struct tcb_s")` | Get a `gdb.Type`, often used with `value.cast(type)` |
|
|
273
|
+
| `gdb.objfiles()` | List of loaded object files (e.g. to check whether an ELF is loaded yet) |
|
|
274
|
+
|
|
275
|
+
> [!NOTE]
|
|
276
|
+
> `lookup_global_symbol` and `parse_and_eval` of a global work on a bare ELF,
|
|
277
|
+
> but they give the *link-time* value. Anything that reads memory
|
|
278
|
+
> (`read_memory`) or runtime state needs a live inferior or a loaded coredump.
|
|
279
|
+
|
|
280
|
+
## Project layout
|
|
281
|
+
|
|
282
|
+
```text
|
|
283
|
+
weargdb/
|
|
284
|
+
├── pyproject.toml # PEP 621 metadata, hatchling backend, no runtime deps
|
|
285
|
+
├── README.md
|
|
286
|
+
├── LICENSE # Apache 2.0
|
|
287
|
+
├── src/
|
|
288
|
+
│ └── weargdb/
|
|
289
|
+
│ ├── __init__.py # imports gdb, calls register_all() on import
|
|
290
|
+
│ └── commands.py # gdb.Command subclasses + the _COMMANDS registry
|
|
291
|
+
└── tests/
|
|
292
|
+
└── test_hexdump.py # pure-Python tests (stub the gdb module)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Testing
|
|
296
|
+
|
|
297
|
+
The commands depend on GDB's embedded `gdb` module, so the gdb-independent logic
|
|
298
|
+
(the hex-dump formatter) is what gets unit-tested under a plain `pytest` run; the
|
|
299
|
+
test injects a stub `gdb` module before importing the package.
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
pip install -e '.[dev]'
|
|
303
|
+
pytest
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Troubleshooting
|
|
307
|
+
|
|
308
|
+
| Symptom | Cause | Fix |
|
|
309
|
+
|---|---|---|
|
|
310
|
+
| `py import weargdb` → `No module named 'weargdb'` | GDB's Python differs from the Python `pip` installed into | Find GDB's Python with `gdb -ex "py import sys; print(sys.path)"`, then `pip install` into that interpreter, or prepend the install dir to `sys.path` in `~/.gdbinit` before `import weargdb`. |
|
|
311
|
+
| `import weargdb` from a normal shell `python3` fails | Expected — the `gdb` module only exists inside GDB | Run inside GDB, not the system Python. |
|
|
312
|
+
| Edited a command but GDB still runs the old one | Python cached the module | Restart GDB, or clear the cache then re-import (see [Adding your own command](#adding-your-own-command)). `source` does not work — `weargdb` is a package. |
|
weargdb-0.0.1/README.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
<!-- SPDX-License-Identifier: Apache-2.0 -->
|
|
2
|
+
<!-- Copyright (c) 2026 Junbo Zheng -->
|
|
3
|
+
|
|
4
|
+
<div align="center">
|
|
5
|
+
|
|
6
|
+
# weargdb
|
|
7
|
+
|
|
8
|
+
_A pip-installable GDB extension that registers custom GDB commands for embedded debugging._
|
|
9
|
+
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
[](pyproject.toml)
|
|
12
|
+
|
|
13
|
+
[How it works](#how-it-works) • [Install](#installation) • [Commands](#commands) • [Usage](#usage) • [Extend](#adding-your-own-command) • [Troubleshooting](#troubleshooting)
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
After installation, a single `py import weargdb` inside GDB makes every bundled
|
|
18
|
+
command available at the `(gdb)` prompt. The commands read symbols, sections,
|
|
19
|
+
and memory straight out of the loaded ELF or coredump using GDB's Python API —
|
|
20
|
+
handy for inspecting NuttX/Vela firmware crash dumps, but tied to nothing
|
|
21
|
+
specific, so they work against any ELF.
|
|
22
|
+
|
|
23
|
+
## How it works
|
|
24
|
+
|
|
25
|
+
GDB ships with an **embedded Python interpreter** and exposes a built-in `gdb`
|
|
26
|
+
module to it. A package becomes a "GDB extension" (rather than an ordinary
|
|
27
|
+
command-line tool) when it does two things:
|
|
28
|
+
|
|
29
|
+
1. `import gdb` — only resolvable inside GDB's embedded Python.
|
|
30
|
+
2. Subclass `gdb.Command` and **instantiate** the subclass — instantiation is
|
|
31
|
+
what registers the command into GDB's command table.
|
|
32
|
+
|
|
33
|
+
`weargdb` does both on import, so `py import weargdb` is all you need.
|
|
34
|
+
|
|
35
|
+
> [!NOTE]
|
|
36
|
+
> `weargdb` cannot be imported by the plain system Python — there is no `gdb`
|
|
37
|
+
> module there. Importing it outside GDB raises a clear `ImportError` telling
|
|
38
|
+
> you to run it inside GDB. This is expected, not a bug.
|
|
39
|
+
|
|
40
|
+
## Why `import`, not `source`
|
|
41
|
+
|
|
42
|
+
GDB offers two ways to run a Python file, and for a package like `weargdb` they
|
|
43
|
+
are **not** interchangeable:
|
|
44
|
+
|
|
45
|
+
| Command | GDB runs the file as | `__name__` becomes |
|
|
46
|
+
|---|---|---|
|
|
47
|
+
| `py import weargdb` | a **module** (package import) | `"weargdb"` |
|
|
48
|
+
| `source path/to/file.py` | a **top-level script** | `"__main__"` |
|
|
49
|
+
|
|
50
|
+
`weargdb` is a *package*: its `__init__.py` does `from .commands import
|
|
51
|
+
register_all`, and `commands.py` does `from . import __version__`. These are
|
|
52
|
+
**relative imports** (note the leading dot), and a relative import only resolves
|
|
53
|
+
when the file is loaded as part of a package — that is, via `import`. If you
|
|
54
|
+
instead `source src/weargdb/__init__.py`, Python runs it as a standalone script
|
|
55
|
+
with no parent package, and the relative import fails immediately:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
ImportError: attempted relative import with no known parent package
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
That is why **`source` cannot load `weargdb`** — you must `import` it. A single
|
|
62
|
+
`py import weargdb` does both jobs at once: it resolves the relative imports
|
|
63
|
+
*and* runs `register_all()`, which registers every command into GDB.
|
|
64
|
+
|
|
65
|
+
`source` is only the right tool for a self-contained single file with no
|
|
66
|
+
relative imports. `weargdb` is deliberately a package — one import entry point,
|
|
67
|
+
a clean `_COMMANDS` registry, and room to grow into submodules.
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# From a local checkout (editable -- code changes take effect on next GDB start)
|
|
73
|
+
pip install -e .
|
|
74
|
+
|
|
75
|
+
# Or a regular install
|
|
76
|
+
pip install .
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `gdb` module is provided by GDB at runtime and is intentionally **not** a
|
|
80
|
+
PyPI dependency, so `pip` never tries to fetch it.
|
|
81
|
+
|
|
82
|
+
> [!IMPORTANT]
|
|
83
|
+
> `pip install` puts `weargdb` on the **system** Python's path. GDB's embedded
|
|
84
|
+
> Python must share that path for `py import weargdb` to resolve. This works out
|
|
85
|
+
> of the box when GDB is linked against the same Python that ran `pip`. If
|
|
86
|
+
> `py import weargdb` reports `No module named 'weargdb'`, see
|
|
87
|
+
> [Troubleshooting](#troubleshooting).
|
|
88
|
+
|
|
89
|
+
## Commands
|
|
90
|
+
|
|
91
|
+
| Command | What it does | Needs a core? |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `wear_hello [args]` | Demo command that echoes its arguments | No |
|
|
94
|
+
| `wear_ver` | Print the weargdb extension version | No |
|
|
95
|
+
| `wear_sym <name>` | Resolve a symbol to its type and address | No (reads ELF) |
|
|
96
|
+
| `wear_sections` | List ELF sections with load addresses and sizes | No (reads ELF) |
|
|
97
|
+
| `wear_dump <expr> [nbytes]` | Hex-dump raw memory at a C expression's address | Yes (reads memory) |
|
|
98
|
+
| `wear_buildinfo` | Print a build-info string baked into the firmware | No (reads `.rodata`) |
|
|
99
|
+
|
|
100
|
+
## Usage
|
|
101
|
+
|
|
102
|
+
```text
|
|
103
|
+
$ gdb-multiarch -q
|
|
104
|
+
(gdb) py import weargdb
|
|
105
|
+
[weargdb] loaded GDB commands: wear_hello, wear_ver, wear_sym, wear_sections, wear_dump, wear_buildinfo
|
|
106
|
+
|
|
107
|
+
(gdb) wear_ver
|
|
108
|
+
weargdb GDB extension v0.0.1
|
|
109
|
+
|
|
110
|
+
(gdb) help user-defined # lists every command weargdb registered
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Inspecting the loaded ELF
|
|
114
|
+
|
|
115
|
+
These commands read the ELF that GDB has loaded (`gdb a.out` or
|
|
116
|
+
`(gdb) file a.out`) — no running inferior or coredump required:
|
|
117
|
+
|
|
118
|
+
```text
|
|
119
|
+
(gdb) wear_sections # list ELF sections with load addresses + sizes
|
|
120
|
+
|
|
121
|
+
(gdb) wear_sym nx_start # function symbol -> its type and address
|
|
122
|
+
nx_start: type=void (void), address=0x...
|
|
123
|
+
|
|
124
|
+
(gdb) wear_sym g_some_global # data symbol -> type, address, and ELF value
|
|
125
|
+
g_some_global: type=int, address=0x...
|
|
126
|
+
value = 0
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
> [!WARNING]
|
|
130
|
+
> For a data symbol, `wear_sym` prints the initializer baked into the ELF's
|
|
131
|
+
> `.data`/`.rodata` — *not* the runtime value. To see the value at crash time,
|
|
132
|
+
> load a coredump first (`target core x.core` / `target nxstub`), then query the
|
|
133
|
+
> symbol.
|
|
134
|
+
|
|
135
|
+
### Dumping memory at a symbol or address
|
|
136
|
+
|
|
137
|
+
`wear_dump <expr> [nbytes]` evaluates a C expression to an address and hex-dumps
|
|
138
|
+
the bytes there (default 64). Unlike the ELF-only commands above, this reads
|
|
139
|
+
**live/core memory**, so it needs a running inferior or a loaded coredump:
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
(gdb) wear_dump &g_some_global 32 # dump 32 bytes at the variable's address
|
|
143
|
+
0x20001000 01 00 00 00 2a 00 00 00 ... ....*...
|
|
144
|
+
(gdb) wear_dump 0x20001000 # a bare address works too (default 64 B)
|
|
145
|
+
(gdb) wear_dump g_tcb->stack_alloc # any C expression GDB can evaluate
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
It chains four of the most-used GDB Python APIs end to end:
|
|
149
|
+
`gdb.string_to_argv` (split args), `gdb.parse_and_eval` (expr -> `gdb.Value`),
|
|
150
|
+
`int(value)` / `value.address` (get the address), and
|
|
151
|
+
`gdb.selected_inferior().read_memory` (read raw bytes).
|
|
152
|
+
|
|
153
|
+
### Reading a build-info string baked into the firmware
|
|
154
|
+
|
|
155
|
+
`wear_buildinfo` reads a single global string the firmware exports at compile
|
|
156
|
+
time, the same way NuttX's own `uname` reads `g_version` out of the ELF. The C
|
|
157
|
+
side and this command are coupled **only** by the symbol name `g_build_info` —
|
|
158
|
+
keep them in sync. Because the string lives in `.rodata`, a bare ELF is enough
|
|
159
|
+
(no coredump needed):
|
|
160
|
+
|
|
161
|
+
```text
|
|
162
|
+
(gdb) wear_buildinfo
|
|
163
|
+
Jun 1 2026 12:00:00 bt
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
To export the symbol, define one global string in your firmware (compile-time
|
|
167
|
+
values, no runtime code):
|
|
168
|
+
|
|
169
|
+
```c
|
|
170
|
+
/* Pick the variant from whatever build macro distinguishes your targets. */
|
|
171
|
+
#ifdef CONFIG_TELEPHONY
|
|
172
|
+
# define BUILD_VARIANT "esim"
|
|
173
|
+
#else
|
|
174
|
+
# define BUILD_VARIANT "bt"
|
|
175
|
+
#endif
|
|
176
|
+
|
|
177
|
+
/* __attribute__((used)) stops LTO from dropping it when nothing references it
|
|
178
|
+
-- otherwise the symbol may be optimized out and the command finds nothing. */
|
|
179
|
+
const char g_build_info[] __attribute__((used)) =
|
|
180
|
+
__DATE__ " " __TIME__ " " BUILD_VARIANT;
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The command uses `gdb.lookup_global_symbol` to find the symbol and
|
|
184
|
+
`gdb.Value.string()` to read the NUL-terminated `char[]` as a Python string.
|
|
185
|
+
|
|
186
|
+
### Load automatically on every GDB start
|
|
187
|
+
|
|
188
|
+
Add to `~/.gdbinit`:
|
|
189
|
+
|
|
190
|
+
```text
|
|
191
|
+
python
|
|
192
|
+
import weargdb
|
|
193
|
+
end
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Adding your own command
|
|
197
|
+
|
|
198
|
+
Edit `src/weargdb/commands.py`:
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
class MyCmd(gdb.Command):
|
|
202
|
+
"""my_cmd -- one-line description."""
|
|
203
|
+
|
|
204
|
+
def __init__(self):
|
|
205
|
+
super().__init__("my_cmd", gdb.COMMAND_USER)
|
|
206
|
+
|
|
207
|
+
def invoke(self, arg, from_tty):
|
|
208
|
+
gdb.write("hello from my_cmd\n")
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Then append `MyCmd` to the `_COMMANDS` tuple at the bottom of the file —
|
|
212
|
+
`register_all()` instantiates every entry in the tuple on import.
|
|
213
|
+
|
|
214
|
+
> [!TIP]
|
|
215
|
+
> After editing, the simplest way to pick up the change is to **restart GDB**.
|
|
216
|
+
> Within a running session, re-running `py import weargdb` is a no-op because
|
|
217
|
+
> Python caches the module. To force a reload without restarting, clear the
|
|
218
|
+
> cache first:
|
|
219
|
+
>
|
|
220
|
+
> ```text
|
|
221
|
+
> (gdb) python import sys; [sys.modules.pop(m) for m in list(sys.modules) if m.startswith("weargdb")]
|
|
222
|
+
> (gdb) py import weargdb
|
|
223
|
+
> ```
|
|
224
|
+
>
|
|
225
|
+
> `source src/weargdb/commands.py` does **not** work — it runs the file as a
|
|
226
|
+
> script, so the relative `from . import __version__` fails. See
|
|
227
|
+
> [Why `import`, not `source`](#why-import-not-source).
|
|
228
|
+
|
|
229
|
+
### GDB Python API cheat sheet
|
|
230
|
+
|
|
231
|
+
The APIs the bundled commands use, plus the ones you will most likely reach for
|
|
232
|
+
when writing your own:
|
|
233
|
+
|
|
234
|
+
| API | Purpose |
|
|
235
|
+
|---|---|
|
|
236
|
+
| `gdb.Command` | Base class for a custom command; instantiating a subclass registers it |
|
|
237
|
+
| `Command.invoke(self, arg, from_tty)` | Called when the command runs; `arg` is the raw argument string |
|
|
238
|
+
| `gdb.write(s)` | Print to GDB's output stream (use instead of `print()`) |
|
|
239
|
+
| `gdb.string_to_argv(arg)` | Split an arg string into a list the way GDB does (honours quoting) |
|
|
240
|
+
| `gdb.execute(cmd, to_string=True)` | Run a GDB command; capture its output as a string |
|
|
241
|
+
| `gdb.lookup_global_symbol(name)` | Look up a global symbol in the ELF; returns `gdb.Symbol` or `None` |
|
|
242
|
+
| `gdb.parse_and_eval(expr)` | Evaluate any C expression to a `gdb.Value` (e.g. `"g_foo->bar"`) |
|
|
243
|
+
| `gdb.selected_inferior().read_memory(addr, n)` | Read `n` raw bytes (needs a live inferior or core) |
|
|
244
|
+
| `gdb.Symbol.value()` / `.type` / `.is_function` | A symbol's value (`gdb.Value`), its type, whether it is a function |
|
|
245
|
+
| `gdb.Value.address` / `int(val)` / `str(val)` | The value's address; convert a `gdb.Value` to Python `int` / `str` |
|
|
246
|
+
| `gdb.Value.type.code` | The type's kind, compared against `gdb.TYPE_CODE_PTR` / `_ARRAY` / `_FUNC` / ... |
|
|
247
|
+
| `gdb.lookup_type("struct tcb_s")` | Get a `gdb.Type`, often used with `value.cast(type)` |
|
|
248
|
+
| `gdb.objfiles()` | List of loaded object files (e.g. to check whether an ELF is loaded yet) |
|
|
249
|
+
|
|
250
|
+
> [!NOTE]
|
|
251
|
+
> `lookup_global_symbol` and `parse_and_eval` of a global work on a bare ELF,
|
|
252
|
+
> but they give the *link-time* value. Anything that reads memory
|
|
253
|
+
> (`read_memory`) or runtime state needs a live inferior or a loaded coredump.
|
|
254
|
+
|
|
255
|
+
## Project layout
|
|
256
|
+
|
|
257
|
+
```text
|
|
258
|
+
weargdb/
|
|
259
|
+
├── pyproject.toml # PEP 621 metadata, hatchling backend, no runtime deps
|
|
260
|
+
├── README.md
|
|
261
|
+
├── LICENSE # Apache 2.0
|
|
262
|
+
├── src/
|
|
263
|
+
│ └── weargdb/
|
|
264
|
+
│ ├── __init__.py # imports gdb, calls register_all() on import
|
|
265
|
+
│ └── commands.py # gdb.Command subclasses + the _COMMANDS registry
|
|
266
|
+
└── tests/
|
|
267
|
+
└── test_hexdump.py # pure-Python tests (stub the gdb module)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Testing
|
|
271
|
+
|
|
272
|
+
The commands depend on GDB's embedded `gdb` module, so the gdb-independent logic
|
|
273
|
+
(the hex-dump formatter) is what gets unit-tested under a plain `pytest` run; the
|
|
274
|
+
test injects a stub `gdb` module before importing the package.
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
pip install -e '.[dev]'
|
|
278
|
+
pytest
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Troubleshooting
|
|
282
|
+
|
|
283
|
+
| Symptom | Cause | Fix |
|
|
284
|
+
|---|---|---|
|
|
285
|
+
| `py import weargdb` → `No module named 'weargdb'` | GDB's Python differs from the Python `pip` installed into | Find GDB's Python with `gdb -ex "py import sys; print(sys.path)"`, then `pip install` into that interpreter, or prepend the install dir to `sys.path` in `~/.gdbinit` before `import weargdb`. |
|
|
286
|
+
| `import weargdb` from a normal shell `python3` fails | Expected — the `gdb` module only exists inside GDB | Run inside GDB, not the system Python. |
|
|
287
|
+
| Edited a command but GDB still runs the old one | Python cached the module | Restart GDB, or clear the cache then re-import (see [Adding your own command](#adding-your-own-command)). `source` does not work — `weargdb` is a package. |
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright (c) 2026 Junbo Zheng
|
|
3
|
+
|
|
4
|
+
[build-system]
|
|
5
|
+
requires = ["hatchling"]
|
|
6
|
+
build-backend = "hatchling.build"
|
|
7
|
+
|
|
8
|
+
[project]
|
|
9
|
+
name = "weargdb"
|
|
10
|
+
dynamic = ["version"]
|
|
11
|
+
description = "A pip-installable GDB extension that registers custom GDB commands"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = { text = "Apache-2.0" }
|
|
14
|
+
requires-python = ">=3.10"
|
|
15
|
+
authors = [
|
|
16
|
+
{ name = "Junbo Zheng", email = "3273070@qq.com" },
|
|
17
|
+
]
|
|
18
|
+
keywords = ["gdb", "gdb-extension", "debugging"]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"License :: OSI Approved :: Apache Software License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
"Topic :: Software Development :: Debuggers",
|
|
27
|
+
]
|
|
28
|
+
# No runtime dependencies: the `gdb` module is provided by GDB's embedded
|
|
29
|
+
# Python interpreter, NOT installable from PyPI. Do not list it here.
|
|
30
|
+
dependencies = []
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"black",
|
|
35
|
+
"build",
|
|
36
|
+
"pytest",
|
|
37
|
+
"twine",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/Junbo-Zheng/weargdb"
|
|
42
|
+
Issues = "https://github.com/Junbo-Zheng/weargdb/issues"
|
|
43
|
+
|
|
44
|
+
[tool.hatch.version]
|
|
45
|
+
path = "src/weargdb/__init__.py"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["src/weargdb"]
|
|
49
|
+
|
|
50
|
+
[tool.black]
|
|
51
|
+
line-length = 88
|
|
52
|
+
target-version = ["py310"]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright (c) 2026 Junbo Zheng
|
|
3
|
+
|
|
4
|
+
"""weargdb: a pip-installable GDB extension that registers custom GDB commands.
|
|
5
|
+
|
|
6
|
+
Usage inside GDB (the embedded Python interpreter provides the ``gdb`` module):
|
|
7
|
+
|
|
8
|
+
(gdb) py import weargdb
|
|
9
|
+
|
|
10
|
+
Importing this package registers all bundled :class:`gdb.Command` subclasses.
|
|
11
|
+
It is NOT meant to be imported by the plain system Python — there is no ``gdb``
|
|
12
|
+
module there, so the import will fail with a clear message pointing you to run
|
|
13
|
+
it inside GDB instead.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
__version__ = "0.0.1"
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
import gdb # noqa: F401 -- only available inside GDB's embedded Python
|
|
20
|
+
except ImportError as exc: # pragma: no cover - exercised only outside GDB
|
|
21
|
+
raise ImportError(
|
|
22
|
+
"The 'weargdb' package is a GDB extension and must be imported inside "
|
|
23
|
+
"GDB's embedded Python, e.g.:\n"
|
|
24
|
+
" gdb-multiarch -ex 'py import weargdb'\n"
|
|
25
|
+
"It cannot be imported by the plain system Python because the built-in "
|
|
26
|
+
"'gdb' module only exists inside GDB."
|
|
27
|
+
) from exc
|
|
28
|
+
|
|
29
|
+
from .commands import register_all
|
|
30
|
+
|
|
31
|
+
# Registering on import is what makes the commands appear at the (gdb) prompt.
|
|
32
|
+
register_all()
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright (c) 2026 Junbo Zheng
|
|
3
|
+
|
|
4
|
+
"""Custom GDB command definitions.
|
|
5
|
+
|
|
6
|
+
Each command is a :class:`gdb.Command` subclass. The crucial step is
|
|
7
|
+
*instantiating* the class (done in :func:`register_all`): defining the class
|
|
8
|
+
alone does nothing — only ``Cls()`` registers it into GDB's command table.
|
|
9
|
+
|
|
10
|
+
To add a new command:
|
|
11
|
+
1. Write a ``class MyCmd(gdb.Command)`` here with an ``invoke`` method.
|
|
12
|
+
2. Append it to the ``_COMMANDS`` tuple below.
|
|
13
|
+
That's it — ``register_all`` instantiates everything in the tuple.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import gdb
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class WeargdbHello(gdb.Command):
|
|
20
|
+
"""wear_hello -- demo command proving custom GDB commands work."""
|
|
21
|
+
|
|
22
|
+
def __init__(self):
|
|
23
|
+
# First arg is the name typed at the (gdb) prompt.
|
|
24
|
+
# gdb.COMMAND_USER lists it under "help user-defined".
|
|
25
|
+
super().__init__("wear_hello", gdb.COMMAND_USER)
|
|
26
|
+
|
|
27
|
+
def invoke(self, arg, from_tty):
|
|
28
|
+
# Runs each time the user types: (gdb) wear_hello [args]
|
|
29
|
+
gdb.write("Hello, Junbo Zheng! This is a custom GDB command.\n")
|
|
30
|
+
gdb.write(f" arg you passed = {arg!r}\n")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class WeargdbVer(gdb.Command):
|
|
34
|
+
"""wear_ver -- print the weargdb extension version."""
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
super().__init__("wear_ver", gdb.COMMAND_USER)
|
|
38
|
+
|
|
39
|
+
def invoke(self, arg, from_tty):
|
|
40
|
+
from . import __version__
|
|
41
|
+
|
|
42
|
+
gdb.write(f"weargdb GDB extension v{__version__}\n")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class WeargdbSym(gdb.Command):
|
|
46
|
+
"""wear_sym -- resolve a symbol from the loaded ELF to its address/value."""
|
|
47
|
+
|
|
48
|
+
def __init__(self):
|
|
49
|
+
super().__init__("wear_sym", gdb.COMMAND_USER)
|
|
50
|
+
|
|
51
|
+
def invoke(self, arg, from_tty):
|
|
52
|
+
# Runs as: (gdb) wear_sym <symbol-name>
|
|
53
|
+
name = arg.strip()
|
|
54
|
+
if not name:
|
|
55
|
+
gdb.write("usage: wear_sym <symbol-name>\n")
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
# lookup_global_symbol reads the ELF symbol table directly — no running
|
|
59
|
+
# inferior or core needed. Returns None when the name is absent.
|
|
60
|
+
sym = gdb.lookup_global_symbol(name)
|
|
61
|
+
if sym is None:
|
|
62
|
+
gdb.write(f"symbol {name!r} not found in the loaded ELF\n")
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
val = sym.value()
|
|
66
|
+
gdb.write(f"{name}: type={sym.type}, address={val.address}\n")
|
|
67
|
+
# For data symbols, .value() reads the initializer from the ELF's
|
|
68
|
+
# .data/.rodata section (static value, not runtime — that needs a core).
|
|
69
|
+
if not sym.is_function:
|
|
70
|
+
gdb.write(f" value = {val}\n")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class WeargdbSections(gdb.Command):
|
|
74
|
+
"""wear_sections -- list ELF sections with their load addresses and sizes."""
|
|
75
|
+
|
|
76
|
+
def __init__(self):
|
|
77
|
+
super().__init__("wear_sections", gdb.COMMAND_USER)
|
|
78
|
+
|
|
79
|
+
def invoke(self, arg, from_tty):
|
|
80
|
+
# Wrap a GDB built-in; to_string=True captures its output as a string
|
|
81
|
+
# so a future version could reformat/filter it instead of echoing raw.
|
|
82
|
+
out = gdb.execute("maintenance info sections", to_string=True)
|
|
83
|
+
gdb.write(out)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class WeargdbDump(gdb.Command):
|
|
87
|
+
"""wear_dump -- hex-dump raw memory at a C expression's address."""
|
|
88
|
+
|
|
89
|
+
def __init__(self):
|
|
90
|
+
super().__init__("wear_dump", gdb.COMMAND_USER)
|
|
91
|
+
|
|
92
|
+
def invoke(self, arg, from_tty):
|
|
93
|
+
# Runs as: (gdb) wear_dump <expr> [nbytes]
|
|
94
|
+
# string_to_argv splits the arg string the way GDB's own commands do
|
|
95
|
+
# (honours quoting), instead of a naive Python str.split().
|
|
96
|
+
argv = gdb.string_to_argv(arg)
|
|
97
|
+
if not argv:
|
|
98
|
+
gdb.write("usage: wear_dump <expr> [nbytes]\n")
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
expr = argv[0]
|
|
102
|
+
nbytes = int(argv[1]) if len(argv) > 1 else 64
|
|
103
|
+
|
|
104
|
+
# parse_and_eval evaluates any C expression in the current context and
|
|
105
|
+
# returns a gdb.Value -- "&g_foo", "g_tcb->stack", "0x20001000", etc.
|
|
106
|
+
try:
|
|
107
|
+
val = gdb.parse_and_eval(expr)
|
|
108
|
+
except gdb.error as e:
|
|
109
|
+
gdb.write(f"cannot evaluate {expr!r}: {e}\n")
|
|
110
|
+
return
|
|
111
|
+
|
|
112
|
+
# A gdb.Value carries its C type. If the expression already names an
|
|
113
|
+
# address-like value (pointer / array / function), int(val) is that
|
|
114
|
+
# address; otherwise take the object's own address via val.address.
|
|
115
|
+
if val.type.code in (
|
|
116
|
+
gdb.TYPE_CODE_PTR,
|
|
117
|
+
gdb.TYPE_CODE_ARRAY,
|
|
118
|
+
gdb.TYPE_CODE_FUNC,
|
|
119
|
+
):
|
|
120
|
+
addr = int(val)
|
|
121
|
+
elif val.address is not None:
|
|
122
|
+
addr = int(val.address)
|
|
123
|
+
else:
|
|
124
|
+
gdb.write(f"{expr!r} has no address to dump (it is a temporary)\n")
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
# read_memory needs a live inferior or a loaded core; it returns a
|
|
128
|
+
# memoryview of raw bytes. This is where a static ELF (no core) fails.
|
|
129
|
+
try:
|
|
130
|
+
inferior = gdb.selected_inferior()
|
|
131
|
+
mem = inferior.read_memory(addr, nbytes)
|
|
132
|
+
except gdb.MemoryError as e:
|
|
133
|
+
gdb.write(f"cannot read {nbytes} bytes at {addr:#x}: {e}\n")
|
|
134
|
+
return
|
|
135
|
+
|
|
136
|
+
gdb.write(self._hexdump(addr, bytes(mem)))
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def _hexdump(base, data):
|
|
140
|
+
# Classic 16-bytes-per-row hex + ASCII view. Returns the formatted
|
|
141
|
+
# string (rather than writing directly) so it is unit-testable without
|
|
142
|
+
# the gdb module -- see tests/test_hexdump.py.
|
|
143
|
+
lines = []
|
|
144
|
+
for off in range(0, len(data), 16):
|
|
145
|
+
chunk = data[off : off + 16]
|
|
146
|
+
hexpart = " ".join(f"{b:02x}" for b in chunk)
|
|
147
|
+
asciipart = "".join(chr(b) if 0x20 <= b <= 0x7E else "." for b in chunk)
|
|
148
|
+
lines.append(f"{base + off:#010x} {hexpart:<47} {asciipart}\n")
|
|
149
|
+
return "".join(lines)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class WeargdbBuildinfo(gdb.Command):
|
|
153
|
+
"""wear_buildinfo -- print the firmware build-info string from the ELF."""
|
|
154
|
+
|
|
155
|
+
# The C side must export a global string with THIS exact name. The two
|
|
156
|
+
# sides are coupled only by this symbol name -- keep them in sync.
|
|
157
|
+
SYMBOL = "g_build_info"
|
|
158
|
+
|
|
159
|
+
def __init__(self):
|
|
160
|
+
super().__init__("wear_buildinfo", gdb.COMMAND_USER)
|
|
161
|
+
|
|
162
|
+
def invoke(self, arg, from_tty):
|
|
163
|
+
# No args. The string lives in .rodata, so a bare ELF is enough -- no
|
|
164
|
+
# running inferior or coredump needed.
|
|
165
|
+
sym = gdb.lookup_global_symbol(self.SYMBOL)
|
|
166
|
+
if sym is None:
|
|
167
|
+
gdb.write(
|
|
168
|
+
f"{self.SYMBOL} not found "
|
|
169
|
+
f"(was the firmware built with build-info exported?)\n"
|
|
170
|
+
)
|
|
171
|
+
return
|
|
172
|
+
|
|
173
|
+
# .value().string() walks the char[] to the NUL terminator and returns
|
|
174
|
+
# a Python str -- the same idiom NuttX's own `uname` uses to read
|
|
175
|
+
# g_version out of the ELF.
|
|
176
|
+
try:
|
|
177
|
+
gdb.write(sym.value().string() + "\n")
|
|
178
|
+
except gdb.error as e:
|
|
179
|
+
gdb.write(f"cannot read {self.SYMBOL} as a string: {e}\n")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# Add new command classes here; register_all() instantiates each one.
|
|
183
|
+
_COMMANDS = (
|
|
184
|
+
WeargdbHello,
|
|
185
|
+
WeargdbVer,
|
|
186
|
+
WeargdbSym,
|
|
187
|
+
WeargdbSections,
|
|
188
|
+
WeargdbDump,
|
|
189
|
+
WeargdbBuildinfo,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def register_all():
|
|
194
|
+
"""Instantiate every command class, registering it into GDB."""
|
|
195
|
+
names = []
|
|
196
|
+
for cls in _COMMANDS:
|
|
197
|
+
cls()
|
|
198
|
+
# The registered command name is the first super().__init__ arg;
|
|
199
|
+
# we re-derive it from the docstring's leading token for the banner.
|
|
200
|
+
names.append(cls.__doc__.split(" ", 1)[0])
|
|
201
|
+
gdb.write(f"[weargdb] loaded GDB commands: {', '.join(names)}\n")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# Copyright (c) 2026 Junbo Zheng
|
|
3
|
+
|
|
4
|
+
"""Unit tests for the pure-Python parts of weargdb.
|
|
5
|
+
|
|
6
|
+
The weargdb package does ``import gdb`` at module top level, and the real gdb
|
|
7
|
+
module only exists inside GDB's embedded Python. To test the gdb-independent
|
|
8
|
+
logic (the hex-dump formatter) under a plain pytest run, we inject a stub
|
|
9
|
+
``gdb`` module into sys.modules *before* importing weargdb.commands.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import sys
|
|
13
|
+
import types
|
|
14
|
+
|
|
15
|
+
# Inject a minimal stub so `import gdb` inside commands.py succeeds. Importing
|
|
16
|
+
# the package also runs register_all(), which instantiates every Command and
|
|
17
|
+
# calls gdb.write, so the stub must cover those too.
|
|
18
|
+
_gdb_stub = types.ModuleType("gdb")
|
|
19
|
+
_gdb_stub.Command = type("Command", (), {"__init__": lambda self, *a, **k: None})
|
|
20
|
+
_gdb_stub.COMMAND_USER = 0
|
|
21
|
+
_gdb_stub.write = lambda *a, **k: None
|
|
22
|
+
# TYPE_CODE_* constants are referenced by WeargdbDump; any distinct values work.
|
|
23
|
+
_gdb_stub.TYPE_CODE_PTR = 1
|
|
24
|
+
_gdb_stub.TYPE_CODE_ARRAY = 2
|
|
25
|
+
_gdb_stub.TYPE_CODE_FUNC = 3
|
|
26
|
+
sys.modules.setdefault("gdb", _gdb_stub)
|
|
27
|
+
|
|
28
|
+
from weargdb.commands import WeargdbDump # noqa: E402 -- after the stub injection
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_hexdump_single_full_row():
|
|
32
|
+
data = bytes(range(16))
|
|
33
|
+
out = WeargdbDump._hexdump(0x1000, data)
|
|
34
|
+
assert out == (
|
|
35
|
+
"0x00001000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f "
|
|
36
|
+
"................\n"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_hexdump_ascii_rendering():
|
|
41
|
+
# "AB" printable, 0x00 and 0xff render as '.'
|
|
42
|
+
data = b"\x00AB\xff"
|
|
43
|
+
out = WeargdbDump._hexdump(0x2000, data)
|
|
44
|
+
assert out == "0x00002000 00 41 42 ff" + " " * (47 - 11) + " .AB.\n"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_hexdump_multiple_rows_and_partial_tail():
|
|
48
|
+
data = bytes(20) # 16 + 4 -> two rows
|
|
49
|
+
out = WeargdbDump._hexdump(0, data)
|
|
50
|
+
rows = out.splitlines()
|
|
51
|
+
assert len(rows) == 2
|
|
52
|
+
assert rows[0].startswith("0x00000000 ")
|
|
53
|
+
assert rows[1].startswith("0x00000010 ") # second row base += 16
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_hexdump_empty():
|
|
57
|
+
assert WeargdbDump._hexdump(0x4000, b"") == ""
|