1python
2import os, subprocess
3kdir = os.environ['KERNELDIR']
4mdir = os.environ['MODULEDIR'] or '/lib/modules'
5gdb.execute(f'add-auto-load-safe-path {kdir}/scripts/gdb/')
6cwd=os.getcwd()
7gdb.execute(f'cd {kdir}')
8gdb.execute(f'source {kdir}/vmlinux-gdb.py')
9p = subprocess.run([f'./linux', '--version'], capture_output=True)
10ver = p.stdout.strip().decode('ascii')
11gdb.execute(f'cd {cwd}')
12end
13break os_early_checks
14commands
15silent
16python
17gdb.execute(f'cd {kdir}')
18gdb.execute(f'lx-symbols {mdir}/{ver}/')
19gdb.execute(f'cd {cwd}')
20end
21# only once
22del 1
23continue
24end
25handle 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#
54break init_new_ldt
55commands
56silent
57continue
58end
59
60echo \n
61echo Welcome to hwsim kernel debugging\n
62echo ---------------------------------\n\n
63echo You can install breakpoints in modules, they're treated\n
64echo as shared libraries, so just say 'y' if asked to make the\n
65echo breakpoint pending on future load.\n\n
66echo Do NOT, however, delete the breakpoint on 'init_new_ldt'!\n\n
67echo Now enter 'run' to start the run.\n\n
68echo Have fun!\n\n
69