1 python
2 import os, subprocess
3 kdir = os.environ['KERNELDIR']
4 mdir = os.environ['MODULEDIR'] or '/lib/modules'
5 gdb.execute(f'add-auto-load-safe-path {kdir}/scripts/gdb/')
6 cwd=os.getcwd()
7 gdb.execute(f'cd {kdir}')
8 gdb.execute(f'source {kdir}/vmlinux-gdb.py')
9 p = subprocess.run([f'./linux', '--version'], capture_output=True)
10 ver = p.stdout.strip().decode('ascii')
11 gdb.execute(f'cd {cwd}')
12 end
13 break os_early_checks
14 commands
15 silent
16 python
17 gdb.execute(f'cd {kdir}')
18 gdb.execute(f'lx-symbols {mdir}/{ver}/')
19 gdb.execute(f'cd {cwd}')
20 end
21 # only once
22 del 1
23 continue
24 end
25 handle 11 nostop noprint pass
26 #
27 # So ... this is complicated. When gdb installs a regular breakpoint
28 # on some place, it writes there a breakpoint instruction (which is
29 # a single 0xCC byte on x86). This breaks out into the debugger and
30 # it can then restart/simulate the correct instruction when continuing
31 # across the breakpoint.
32 #
33 # Additionally, gdb (correctly) removes these breakpoint instructions
34 # from forked children when detaching from them. This also seems fine.
35 #
36 # However, due to how user-mode-linux works, this causes issues with
37 # kernel modules. These are loaded into the vmalloc area, and even if
38 # that isn't quite part of physmem, it's still mapped as MAP_SHARED.
39 #
40 # Unfortunately, this means that gdb deletes breakpoints in modules
41 # when a new userspace process is started, since that causes a new
42 # process to be created by clone() and gdb has to detach from it.
43 #
44 # The other thing to know is that when gdb hits a breakpoint it will
45 # restore all the code to normal, and reinstall breakpoints when we
46 # continue.
47 #
48 # Thus we can use that behaviour to work around the module issue:
49 # simply put a breakpoint on init_new_ldt which happens just after
50 # the clone() for a new userspace process, and do nothing there but
51 # continue, which reinstalls all breakpoints, including the ones in
52 # modules.
53 #
54 break init_new_ldt
55 commands
56 silent
57 continue
58 end
59 
60 echo \n
61 echo Welcome to hwsim kernel debugging\n
62 echo ---------------------------------\n\n
63 echo You can install breakpoints in modules, they're treated\n
64 echo as shared libraries, so just say 'y' if asked to make the\n
65 echo breakpoint pending on future load.\n\n
66 echo Do NOT, however, delete the breakpoint on 'init_new_ldt'!\n\n
67 echo Now enter 'run' to start the run.\n\n
68 echo Have fun!\n\n
69