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