cortex-unwind 1.1.0__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.
- cortex_unwind-1.1.0/LICENSE.txt +21 -0
- cortex_unwind-1.1.0/PKG-INFO +134 -0
- cortex_unwind-1.1.0/README.md +114 -0
- cortex_unwind-1.1.0/cortex_unwind.egg-info/PKG-INFO +134 -0
- cortex_unwind-1.1.0/cortex_unwind.egg-info/SOURCES.txt +10 -0
- cortex_unwind-1.1.0/cortex_unwind.egg-info/dependency_links.txt +1 -0
- cortex_unwind-1.1.0/cortex_unwind.egg-info/entry_points.txt +2 -0
- cortex_unwind-1.1.0/cortex_unwind.egg-info/top_level.txt +1 -0
- cortex_unwind-1.1.0/cortex_unwind.py +253 -0
- cortex_unwind-1.1.0/pyproject.toml +30 -0
- cortex_unwind-1.1.0/setup.cfg +7 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Trust & Thread
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cortex-unwind
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: ARM Cortex-M HardFault stack frame unwinder and address mapper CLI
|
|
5
|
+
Author-email: Trust & Thread <contact@trustandthread.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.trustandthread.com
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/GaneshMba4066/cortex-unwind/issues
|
|
9
|
+
Project-URL: Documentation, https://www.trustandthread.com/firmware-checklist/
|
|
10
|
+
Keywords: arm,cortex-m,stm32,hardfault,stack,unwinder,debugging
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
15
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
16
|
+
Requires-Python: >=3.6
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE.txt
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
\# cortex-unwind
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Zero-dependency CLI tool to decode ARM Cortex-M hardware stack frames and map
|
|
26
|
+
|
|
27
|
+
crash sites to source lines via `arm-none-eabi-addr2line`.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
When a Cortex-M core takes a HardFault, hardware stacks 8 registers
|
|
32
|
+
|
|
33
|
+
(`r0..r3, r12, lr, pc, xPSR`) onto the active stack. Paste those 8 words from a
|
|
34
|
+
|
|
35
|
+
UART fault log or GDB (`x/8wx $sp`) and this tool gives you the crash site.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Free \& Open Source (MIT) by \[Trust \& Thread](https://www.trustandthread.com).
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
\## Installation
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
pip install cortex-unwind
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Or run with zero install:
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
curl -sL https://raw.githubusercontent.com/GaneshMba4066/cortex-unwind/main/cortex\_unwind.py | python3
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
\## Quick Start
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
Pass the 8 hardware-stacked registers (`R0` `R1` `R2` `R3` `R12` `LR` `PC` `xPSR`):
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
With an ELF file for instant symbol resolution of PC and LR:
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000 build/firmware.elf
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
Got a raw hex dump from UART? Let `--auto` find the frame for you:
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
cortex-unwind --auto "0x00 0x01 0x20000000 0x40021000 0x00 0x08001255 0x08001290 0x21000000"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
\## What you get
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
\- Stacked register breakdown with Thumb-bit notes on PC/LR
|
|
92
|
+
|
|
93
|
+
\- Execution state: IPSR → active exception (HardFault, NMI, IRQn...), APSR flags
|
|
94
|
+
|
|
95
|
+
\- Thumb-bit validation (clear T-bit = corrupt PC, flagged immediately)
|
|
96
|
+
|
|
97
|
+
\- `EXC\_RETURN` decoding of LR — MSP/PSP selection and basic (8-word) vs FP-extended (26-word) frame detection, per ARMv7-M/ARMv8-M bit 4 encoding
|
|
98
|
+
|
|
99
|
+
\- `PC` and `LR` mapped to `function @ file:line` when an ELF and `arm-none-eabi-addr2line` are available
|
|
100
|
+
|
|
101
|
+
\- EXC\_RETURN LRs are never symbolized (they aren't addresses — no misleading output)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
\## Requirements
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
\- Python 3.6+, zero pip dependencies
|
|
110
|
+
|
|
111
|
+
\- Optional: `arm-none-eabi-addr2line` (GNU Arm Embedded toolchain) + an unstripped `.elf` for source mapping
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
\## Companion tools
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
\- \[`cfsr-decode`](https://github.com/GaneshMba4066/cfsr-decode) — decode the CFSR/HFSR fault \*cause\*
|
|
120
|
+
|
|
121
|
+
\- `cortex-unwind` (this tool) — decode the crash \*location\*
|
|
122
|
+
|
|
123
|
+
\- \[Production Toolkit ($49)](https://www.trustandthread.com/toolkit/) — drop-in C HardFault handlers + runtime crash logging
|
|
124
|
+
|
|
125
|
+
\- \[Firmware Crash Checklist (free)](https://www.trustandthread.com/firmware-checklist/)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
\## License
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
MIT — Trust \& Thread
|
|
134
|
+
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
\# cortex-unwind
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
Zero-dependency CLI tool to decode ARM Cortex-M hardware stack frames and map
|
|
6
|
+
|
|
7
|
+
crash sites to source lines via `arm-none-eabi-addr2line`.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
When a Cortex-M core takes a HardFault, hardware stacks 8 registers
|
|
12
|
+
|
|
13
|
+
(`r0..r3, r12, lr, pc, xPSR`) onto the active stack. Paste those 8 words from a
|
|
14
|
+
|
|
15
|
+
UART fault log or GDB (`x/8wx $sp`) and this tool gives you the crash site.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Free \& Open Source (MIT) by \[Trust \& Thread](https://www.trustandthread.com).
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
\## Installation
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
pip install cortex-unwind
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Or run with zero install:
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
curl -sL https://raw.githubusercontent.com/GaneshMba4066/cortex-unwind/main/cortex\_unwind.py | python3
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
\## Quick Start
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
Pass the 8 hardware-stacked registers (`R0` `R1` `R2` `R3` `R12` `LR` `PC` `xPSR`):
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
With an ELF file for instant symbol resolution of PC and LR:
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000 build/firmware.elf
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Got a raw hex dump from UART? Let `--auto` find the frame for you:
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
cortex-unwind --auto "0x00 0x01 0x20000000 0x40021000 0x00 0x08001255 0x08001290 0x21000000"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
\## What you get
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
\- Stacked register breakdown with Thumb-bit notes on PC/LR
|
|
72
|
+
|
|
73
|
+
\- Execution state: IPSR → active exception (HardFault, NMI, IRQn...), APSR flags
|
|
74
|
+
|
|
75
|
+
\- Thumb-bit validation (clear T-bit = corrupt PC, flagged immediately)
|
|
76
|
+
|
|
77
|
+
\- `EXC\_RETURN` decoding of LR — MSP/PSP selection and basic (8-word) vs FP-extended (26-word) frame detection, per ARMv7-M/ARMv8-M bit 4 encoding
|
|
78
|
+
|
|
79
|
+
\- `PC` and `LR` mapped to `function @ file:line` when an ELF and `arm-none-eabi-addr2line` are available
|
|
80
|
+
|
|
81
|
+
\- EXC\_RETURN LRs are never symbolized (they aren't addresses — no misleading output)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
\## Requirements
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
\- Python 3.6+, zero pip dependencies
|
|
90
|
+
|
|
91
|
+
\- Optional: `arm-none-eabi-addr2line` (GNU Arm Embedded toolchain) + an unstripped `.elf` for source mapping
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
\## Companion tools
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
\- \[`cfsr-decode`](https://github.com/GaneshMba4066/cfsr-decode) — decode the CFSR/HFSR fault \*cause\*
|
|
100
|
+
|
|
101
|
+
\- `cortex-unwind` (this tool) — decode the crash \*location\*
|
|
102
|
+
|
|
103
|
+
\- \[Production Toolkit ($49)](https://www.trustandthread.com/toolkit/) — drop-in C HardFault handlers + runtime crash logging
|
|
104
|
+
|
|
105
|
+
\- \[Firmware Crash Checklist (free)](https://www.trustandthread.com/firmware-checklist/)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
\## License
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
MIT — Trust \& Thread
|
|
114
|
+
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cortex-unwind
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: ARM Cortex-M HardFault stack frame unwinder and address mapper CLI
|
|
5
|
+
Author-email: Trust & Thread <contact@trustandthread.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.trustandthread.com
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/GaneshMba4066/cortex-unwind/issues
|
|
9
|
+
Project-URL: Documentation, https://www.trustandthread.com/firmware-checklist/
|
|
10
|
+
Keywords: arm,cortex-m,stm32,hardfault,stack,unwinder,debugging
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
15
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
16
|
+
Requires-Python: >=3.6
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE.txt
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
\# cortex-unwind
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
Zero-dependency CLI tool to decode ARM Cortex-M hardware stack frames and map
|
|
26
|
+
|
|
27
|
+
crash sites to source lines via `arm-none-eabi-addr2line`.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
When a Cortex-M core takes a HardFault, hardware stacks 8 registers
|
|
32
|
+
|
|
33
|
+
(`r0..r3, r12, lr, pc, xPSR`) onto the active stack. Paste those 8 words from a
|
|
34
|
+
|
|
35
|
+
UART fault log or GDB (`x/8wx $sp`) and this tool gives you the crash site.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Free \& Open Source (MIT) by \[Trust \& Thread](https://www.trustandthread.com).
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
\## Installation
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
pip install cortex-unwind
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Or run with zero install:
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
curl -sL https://raw.githubusercontent.com/GaneshMba4066/cortex-unwind/main/cortex\_unwind.py | python3
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
\## Quick Start
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
Pass the 8 hardware-stacked registers (`R0` `R1` `R2` `R3` `R12` `LR` `PC` `xPSR`):
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
With an ELF file for instant symbol resolution of PC and LR:
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
cortex-unwind 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000 build/firmware.elf
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
Got a raw hex dump from UART? Let `--auto` find the frame for you:
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
cortex-unwind --auto "0x00 0x01 0x20000000 0x40021000 0x00 0x08001255 0x08001290 0x21000000"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
\## What you get
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
\- Stacked register breakdown with Thumb-bit notes on PC/LR
|
|
92
|
+
|
|
93
|
+
\- Execution state: IPSR → active exception (HardFault, NMI, IRQn...), APSR flags
|
|
94
|
+
|
|
95
|
+
\- Thumb-bit validation (clear T-bit = corrupt PC, flagged immediately)
|
|
96
|
+
|
|
97
|
+
\- `EXC\_RETURN` decoding of LR — MSP/PSP selection and basic (8-word) vs FP-extended (26-word) frame detection, per ARMv7-M/ARMv8-M bit 4 encoding
|
|
98
|
+
|
|
99
|
+
\- `PC` and `LR` mapped to `function @ file:line` when an ELF and `arm-none-eabi-addr2line` are available
|
|
100
|
+
|
|
101
|
+
\- EXC\_RETURN LRs are never symbolized (they aren't addresses — no misleading output)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
\## Requirements
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
\- Python 3.6+, zero pip dependencies
|
|
110
|
+
|
|
111
|
+
\- Optional: `arm-none-eabi-addr2line` (GNU Arm Embedded toolchain) + an unstripped `.elf` for source mapping
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
\## Companion tools
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
\- \[`cfsr-decode`](https://github.com/GaneshMba4066/cfsr-decode) — decode the CFSR/HFSR fault \*cause\*
|
|
120
|
+
|
|
121
|
+
\- `cortex-unwind` (this tool) — decode the crash \*location\*
|
|
122
|
+
|
|
123
|
+
\- \[Production Toolkit ($49)](https://www.trustandthread.com/toolkit/) — drop-in C HardFault handlers + runtime crash logging
|
|
124
|
+
|
|
125
|
+
\- \[Firmware Crash Checklist (free)](https://www.trustandthread.com/firmware-checklist/)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
\## License
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
MIT — Trust \& Thread
|
|
134
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE.txt
|
|
2
|
+
README.md
|
|
3
|
+
cortex_unwind.py
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.cfg
|
|
6
|
+
cortex_unwind.egg-info/PKG-INFO
|
|
7
|
+
cortex_unwind.egg-info/SOURCES.txt
|
|
8
|
+
cortex_unwind.egg-info/dependency_links.txt
|
|
9
|
+
cortex_unwind.egg-info/entry_points.txt
|
|
10
|
+
cortex_unwind.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cortex_unwind
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
cortex-unwind: ARM Cortex-M HardFault Stack Frame Unwinder
|
|
4
|
+
Free & Open Source (MIT) by Trust & Thread — https://www.trustandthread.com
|
|
5
|
+
|
|
6
|
+
Feed it the 8-word hardware-stacked frame (r0..r3, r12, lr, pc, xPSR) from a
|
|
7
|
+
UART fault log or GDB dump, optionally an .elf, and get the crash site mapped
|
|
8
|
+
to source line via arm-none-eabi-addr2line.
|
|
9
|
+
|
|
10
|
+
Zero dependencies. Pure stdlib Python 3.6+.
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
python cortex_unwind.py <r0> <r1> <r2> <r3> <r12> <lr> <pc> <xpsr> [elf]
|
|
14
|
+
python cortex_unwind.py --auto <paste_or_hex_dump> [elf]
|
|
15
|
+
python cortex_unwind.py --example
|
|
16
|
+
"""
|
|
17
|
+
import os
|
|
18
|
+
import re
|
|
19
|
+
import shutil
|
|
20
|
+
import subprocess
|
|
21
|
+
import sys
|
|
22
|
+
|
|
23
|
+
VERSION = "1.1.0"
|
|
24
|
+
SITE = "https://www.trustandthread.com"
|
|
25
|
+
|
|
26
|
+
FRAME_REGS = ["r0", "r1", "r2", "r3", "r12", "lr", "pc", "xpsr"]
|
|
27
|
+
|
|
28
|
+
EXCEPTIONS = {
|
|
29
|
+
0: "Thread mode",
|
|
30
|
+
2: "NMI",
|
|
31
|
+
3: "HardFault",
|
|
32
|
+
4: "MemManage",
|
|
33
|
+
5: "BusFault",
|
|
34
|
+
6: "UsageFault",
|
|
35
|
+
10: "DebugMon",
|
|
36
|
+
14: "PendSV",
|
|
37
|
+
15: "SysTick",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
def parse_hex(tok, name="hex"):
|
|
41
|
+
t = tok.strip().lower().replace("0x", "").replace(",", "")
|
|
42
|
+
try:
|
|
43
|
+
return int(t, 16)
|
|
44
|
+
except ValueError:
|
|
45
|
+
sys.exit(f"error: {name} value '{tok}' is not valid hex")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def banner(ch="=", width=66):
|
|
49
|
+
return ch * width
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def addr2line(tool, elf, addr):
|
|
53
|
+
exe = shutil.which(tool)
|
|
54
|
+
if not exe:
|
|
55
|
+
return None
|
|
56
|
+
try:
|
|
57
|
+
r = subprocess.run(
|
|
58
|
+
[exe, "-f", "-C", "-e", elf, f"0x{addr:08X}"],
|
|
59
|
+
capture_output=True, text=True, timeout=10,
|
|
60
|
+
)
|
|
61
|
+
lines = [l.strip() for l in r.stdout.strip().splitlines() if l.strip()]
|
|
62
|
+
if len(lines) >= 2 and "??" not in lines[0]:
|
|
63
|
+
return lines[0], lines[1]
|
|
64
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
65
|
+
pass
|
|
66
|
+
return None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def pick_addr2line():
|
|
70
|
+
for t in ("arm-none-eabi-addr2line", "addr2line"):
|
|
71
|
+
if shutil.which(t):
|
|
72
|
+
return t
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def is_exc_return(val):
|
|
77
|
+
return (val & 0xFFFFFFF0) == 0xFFFFFFF0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def decode_xpsr(xpsr):
|
|
81
|
+
lines = []
|
|
82
|
+
ipsr = xpsr & 0x1FF
|
|
83
|
+
lines.append(f"IPSR = {ipsr}")
|
|
84
|
+
if ipsr in EXCEPTIONS:
|
|
85
|
+
lines.append(f" Active exception: {EXCEPTIONS[ipsr]}")
|
|
86
|
+
elif 16 <= ipsr < 256:
|
|
87
|
+
lines.append(f" Active IRQ: IRQ{ipsr - 16} (external interrupt)")
|
|
88
|
+
if xpsr & (1 << 24):
|
|
89
|
+
lines.append(" Thumb bit set (EPSR.T=1) — valid M-profile Thumb state")
|
|
90
|
+
else:
|
|
91
|
+
lines.append(" [!] Thumb bit CLEAR (EPSR.T=0) — invalid state or corrupt branch target")
|
|
92
|
+
flags = []
|
|
93
|
+
if xpsr & (1 << 31): flags.append("N")
|
|
94
|
+
if xpsr & (1 << 30): flags.append("Z")
|
|
95
|
+
if xpsr & (1 << 29): flags.append("C")
|
|
96
|
+
if xpsr & (1 << 28): flags.append("V")
|
|
97
|
+
if xpsr & (1 << 27): flags.append("Q")
|
|
98
|
+
if flags:
|
|
99
|
+
lines.append(f" APSR flags: {''.join(flags)}")
|
|
100
|
+
return lines
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def classify_exc_return(lr):
|
|
104
|
+
"""Decode an EXC_RETURN value. Returns (lines, uses_fp_frame) or (None, None)."""
|
|
105
|
+
if not is_exc_return(lr):
|
|
106
|
+
return None, None
|
|
107
|
+
n = lr & 0x1F
|
|
108
|
+
kind = {
|
|
109
|
+
0x01: ("Return to Handler mode using MSP (no FP)", False),
|
|
110
|
+
0x09: ("Return to Thread mode using MSP (no FP)", False),
|
|
111
|
+
0x0D: ("Return to Thread mode using PSP (no FP)", False),
|
|
112
|
+
0x11: ("Return to Handler mode using MSP (FP extended frame)", True),
|
|
113
|
+
0x19: ("Return to Thread mode using MSP (FP extended frame)", True),
|
|
114
|
+
0x1D: ("Return to Thread mode using PSP (FP extended frame)", True),
|
|
115
|
+
}
|
|
116
|
+
desc, fp = kind.get(n, (f"Variant 0x{lr & 0xFF:X}", not bool(lr & (1 << 4))))
|
|
117
|
+
lines = [f" EXC_RETURN value detected: 0x{lr:08X} ({desc})"]
|
|
118
|
+
if fp:
|
|
119
|
+
lines.append(" Extended frame: 26 words stacked (includes FP registers).")
|
|
120
|
+
else:
|
|
121
|
+
lines.append(" Basic frame: 8 words stacked.")
|
|
122
|
+
return lines, fp
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def report(regs, elf=None):
|
|
126
|
+
tool = pick_addr2line()
|
|
127
|
+
out = []
|
|
128
|
+
w = out.append
|
|
129
|
+
w(banner())
|
|
130
|
+
w(f" TRUST & THREAD :: CORTEX-M STACK FRAME UNWINDER v{VERSION}")
|
|
131
|
+
w(banner())
|
|
132
|
+
w(f" {'Reg':<6} {'Value':<12} Notes")
|
|
133
|
+
w(banner("-"))
|
|
134
|
+
lr_val = regs[5]
|
|
135
|
+
pc = regs[6] & ~1
|
|
136
|
+
exc_lines, uses_fp = classify_exc_return(lr_val)
|
|
137
|
+
if exc_lines:
|
|
138
|
+
lr_note = "(EXC_RETURN — see below)"
|
|
139
|
+
lr_sym = None
|
|
140
|
+
else:
|
|
141
|
+
lr_note = "(Thumb bit set)" if lr_val & 1 else ""
|
|
142
|
+
lr_sym = lr_val & ~1
|
|
143
|
+
for name, val in zip(FRAME_REGS, regs):
|
|
144
|
+
note = ""
|
|
145
|
+
if name == "pc" and val & 1:
|
|
146
|
+
note = "(Thumb bit set)"
|
|
147
|
+
if name == "lr":
|
|
148
|
+
note = lr_note
|
|
149
|
+
w(f" {name:<6} 0x{val:08X} {note}")
|
|
150
|
+
w(banner("-"))
|
|
151
|
+
|
|
152
|
+
w("\n [Execution State]")
|
|
153
|
+
for line in decode_xpsr(regs[7]):
|
|
154
|
+
w(" " + line)
|
|
155
|
+
|
|
156
|
+
if exc_lines:
|
|
157
|
+
w("\n [EXC_RETURN Context]")
|
|
158
|
+
for line in exc_lines:
|
|
159
|
+
w(" " + line)
|
|
160
|
+
if uses_fp:
|
|
161
|
+
w(" [!] LR indicates an FP extended frame — pass 26 words (8 + S0-S15 + FPSCR)")
|
|
162
|
+
w(" for full FP register recovery in a future release.")
|
|
163
|
+
|
|
164
|
+
w("\n [Crash Site]")
|
|
165
|
+
w(f" PC (faulting instruction): 0x{pc:08X}")
|
|
166
|
+
if elf:
|
|
167
|
+
res = addr2line(tool, elf, pc)
|
|
168
|
+
w(f" -> {res[0]} at {res[1]}" if res else " -> no symbol match (stripped ELF or invalid PC)")
|
|
169
|
+
if lr_sym is not None:
|
|
170
|
+
w(f" LR (caller return): 0x{lr_sym:08X}")
|
|
171
|
+
if elf:
|
|
172
|
+
res = addr2line(tool, elf, lr_sym)
|
|
173
|
+
w(f" -> {res[0]} at {res[1]}" if res else " -> no symbol match")
|
|
174
|
+
else:
|
|
175
|
+
w(" LR (caller return): hardware EXC_RETURN — no caller address available in this frame.")
|
|
176
|
+
w(" For the caller, unwind the next (callee-saved) frame or use the toolkit's full unwinder.")
|
|
177
|
+
|
|
178
|
+
if elf and not tool:
|
|
179
|
+
w("\n [?] Note: arm-none-eabi-addr2line not found in PATH.")
|
|
180
|
+
|
|
181
|
+
w("\n" + banner())
|
|
182
|
+
w(" NEXT STEPS & REMEDIATION:")
|
|
183
|
+
w(f" 1. Free crash triage guide: {SITE}/firmware-checklist/")
|
|
184
|
+
w(f" 2. Drop-in C HardFault handler & runtime crash logger: {SITE}/toolkit/")
|
|
185
|
+
w(banner() + "\n")
|
|
186
|
+
print("\n".join(out))
|
|
187
|
+
return 0
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
EXAMPLE = """
|
|
191
|
+
Examples:
|
|
192
|
+
# Basic decode of 8 stacked registers:
|
|
193
|
+
python cortex_unwind.py 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000
|
|
194
|
+
|
|
195
|
+
# Auto-resolve symbols from an ELF:
|
|
196
|
+
python cortex_unwind.py 0x0 0x1 0x20000000 0x40021000 0x0 0x08001255 0x08001290 0x21000000 build/firmware.elf
|
|
197
|
+
|
|
198
|
+
# EXC_RETURN in LR (typical HardFault from an ISR):
|
|
199
|
+
python cortex_unwind.py 0x0 0x1 0x20000000 0x40021000 0x0 0xFFFFFFF9 0x08001290 0x21000000
|
|
200
|
+
|
|
201
|
+
# Auto-extract from raw memory dump:
|
|
202
|
+
python cortex_unwind.py --auto "0x00 0x01 0x20000000 0x40021000 0x00 0x08001255 0x08001290 0x21000000"
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def auto_extract(tokens):
|
|
207
|
+
# A single quoted argument may contain the whole dump — split on whitespace.
|
|
208
|
+
split = []
|
|
209
|
+
for t in tokens:
|
|
210
|
+
split.extend(t.replace(",", " ").split())
|
|
211
|
+
hexish = [t for t in split if re.fullmatch(r"(?:0[xX])?[0-9a-fA-F]{1,8}", t)]
|
|
212
|
+
if len(hexish) >= 8:
|
|
213
|
+
return [parse_hex(t, "frame") for t in hexish[-8:]]
|
|
214
|
+
return None
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def main():
|
|
218
|
+
args = [a for a in sys.argv[1:] if a != "--"]
|
|
219
|
+
if not args or args[0] in ("-h", "--help"):
|
|
220
|
+
print(__doc__.strip())
|
|
221
|
+
print(EXAMPLE)
|
|
222
|
+
return 0
|
|
223
|
+
if args[0] in ("-v", "--version"):
|
|
224
|
+
print(f"cortex-unwind v{VERSION}")
|
|
225
|
+
return 0
|
|
226
|
+
if args[0] == "--example":
|
|
227
|
+
print(EXAMPLE)
|
|
228
|
+
return 0
|
|
229
|
+
|
|
230
|
+
elf = None
|
|
231
|
+
frame = None
|
|
232
|
+
if args[0] == "--auto":
|
|
233
|
+
rest = args[1:]
|
|
234
|
+
for item in rest:
|
|
235
|
+
if item.endswith(".elf") and os.path.isfile(item):
|
|
236
|
+
elf = item
|
|
237
|
+
frame = auto_extract(rest)
|
|
238
|
+
if not frame:
|
|
239
|
+
sys.exit("error: --auto could not find 8 consecutive 32-bit hex values")
|
|
240
|
+
else:
|
|
241
|
+
if len(args) == 9 and args[8].endswith(".elf"):
|
|
242
|
+
elf = args[8]
|
|
243
|
+
args = args[:8]
|
|
244
|
+
if len(args) != 8:
|
|
245
|
+
print(__doc__.strip())
|
|
246
|
+
print(EXAMPLE)
|
|
247
|
+
return 1
|
|
248
|
+
frame = [parse_hex(a, FRAME_REGS[i]) for i, a in enumerate(args)]
|
|
249
|
+
return report(frame, elf)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
if __name__ == "__main__":
|
|
253
|
+
sys.exit(main())
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cortex-unwind"
|
|
7
|
+
version = "1.1.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Trust & Thread", email = "contact@trustandthread.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "ARM Cortex-M HardFault stack frame unwinder and address mapper CLI"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.6"
|
|
14
|
+
license = { text = "MIT" }
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Topic :: Software Development :: Embedded Systems",
|
|
20
|
+
"Topic :: Software Development :: Debuggers",
|
|
21
|
+
]
|
|
22
|
+
keywords = ["arm", "cortex-m", "stm32", "hardfault", "stack", "unwinder", "debugging"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
"Homepage" = "https://www.trustandthread.com"
|
|
26
|
+
"Bug Tracker" = "https://github.com/GaneshMba4066/cortex-unwind/issues"
|
|
27
|
+
"Documentation" = "https://www.trustandthread.com/firmware-checklist/"
|
|
28
|
+
|
|
29
|
+
[project.scripts]
|
|
30
|
+
cortex-unwind = "cortex_unwind:main"
|