xref: /freebsd/tools/debugscripts/gdbinit.kernel (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1# General kernel macros
2
3# Print the command name of the current process
4define pname
5p (char *)curproc->p_comm
6end
7document pname
8Print the command name of the current process.
9end
10
11# Show contents of bp supplied as first parameter:
12#
13# (gdb) bpp bp
14define bpp
15set $bp = (struct buf *) $arg0
16    if $bp->b_io.bio_dev
17      printf "  Buffer at 0x%x: dev 0x%x  data 0x%x  bcount 0x%x  blkno 0x%x resid 0x%x\n", \
18        $bp, \
19        $bp->b_io.bio_dev->si_udev, \
20        $bp->b_io.bio_data, \
21        $bp->b_io.bio_bcount, \
22        $bp->b_io.bio_blkno, \
23        $bp->b_io.bio_resid
24    else
25      printf "  Buffer at 0x%x: dev (none) data 0x%x  bcount 0x%x  blkno 0x%x resid 0x%x\n", \
26        $bp, \
27        $bp->b_io.bio_data, \
28        $bp->b_io.bio_bcount, \
29        $bp->b_io.bio_blkno, \
30        $bp->b_io.bio_resid
31    end
32    printf "   flags 0x%x: ", $bp->b_flags
33      if $bp->b_flags & 0x10
34        printf "busy "
35      end
36      if $bp->b_flags & 0x40
37        printf "call "
38      end
39      if $bp->b_flags & 0x200
40        printf "done "
41      end
42      if $bp->b_flags & 0x800
43        printf "error "
44      end
45      if $bp->b_flags & 0x40000
46        printf "phys "
47      end
48      if $bp->b_flags & 0x100000
49        printf "read "
50      end
51    printf "\n"
52end
53document bpp
54Show summary information about the buffer header (struct bp) pointed at by the parameter.
55end
56
57# Show more detailed contents of bp supplied as first parameter:
58#
59# (gdb) bpl bp
60define bpl
61set $bp = (struct buf *) $arg0
62printf "b_proc: "
63output $bp->b_proc
64printf "\nb_flags:      "
65output $bp->b_flags
66printf "\nb_qindex:     "
67output $bp->b_qindex
68printf "\nb_usecount:   "
69output $bp->b_usecount
70printf "\nb_error:      "
71output $bp->b_error
72printf "\nb_bufsize:    "
73output $bp->b_bufsize
74printf "\nb_io.bio_bcount:     "
75output $bp->b_io.bio_bcount
76printf "\nb_io.bio_resid:      "
77output $bp->b_io.bio_resid
78printf "\nb_io.bio_dev:        "
79output $bp->b_io.bio_dev
80printf "\nb_io.bio_data:       "
81output $bp->b_io.bio_data
82printf "\nb_kvasize:    "
83output $bp->b_kvasize
84printf "\nb_lblkno:     "
85output $bp->b_lblkno
86printf "\nb_io.bio_blkno:      "
87output $bp->b_io.bio_blkno
88printf "\nb_iodone:     "
89output $bp->b_iodone
90printf "\nb_vp: "
91output $bp->b_vp
92printf "\nb_dirtyoff:   "
93output $bp->b_dirtyoff
94printf "\nb_dirtyend:   "
95output $bp->b_dirtyend
96printf "\nb_generation: "
97output $bp->b_generation
98printf "\nb_rcred:      "
99output $bp->b_rcred
100printf "\nb_wcred:      "
101output $bp->b_wcred
102printf "\nb_validoff:   "
103output $bp->b_validoff
104printf "\nb_validend:   "
105output $bp->b_validend
106printf "\nb_pblkno:     "
107output $bp->b_pblkno
108printf "\nb_saveaddr:   "
109output $bp->b_saveaddr
110printf "\nb_savekva:    "
111output $bp->b_savekva
112printf "\nb_driver1:    "
113output $bp->b_driver1
114printf "\nb_driver2:    "
115output $bp->b_driver2
116printf "\nb_spc:        "
117output $bp->b_spc
118printf "\nb_npages:     "
119output $bp->b_npages
120printf "\n"
121end
122document bpl
123Show detailed information about the buffer header (struct bp) pointed at by the parameter.
124end
125
126# Show contents of buffer header in local variable bp.
127define bp
128bpp bp
129end
130document bp
131Show information about the buffer header pointed to by the variable bp in the current frame.
132end
133
134# Show data of buffer header in local variable bp as string.
135define bpd
136    printf "Buffer data:\n%s", (char *) bp->b_io.bio_data
137end
138document bpd
139Show the contents (char*) of bp->data in the current frame.
140end
141document bpl
142Show detailed information about the buffer header (struct bp) pointed at by the local variable bp.
143end
144define bx
145printf "\n b_vnbufs "
146output/x bp->b_vnbufs
147printf "\n b_freelist "
148output/x bp->b_freelist
149printf "\n b_act "
150output/x bp->b_act
151printf "\n b_flags "
152output/x bp->b_flags
153printf "\n b_qindex "
154output/x bp->b_qindex
155printf "\n b_usecount "
156output/x bp->b_usecount
157printf "\n b_error "
158output/x bp->b_error
159printf "\n b_bufsize "
160output/x bp->b_bufsize
161printf "\n b_io.bio_bcount "
162output/x bp->b_io.bio_bcount
163printf "\n b_io.bio_resid "
164output/x bp->b_io.bio_resid
165printf "\n b_io.bio_dev "
166output/x bp->b_io.bio_dev
167printf "\n b_io.bio_data "
168output/x bp->b_io.bio_data
169printf "\n b_kvasize "
170output/x bp->b_kvasize
171printf "\n b_io.bio_blkno "
172output/x bp->b_io.bio_blkno
173printf "\n b_iodone_chain "
174output/x bp->b_iodone_chain
175printf "\n b_vp "
176output/x bp->b_vp
177printf "\n b_dirtyoff "
178output/x bp->b_dirtyoff
179printf "\n b_validoff "
180output/x bp->b_validoff
181echo \n
182end
183document bx
184Print a number of fields from the buffer header pointed at in by the pointer bp in the current environment.
185end
186
187# Switch back to ddb
188define ddb
189set boothowto=0x80000000
190s
191end
192document ddb
193Switch back to ddb.
194end
195
196# ps: equivalent of the userland command
197define ps
198    set $nproc = nprocs
199    set $aproc = allproc.lh_first
200    set $proc = allproc.lh_first
201    set $tid = 1
202    printf "pid/ID ppid/tid uid  pgrp     flag st comm/name  proc/thread\n"
203    while (--$nproc >= 0)
204        set $pptr = $proc.p_pptr
205        if ($pptr == 0)
206           set $pptr = $proc
207        end
208        if ($proc.p_state)
209            printf " %5d  %6d %4d %5d %8x %2d %-10s %p\n", \
210                   $proc.p_pid, $pptr->p_pid, \
211                   $proc.p_ucred->cr_ruid, \
212                   $proc.p_pgrp->pg_id, $proc.p_flag, $proc.p_state, \
213                   &$proc.p_comm[0], $aproc
214            set $thread = $proc->p_threads.tqh_first
215            while ($thread)
216                printf "(%5d) %6d                        %-10s %p", \
217                   $tid, $thread->td_tid, $thread->td_name, $thread
218                if ($thread.td_wmesg)
219                    printf " %s", $thread.td_wmesg
220                end
221                printf "\n"
222                set $thread = $thread->td_plist.tqe_next
223                set $tid = $tid + 1
224            end
225        end
226        set $aproc = $proc.p_list.le_next
227        if ($aproc == 0 && $nproc > 0)
228            set $aproc = zombproc
229        end
230        set $proc = $aproc
231    end
232end
233document ps
234Show process status without options.
235end
236
237# Specify a process for other commands to refer to.
238# Most are machine-dependent.
239define defproc
240    set $nproc = nprocs
241    set $aproc = allproc.lh_first
242    set $proc = allproc.lh_first
243    while (--$nproc >= 0)
244        if ($proc->p_pid == $arg0)
245	   set $pptr = $proc.p_pptr
246           if ($pptr == 0)
247              set $pptr = $proc
248           end
249	   set $myvectorproc = $proc
250           if ($proc.p_state)
251               set $thread = $proc->p_threads.tqh_first
252               while ($thread)
253                   printf "%5d %08x %08x %4d %5d %5d  %06x  %d  %-10s   ", \
254                          $proc.p_pid, $aproc, \
255                          $proc.p_uarea, $proc.p_ucred->cr_ruid, $pptr->p_pid, \
256                          $proc.p_pgrp->pg_id, $proc.p_flag, $proc.p_state, \
257                          &$proc.p_comm[0]
258                   if ($thread.td_wchan)
259                       if ($thread.td_wmesg)
260                           printf "%s ", $thread.td_wmesg
261                       end
262                       printf "%x", $thread.td_wchan
263                   end
264                   printf "\n"
265                   set $thread = $thread->td_plist.tqe_next
266               end
267           end
268           btpp
269	   set $nproc = 0
270	else
271           set $proc = $proc.p_list.le_next
272        end
273   end
274end
275document defproc
276Specify a process for btpp and fr commands.
277end
278
279define vdev
280if (vp->v_type == VBLK)
281  p *vp->v_un.vu_spec.vu_specinfo
282  printf "numoutput: %d\n", vp->v_numoutput
283else
284  echo "Not a block device"
285end
286end
287document vdev
288Show some information of the vnode pointed to by the local variable vp.
289end
290
291# Kludge.  When changing macros, it's convenient to copy and paste
292# definitions from the editor into the debugger window.
293# Unfortunately, gdb insists on asking for confirmation after the
294# "define" line.  y enables you to insert the confirmation in the
295# definition without affecting the way the macro runs (much).
296define y
297echo Check your .gdbinit: it contains a y command\n
298end
299
300document y
301Kludge for writing macros   This is a no-op except for printing a message   See gdb(4) for more details.
302end
303
304# dmesg: print msgbuf.  Can take forever.
305define dmesg
306printf "%s", msgbufp->msg_ptr
307end
308document dmesg
309Print the system message buffer (dmesg) This can take a long time due to the time it takes to transmit the data across a serial line and even on a firewire connection the processing time slows it down
310end
311
312# checkmem: check unallocated memory for modifications
313# this assumes that DIAGNOSTIC is set, which causes
314# free memory to be set to 0xdeadc0de
315#
316# Use: checkmem offset length
317define checkmem
318set $offset = $arg0
319# XXX sizeof int.   Needs changing for 64 bit machines.
320# subtract 1 because the last word is always different.
321set $length = $arg1 / 4 - 1
322set $word = 0
323while ($word < $length)
324   if ((int *) $offset) [$word] != 0xdeadc0de
325      printf "invalid word 0x%x at 0x%x\n", ((int *) $offset) [$word], &((int *) $offset) [$word]
326   end
327   set $word = $word + 1
328end
329end
330
331document checkmem
332Check unallocated memory for modifications  This assumes that DIAGNOSTIC is set which causes free memory to be set to 0xdeadc0de.
333end
334
335define kernel
336  exec-file kernel.$arg0
337  symbol-file symbols.$arg0
338  core-file vmcore.$arg0
339end
340
341define kldstat
342  set $kld = linker_files.tqh_first
343  printf "Id Refs Address    Size     Name\n"
344  while ($kld != 0)
345    printf "%2d %4d 0x%08x %-8x %s\n", \
346      $kld->id, $kld->refs, $kld->address, $kld->size, $kld->filename
347    set $kld = $kld->link.tqe_next
348  end
349end
350
351document kldstat
352  Lists the modules that were loaded when the kernel crashed.
353end
354
355define kldstat-v
356  set $kld = linker_files.tqh_first
357  printf "Id Refs Address    Size     Name\n"
358  while ($kld != 0)
359    printf "%2d %4d 0x%08x %-8x %s\n", \
360      $kld->id, $kld->refs, $kld->address, $kld->size, $kld->filename
361    printf "        Contains modules:\n"
362    printf "                Id Name\n"
363    set $module = $kld->modules.tqh_first
364    while ($module != 0)
365      printf "                %2d %s\n", $module->id, $module->name
366      set $module = $module->link.tqe_next
367    end
368    set $kld = $kld->link.tqe_next
369  end
370end
371