安装开发环境

1
sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu

理解框架代码的编译

  • 输出到vim中
    • make nB | vim -
  • 在 Vim 里做一些你舒适的文本处理
    • :%s/^/\r 在命令之间插入空行
    • :%s/ /\r /g 将命令的参数缩进排版
  • 另存为新文件
    • :w filename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
riscv64-linux-gnu-gcc 
-c
-o
kernel/entry.o
kernel/entry.S

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/start.o
kernel/start.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/console.o
kernel/console.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/printf.o
kernel/printf.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/uart.o
kernel/uart.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/kalloc.o
kernel/kalloc.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/spinlock.o
kernel/spinlock.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/string.o
kernel/string.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/main.o
kernel/main.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/vm.o
kernel/vm.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/proc.o
kernel/proc.c

riscv64-linux-gnu-gcc
-c
-o
kernel/swtch.o
kernel/swtch.S

riscv64-linux-gnu-gcc
-c
-o
kernel/trampoline.o
kernel/trampoline.S

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/trap.o
kernel/trap.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/syscall.o
kernel/syscall.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/sysproc.o
kernel/sysproc.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/bio.o
kernel/bio.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/fs.o
kernel/fs.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/log.o
kernel/log.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/sleeplock.o
kernel/sleeplock.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/file.o
kernel/file.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/pipe.o
kernel/pipe.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/exec.o
kernel/exec.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/sysfile.o
kernel/sysfile.c

riscv64-linux-gnu-gcc
-c
-o
kernel/kernelvec.o
kernel/kernelvec.S

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/plic.o
kernel/plic.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-c
-o
kernel/virtio_disk.o
kernel/virtio_disk.c

riscv64-linux-gnu-gcc
-Wall
-Werror
-O
-fno-omit-frame-pointer
-ggdb
-DSOL_UTIL
-MD
-mcmodel=medany
-ffreestanding
-fno-common
-nostdlib
-mno-relax
-I.
-fno-stack-protector
-fno-pie
-no-pie
-march=rv64g
-nostdinc
-I.
-Ikernel
-c
user/initcode.S
-o
user/initcode.o

riscv64-linux-gnu-ld
-z
max-page-size=4096
-N
-e
start
-Ttext
0
-o
user/initcode.out
user/initcode.o

riscv64-linux-gnu-objcopy
-S
-O
binary
user/initcode.out
user/initcode

riscv64-linux-gnu-objdump
-S
user/initcode.o
>
user/initcode.asm

riscv64-linux-gnu-ld
-z
max-page-size=4096
-T
kernel/kernel.ld
-o
kernel/kernel
kernel/entry.o
kernel/start.o
kernel/console.o
kernel/printf.o
kernel/uart.o
kernel/kalloc.o
kernel/spinlock.o
kernel/string.o
kernel/main.o
kernel/vm.o
kernel/proc.o
kernel/swtch.o
kernel/trampoline.o
kernel/trap.o
kernel/syscall.o
kernel/sysproc.o
kernel/bio.o
kernel/fs.o
kernel/log.o
kernel/sleeplock.o
kernel/file.o
kernel/pipe.o
kernel/exec.o
kernel/sysfile.o
kernel/kernelvec.o
kernel/plic.o
kernel/virtio_disk.o



riscv64-linux-gnu-objdump
-S
kernel/kernel
>
kernel/kernel.asm

riscv64-linux-gnu-objdump
-t
kernel/kernel
|
sed
'1,/SYMBOL
TABLE/d;
s/
.*
/
/;
/^$/d'
>
kernel/kernel.sym

gdb调试

makefile中的定义的make commend

1
2
3
4
5
6
7
8
9
10
11
12
qemu: $K/kernel fs.img
$(QEMU) $(QEMUOPTS)

.gdbinit: .gdbinit.tmpl-riscv
sed "s/:1234/:$(GDBPORT)/" < $^ > $@

qemu-gdb: $K/kernel .gdbinit fs.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)

print-gdbport:
@echo $(GDBPORT)

终端1

1
2
3
4
grand@Lubuntu ~/xv6-labs-2020 (util)> make qemu-gdb
sed "s/:1234/:26000/" < .gdbinit.tmpl-riscv > .gdbinit
*** Now run 'gdb' in another window.
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -S -gdb tcp::26000

或者make CPUS=1 qemu-gdb,Makefile中的CPUS即qemu -smp $(CPUS),cpu核数,设置为1时,单线程,方便调试

终端2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
grand@Lubuntu ~/xv6-labs-2020 (util)> gdb-multiarch kernel/kernel
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from kernel/kernel...
warning: File "/home/grand/xv6-labs-2020/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
add-auto-load-safe-path /home/grand/xv6-labs-2020/.gdbinit
line to your configuration file "/home/grand/.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "/home/grand/.gdbinit".
For more information about this security protection see the
--Type <RET> for more, q to quit, c to continue without paging--

按q回车退出,在/home/grand/.gdbinit中添加add-auto-load-safe-path /home/grand/xv6-labs-2020/.gdbinit

1
grand@Lubuntu ~/xv6-labs-2020 (util) [1]> echo "add-auto-load-safe-path /home/grand/xv6-labs-2020/.gdbinit" >> "/home/grand/.gdbinit"

重新运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
grand@Lubuntu ~/xv6-labs-2020 (util)> gdb-multiarch kernel/kernel
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from kernel/kernel...
The target architecture is assumed to be riscv:rv64
0x0000000000001000 in ?? ()
(gdb)

现在就可以去调试了,快来试试下面的命令,了解熟悉gdb的基本调试操作。

gdb常用命令

tui界面相关的(Text User Interface commands)

  • tui enable 启动tui界面,默认显示源码
  • tui disable 关闭tui界面
  • layout asm 启动tui界面,查看汇编代码
  • layout src 启动tui界面,查看源码
  • layout split 启动tui界面,同时查看源码和汇编
  • layout reg 启动tui节目,查看寄存器
  • focus [win] 多个窗口时,可以聚焦到某一窗口,例如focus src, focus asm, focus reg

执行相关的

  • si 执行一条汇编指令
  • ni 执行一条汇编指令,若是跳转指令,则执行完返回。
  • s 执行一行代码,进入函数,step
  • n 执行一行代码,不进入函数,next
  • c 继续运行,遇到断点暂停,continue
  • r 执行整个程序,遇到断点暂停,run
  • finish 函数结束,遇到断点暂停

断点

  • b [func] 添加断点,如b main
  • b [line] 添加断点,如b 17
  • b filename:[line/func] 添加断点,如b kernel/main.c:15
  • i breakpoints 查看断点的信息
  • d [num] 删除断点/观察点,如b 1,不填num时,删除所有的断点

观察点

  • watch [val] 设置观察点,当某个变量的值发生变化时,暂停提示
  • watch [expr] 设置观察点,当表达式发生变化时,暂停提示
  • d [num] 删除断点/观察点

查看

  • p [var] 打印某个变量
  • i locals 查看局部变量
  • i args 查看函数的入参
  • i frame 查看当前栈帧信息
  • bt/basktrace 查看所有栈帧的位置
  • frame [num] 查看某个栈帧的信息

vscode调试

ssh远程链接

  • 在vscode安装ssh插件,在ubuntu安装openssh-server
    • sudo apt install openssh-server
      • 出错了先按照提示安装依赖,sudo apt-get install openssh-client=1:8.2p1-4
      • 再重新安装
  • 给ubuntu添加一个网络适配器,设定为仅主机模式
  • 在ubuntu给该网卡设定静态ip
  • 最后在vscode登录,username@ip,输入密码

vscode配置教程

调试手册和实验手册