Skip to content

Commit d391d66

Browse files
committed
LLDB -> GDB; +setup instructions for Windows and mac; -cargo-edit
this commit: - changes the debugger to GDB since Windows and macOS users reported errors with LLDB - adds setup instructions for Windows and macOS - drops the cargo-edit dependency since it's used only once and lets us simplify the setup instructions - adds a note regarding .cargo/config being required in section 3 since some people missed it while reading section 2 - removes a note about llvm-objdump not working correctly since the problem was fixed in cargo-binutils v0.1.4 closes #8 closes #7
1 parent 7861c4a commit d391d66

File tree

6 files changed

+229
-117
lines changed

6 files changed

+229
-117
lines changed

ci/exceptions/app/app.objdump

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
app: file format ELF32-arm-little
3+
4+
Disassembly of section .text:
5+
main:
6+
40: trap
7+
42: trap
8+
9+
Reset:
10+
44: movw r1, #0x0
11+
48: movw r0, #0x0
12+
4c: movt r1, #0x2000
13+
50: movt r0, #0x2000
14+
54: subs r1, r1, r0
15+
56: bl #0xd2
16+
5a: movw r1, #0x0
17+
5e: movw r0, #0x0
18+
62: movt r1, #0x2000
19+
66: movt r0, #0x2000
20+
6a: subs r2, r1, r0
21+
6c: movw r1, #0x0
22+
70: movt r1, #0x0
23+
74: bl #0x8
24+
78: bl #-0x3c
25+
7c: trap
26+
27+
DefaultExceptionHandler:
28+
7e: b #-0x4 <DefaultExceptionHandler>
29+
30+
UsageFault:
31+
7f: sub sp, #0x19c
32+
33+
__aeabi_memcpy:
34+
80: push {r4, r5, r7, lr}
35+
82: cbz r2, #0x56
36+
84: subs r3, r2, #0x1
37+
86: and r12, r2, #0x3
38+
8a: cmp r3, #0x3
39+
8c: bhs #0x8 <__aeabi_memcpy+0x18>
40+
8e: movs r2, #0x0
41+
90: cmp.w r12, #0x0
42+
94: bne #0x26 <__aeabi_memcpy+0x3e>
43+
96: b #0x42 <__aeabi_memcpy+0x5c>
44+
98: sub.w lr, r2, r12
45+
9c: movs r2, #0x0
46+
9e: ldrb r3, [r1, r2]
47+
a0: adds r4, r1, r2
48+
a2: strb r3, [r0, r2]
49+
a4: adds r3, r0, r2
50+
a6: adds r2, #0x4
51+
a8: ldrb r5, [r4, #0x1]
52+
aa: cmp lr, r2
53+
ac: strb r5, [r3, #0x1]
54+
ae: ldrb r5, [r4, #0x2]
55+
b0: strb r5, [r3, #0x2]
56+
b2: ldrb r4, [r4, #0x3]
57+
b4: strb r4, [r3, #0x3]
58+
b6: bne #-0x1c <__aeabi_memcpy+0x1e>
59+
b8: cmp.w r12, #0x0
60+
bc: beq #0x1c <__aeabi_memcpy+0x5c>
61+
be: ldrb r3, [r1, r2]
62+
c0: cmp.w r12, #0x1
63+
c4: strb r3, [r0, r2]
64+
c6: beq #0x12 <__aeabi_memcpy+0x5c>
65+
c8: adds r3, r2, #0x1
66+
ca: cmp.w r12, #0x2
67+
ce: ldrb r5, [r1, r3]
68+
d0: strb r5, [r0, r3]
69+
d2: it eq
70+
d4: popeq {r4, r5, r7, pc}
71+
d6: adds r2, #0x2
72+
d8: ldrb r1, [r1, r2]
73+
da: strb r1, [r0, r2]
74+
dc: pop {r4, r5, r7, pc}
75+
76+
__aeabi_memset:
77+
de: cmp r1, #0x0
78+
e0: it eq
79+
e2: bxeq lr
80+
e4: push {r7, lr}
81+
e6: subs r3, r1, #0x1
82+
e8: and r12, r1, #0x3
83+
ec: cmp r3, #0x3
84+
ee: bhs #0x2 <__aeabi_memset+0x16>
85+
f0: movs r1, #0x0
86+
f2: b #0x14 <__aeabi_memset+0x2c>
87+
f4: sub.w lr, r1, r12
88+
f8: movs r1, #0x0
89+
fa: strb r2, [r0, r1]
90+
fc: adds r3, r0, r1
91+
fe: adds r1, #0x4
92+
100: cmp lr, r1
93+
102: strb r2, [r3, #0x1]
94+
104: strb r2, [r3, #0x2]
95+
106: strb r2, [r3, #0x3]
96+
108: bne #-0x12 <__aeabi_memset+0x1c>
97+
10a: cmp.w r12, #0x0
98+
10e: pop.w {r7, lr}
99+
112: itt ne
100+
114: strbne r2, [r0, r1]
101+
116: cmpne.w r12, #0x1
102+
11a: bne #0x0 <__aeabi_memset+0x40>
103+
11c: bx lr
104+
11e: add r0, r1
105+
120: cmp.w r12, #0x2
106+
124: strb r2, [r0, #0x1]
107+
126: it ne
108+
128: strbne r2, [r0, #0x2]
109+
12a: bx lr
110+
111+
__aeabi_memclr:
112+
12c: movs r2, #0x0
113+
12e: b.w #-0x54 <__aeabi_memset>

ci/script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ main() {
7979

8080
# check that the disassembly matches
8181
pushd app
82+
diff app.objdump \
83+
<(cargo objdump --bin app --release -- -d -no-show-raw-insn -print-imm-hex)
8284
diff app.vector_table.objdump \
8385
<(cargo objdump --bin app --release -- -s -j .vector_table)
8486
edition_check

src/exceptions.md

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -117,57 +117,39 @@ application:
117117
```
118118

119119
``` console
120-
(lldb) b DefaultExceptionHandler
121-
Breakpoint 1: where = app`DefaultExceptionHandler at lib.rs:95, address = 0x000000ec
120+
(gdb) target remote :3333
121+
Remote debugging using :3333
122+
Reset () at ../rt/src/lib.rs:7
123+
7 pub unsafe extern "C" fn Reset() -> ! {
122124

123-
(lldb) continue
124-
Process 1 resuming
125-
Process 1 stopped
126-
* thread #1, stop reason = breakpoint 1.1
127-
frame #0: 0x000000ec app`DefaultExceptionHandler at lib.rs:95
128-
92
129-
93 #[no_mangle]
130-
94 pub extern "C" fn DefaultExceptionHandler() {
131-
-> 95 loop {}
132-
96 }
125+
(gdb) b DefaultExceptionHandler
126+
Breakpoint 1 at 0xec: file ../rt/src/lib.rs, line 95.
127+
128+
(gdb) continue
129+
Continuing.
130+
131+
Breakpoint 1, DefaultExceptionHandler ()
132+
at ../rt/src/lib.rs:95
133+
95 loop {}
134+
135+
(gdb) list
136+
90 Vector { handler: SysTick },
137+
91 ];
138+
92
139+
93 #[no_mangle]
140+
94 pub extern "C" fn DefaultExceptionHandler() {
141+
95 loop {}
142+
96 }
133143
```
134144

135145
And for completeness, here's the disassembly of the optimized version of the program:
136146

137147
``` console
138-
$ cargo objdump --bin app --release -- -d
148+
$ cargo objdump --bin app --release -- -d -no-show-raw-insn -print-imm-hex
139149
```
140150

141-
> **NOTE** `llvm-objdump`, which is what `cargo-objdump` invokes, produces
142-
> broken output for this particular file so the output below is actually the
143-
> output from `arm-none-eabi-objdump`
144-
145-
146151
``` text
147-
00000040 <main>:
148-
40: defe udf #254 ; 0xfe
149-
42: defe udf #254 ; 0xfe
150-
151-
00000044 <Reset>:
152-
44: f240 0100 movw r1, #0
153-
48: f240 0000 movw r0, #0
154-
4c: f2c2 0100 movt r1, #8192 ; 0x2000
155-
50: f2c2 0000 movt r0, #8192 ; 0x2000
156-
54: 1a09 subs r1, r1, r0
157-
56: f000 f869 bl 12c <__aeabi_memclr>
158-
5a: f240 0100 movw r1, #0
159-
5e: f240 0000 movw r0, #0
160-
62: f2c2 0100 movt r1, #8192 ; 0x2000
161-
66: f2c2 0000 movt r0, #8192 ; 0x2000
162-
6a: 1a0a subs r2, r1, r0
163-
6c: f240 1132 movw r1, #306 ; 0x132
164-
70: f2c0 0100 movt r1, #0
165-
74: f000 f804 bl 80 <__aeabi_memcpy>
166-
78: f7ff ffe2 bl 40 <main>
167-
7c: defe udf #254 ; 0xfe
168-
169-
0000007e <DefaultExceptionHandler>:
170-
7e: e7fe b.n 7e <DefaultExceptionHandler>
152+
{{#include ../ci/exceptions/app/app.objdump:1:28}}
171153
```
172154

173155
``` console
@@ -221,18 +203,28 @@ matches the name we used in `EXCEPTIONS`.
221203
You can test it in QEMU
222204

223205
``` console
224-
(lldb) b HardFault
225-
226-
(lldb) continue
227-
Process 1 resuming
228-
Process 1 stopped
229-
* thread #1, stop reason = breakpoint 1.1
230-
frame #0: 0x00000044 app`HardFault at main.rs:18
231-
15 #[no_mangle]
232-
16 pub extern "C" fn HardFault() -> ! {
233-
17 // do something interesting here
234-
-> 18 loop {}
235-
19 }
206+
(gdb) target remote :3333
207+
Remote debugging using :3333
208+
Reset () at /home/japaric/rust/embedonomicon/ci/exceptions/rt/src/lib.rs:7
209+
7 pub unsafe extern "C" fn Reset() -> ! {
210+
211+
(gdb) b HardFault
212+
Breakpoint 1 at 0x44: file src/main.rs, line 18.
213+
214+
(gdb) continue
215+
Continuing.
216+
217+
Breakpoint 1, HardFault () at src/main.rs:18
218+
18 loop {}
219+
220+
(gdb) list
221+
13 }
222+
14
223+
15 #[no_mangle]
224+
16 pub extern "C" fn HardFault() -> ! {
225+
17 // do something interesting here
226+
18 loop {}
227+
19 }
236228
```
237229

238230
The program now executes the user defined `HardFault` function instead of the

src/main.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ $ cargo new --edition 2018 --bin app
6767

6868
$ cd app
6969

70-
$ cargo add rt --path ../rt
70+
$ # modify Cargo.toml to include the `rt` crate as a dependency
71+
$ tail -n2 Cargo.toml
72+
```
7173

74+
``` toml
75+
{{#include ../ci/main/app/src/Cargo.toml:7:8}}
76+
```
77+
78+
``` console
7279
$ # copy over the config file that sets a default target and tweaks the linker invocation
7380
$ cp -r ../rt/.cargo .
7481

src/memory-layout.md

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ We have to tweak linker process to make it use our linker script. This is done
174174
passing the `-C link-arg` flag to `rustc` but there are two ways to do it: you
175175
can use the `cargo-rustc` subcommand instead of `cargo-build` as shown below:
176176

177+
**IMPORTANT**: Make sure you have the `.cargo/config` file that was added at the
178+
end of the last section before running this command.
179+
177180
``` console
178181
$ cargo rustc -- -C link-arg=-Tlink.x
179182
```
@@ -241,51 +244,30 @@ $ qemu-system-arm \
241244

242245
``` console
243246
$ # on a different terminal
244-
$ lldb target/thumbv7m-none-eabi/debug/app
245-
246-
(lldb) gdb-remote 3333
247-
Process 1 stopped
248-
* thread #1, stop reason = signal SIGTRAP
249-
frame #0: 0x00000008 app`Reset at main.rs:22
250-
19
251-
20 #[panic_handler]
252-
21 fn panic(_panic: &PanicInfo<'_>) -> ! {
253-
-> 22 loop {}
254-
23 }
255-
256-
(lldb) # ^ that source is wrong; the processor is about to execute Reset; see below
257-
(lldb) disassemble -frame
258-
app`Reset:
259-
-> 0x8 <+0>: sub sp, #0x4
260-
0xa <+2>: movs r0, #0x2a
261-
0xc <+4>: str r0, [sp]
262-
0xe <+6>: b 0x10 ; <+8> at main.rs:12
263-
0x10 <+8>: b 0x10 ; <+8> at main.rs:12
264-
265-
(lldb) # the SP has the initial value we programmed in the vector table
266-
(lldb) print/x $sp
267-
(unsigned int) $0 = 0x20010000
268-
269-
(lldb) step
270-
(lldb) step
271-
(lldb) step
272-
Process 1 stopped
273-
* thread #1, stop reason = step in
274-
frame #0: 0x0000000e app`Reset at main.rs:13
275-
10 let _x = 42;
276-
11
277-
12 // can't return so we go into an infinite loop here
278-
-> 13 loop {}
279-
14 }
280-
15
281-
16 // The reset vector, a pointer into the reset handler
282-
283-
(lldb) # next we inspect the stack variable `_x`
284-
(lldb) print _x
285-
(int) $1 = 42
286-
287-
(lldb) print &_x
288-
(int *) $2 = 0x2000fffc
289-
290-
(lldb) exit
247+
$ arm-none-eabi-gdb -q target/thumbv7m-none-eabi/debug/app
248+
Reading symbols from target/thumbv7m-none-eabi/debug/app...done.
249+
250+
(gdb) target remote :3333
251+
Remote debugging using :3333
252+
Reset () at src/main.rs:8
253+
8 pub unsafe extern "C" fn Reset() -> ! {
254+
255+
(gdb) # the SP has the initial value we programmed in the vector table
256+
(gdb) print/x $sp
257+
$1 = 0x20010000
258+
259+
(gdb) step
260+
9 let _x = 42;
261+
262+
(gdb) step
263+
12 loop {}
264+
265+
(gdb) # next we inspect the stack variable `_x`
266+
(gdb) print _x
267+
$2 = 42
268+
269+
(gdb) print &_x
270+
$3 = (i32 *) 0x2000fffc
271+
272+
(gdb) quit
291273
```

0 commit comments

Comments
 (0)