1 # GDB Macros User Guide {#gdb_macros}
5 When debugging an spdk application using gdb we may need to view data structures
6 in lists, e.g. information about bdevs or threads.
8 If, for example I have several bdevs, and I wish to get information on bdev by
9 the name 'test_vols3', I will need to manually iterate over the list as follows:
12 (gdb) p g_bdev_mgr->bdevs->tqh_first->name
13 $5 = 0x7f7dcc0b21b0 "test_vols1"
14 (gdb) p g_bdev_mgr->bdevs->tqh_first->internal->link->tqe_next->name
15 $6 = 0x7f7dcc0b1a70 "test_vols2"
17 g_bdev_mgr->bdevs->tqh_first->internal->link->tqe_next->internal->link->tqe_next->name
18 $7 = 0x7f7dcc215a00 "test_vols3"
20 g_bdev_mgr->bdevs->tqh_first->internal->link->tqe_next->internal->link->tqe_next
21 $8 = (struct spdk_bdev *) 0x7f7dcc2c7c08
24 At this stage, we can start looking at the relevant fields of our bdev which now
25 we know is in address 0x7f7dcc2c7c08.
27 This can be somewhat troublesome if there are 100 bdevs, and the one we need is
30 Instead, we can use a gdb macro in order to get information about all the
38 (gdb) spdk_print_bdevs
40 SPDK object of type struct spdk_bdev at 0x7f7dcc1642a8
41 ((struct spdk_bdev*) 0x7f7dcc1642a8)
42 name 0x7f7dcc0b21b0 "test_vols1"
46 SPDK object of type struct spdk_bdev at 0x7f7dcc216008
47 ((struct spdk_bdev*) 0x7f7dcc216008)
48 name 0x7f7dcc0b1a70 "test_vols2"
52 SPDK object of type struct spdk_bdev at 0x7f7dcc2c7c08
53 ((struct spdk_bdev*) 0x7f7dcc2c7c08)
54 name 0x7f7dcc215a00 "test_vols3"
59 Finding a bdev by name:
62 (gdb) spdk_find_bdev test_vols1
65 SPDK object of type struct spdk_bdev at 0x7f7dcc1642a8
66 ((struct spdk_bdev*) 0x7f7dcc1642a8)
67 name 0x7f7dcc0b21b0 "test_vols1"
70 Printing spdk threads:
73 (gdb) spdk_print_threads
75 SPDK object of type struct spdk_thread at 0x7fffd0008b50
76 ((struct spdk_thread*) 0x7fffd0008b50)
77 name 0x7fffd00008e0 "reactor_1"
79 SPDK object of type struct spdk_io_channel at 0x7fffd0052610
80 ((struct spdk_io_channel*) 0x7fffd0052610)
83 device 0x7fffd0008c80 (0x7fffd0008ce0 "nvmf_tgt")
86 SPDK object of type struct spdk_io_channel at 0x7fffd0056cd0
87 ((struct spdk_io_channel*) 0x7fffd0056cd0)
90 device 0x7fffd0056bf0 (0x7fffd0008e70 "test_vol1")
93 SPDK object of type struct spdk_io_channel at 0x7fffd00582e0
94 ((struct spdk_io_channel*) 0x7fffd00582e0)
97 device 0x7fffd0056c50 (0x7fffd0056cb0 "bdev_test_vol1")
100 SPDK object of type struct spdk_io_channel at 0x7fffd00583b0
101 ((struct spdk_io_channel*) 0x7fffd00583b0)
104 device 0x7fffd0005630 (0x7fffd0005690 "bdev_mgr")
108 Printing nvmf subsystems:
111 (gdb) spdk_print_nvmf_subsystems
113 SPDK object of type struct spdk_nvmf_subsystem at 0x7fffd0008d00
114 ((struct spdk_nvmf_subsystem*) 0x7fffd0008d00)
115 name "nqn.2014-08.org.nvmexpress.discovery", '\000' <repeats 187 times>
116 nqn "nqn.2014-08.org.nvmexpress.discovery", '\000' <repeats 187 times>
121 SPDK object of type struct spdk_nvmf_subsystem at 0x7fffd0055760
122 ((struct spdk_nvmf_subsystem*) 0x7fffd0055760)
123 name "nqn.2016-06.io.spdk.umgmt:cnode1", '\000' <repeats 191 times>
124 nqn "nqn.2016-06.io.spdk.umgmt:cnode1", '\000' <repeats 191 times>
128 # Loading The gdb Macros
130 Copy the gdb macros to the host where you are about to debug.
131 It is best to copy the file either to somewhere within the PYTHONPATH, or to add
132 the destination directory to the PYTHONPATH. This is not mandatory, and can be
133 worked around, but can save a few steps when loading the module to gdb.
135 From gdb, with the application core open, invoke python and load the modules.
137 In the example below, I copied the macros to the /tmp directory which is not in
138 the PYTHONPATH, so I had to manually add the directory to the path.
143 >sys.path.append('/tmp')
146 (gdb) spdk_load_macros
149 # Using the gdb Data Directory
151 On most systems, the data directory is /usr/share/gdb. The python script should
152 be copied into the python/gdb/function (or python/gdb/command) directory under
153 the data directory, e.g. /usr/share/gdb/python/gdb/function.
155 If the python script is in there, then the only thing you need to do when
156 starting gdb is type "spdk_load_macros".
158 # Using .gdbinit To Load The Macros
160 .gdbinit can also be used in order to run automatically run the manual steps
161 above prior to starting gdb.
166 source /opt/km/install/tools/gdb_macros/gdb_macros.py
169 When starting gdb you still have to call spdk_load_macros.
171 # Why Do We Need to Explicitly Call spdk_load_macros
173 The reason is that the macros need to use globals provided by spdk in order to
174 iterate the spdk lists and build iterable representations of the list objects.
175 This will result in errors if these are not available which is very possible if
176 gdb is used for reasons other than debugging spdk core dumps.
178 In the example bellow, I attempted to load the macros when the globals are not
179 available causing gdb to fail loading the gdb_macros:
182 (gdb) spdk_load_macros
183 Traceback (most recent call last):
184 File "/opt/km/install/tools/gdb_macros/gdb_macros.py", line 257, in invoke
186 File "/opt/km/install/tools/gdb_macros/gdb_macros.py", line 241, in __init__
187 threads = SpdkThreads()
188 File "/opt/km/install/tools/gdb_macros/gdb_macros.py", line 234, in __init__
189 super(SpdkThreads, self).__init__('g_threads', SpdkThread)
190 File "/opt/km/install/tools/gdb_macros/gdb_macros.py", line 25, in __init__
192 File "/opt/km/install/tools/gdb_macros/gdb_macros.py", line 10, in __init__
193 self.list = gdb.parse_and_eval(self.list_pointer)
194 RuntimeError: No symbol table is loaded. Use the "file" command.
195 Error occurred in Python command: No symbol table is loaded. Use the "file"
201 - spdk_load_macros: load the macros (use --reload in order to reload them)
202 - spdk_print_bdevs: information about bdevs
203 - spdk_find_bdev: find a bdev (substring search)
204 - spdk_print_io_devices: information about io devices
205 - spdk_print_nvmf_subsystems: information about nvmf subsystems
206 - spdk_print_threads: information about threads
210 The list iteration macros are usually built from 3 layers:
212 - SpdkPrintCommand: inherits from gdb.Command and invokes the list iteration
213 - SpdkTailqList: Performs the iteration of a tailq list according to the tailq
214 member implementation
215 - SpdkObject: Provides the __str__ function so that the list iteration can print
218 Other useful objects:
220 - SpdkNormalTailqList: represents a list which has 'tailq' as the tailq object
221 - SpdkArr: Iteration over an array (instead of a linked list)