NListtree.mcc: fix sorted insert
[AROS.git] / _gdbinit
blob066374f44245cd027f64b1b3414e6b60acf5dab7
1 handle SIGUSR1 pass noprint nostop
2 handle SIGUSR2 pass noprint nostop
3 set follow-fork-mode child
5 # This is a bit tricky, because we have several cases,
6 # and gdb has no easy 'is this symbol defined' tests.
8 # Case 1: Hosted AROSBootstrap (ie linux-i386)
9 #         Provides SysBase, Debug_ModList, and Debug_KickList
10 # Case 2: ROM (ie amiga-m68k)
11 #         Provides AbsExecBase and SysBase
13 define _sysbase_init
14     init-if-undefined $AbsExecBase = *(struct IntExecBase **)&SysBase
15 end
16 document _sysbase_init
17 Initialize $AbsExecBase convenience variables
18 end
20 define _debug_init
21     _sysbase_init
22     init-if-undefined $_debug_DebugBase = 0
24     if $AbsExecBase != 0 && $_debug_DebugBase == 0
25         set $_debug_DebugBase = (struct DebugBase *)($AbsExecBase->DebugBase)
26     end
28     if $_debug_DebugBase != 0
29         set $_debug_DebugBase = (struct DebugBase *)($_debug_DebugBase)
30         # This is the case for debugging a ROM
31         set $_debug_ModList = &($_debug_DebugBase->db_Modules)
32     else
33         # This is the case for debugging a bootstrap
34         init-if-undefined $_debug_ModList = Debug_ModList
35     end
36 end
37 document _debug_init
38 Initialize debugging convenience variables
39 end
41 define liblist
42     _sysbase_init
43     set $lib = ((struct ExecBase *)$AbsExecBase)->LibList.lh_Head
45     printf "Base     OpenC   Name\n"
46     printf "---------------------------------------------------------------\n"
47     while ($lib->ln_Succ != 0)
48     printf "%p %5d   %s\n", \
49         $lib, \
50         ((struct Library *)$lib)->lib_OpenCnt, \
51         $lib->ln_Name
52     set $lib = $lib->ln_Succ
53     end
54 end
55 document liblist
56 List the current libraries in the system
57 end
59 define devlist
60     _debug_init
61     set $dev = ((struct ExecBase *)$AbsExecBase)->DeviceList.lh_Head
62     printf "Base     OpenC   Name\n"
63     printf "---------------------------------------------------------------\n"
64     while ($dev->ln_Succ != 0)
65     printf "%p %5d   %s\n", \
66         $dev, \
67         ((struct Library *)$dev)->lib_OpenCnt, \
68         $dev->ln_Name
69     set $dev = $dev->ln_Succ
70     end
71 end
72 document devlist
73 List the current devices in the system
74 end
76 define resourcelist
77     _debug_init
78     set $res = ((struct ExecBase *)$AbsExecBase)->ResourceList.lh_Head
79     printf "Base     Name\n"
80     printf "---------------------------------------------------------------\n"
81     while ($res->ln_Succ != 0)
82     printf "%p %s\n", $res, $res->ln_Name
83     set $res = $res->ln_Succ
84     end
85 end
86 document resourcelist
87 List the current resources in the system
88 end
90 define residentlist
91     _debug_init
92     set $resp = (struct Resident **)((struct ExecBase *)$AbsExecBase)->ResModules
93     set $i = 0
95     printf "Address    Pri Flags Vers Type  Name\n"
96     printf "--------------------------------------------------------------\n"
97     while (($resp)[$i] != 0)
98     set $res = ($resp)[$i]
99     printf "%p  %4d    %02x  %3d  %3d  %s\n", \
100         $res, \
101         ((struct Resident *)$res)->rt_Pri, \
102         ((struct Resident *)$res)->rt_Flags, \
103         ((struct Resident *)$res)->rt_Version, \
104         ((struct Resident *)$res)->rt_Type, \
105         ((struct Resident *)$res)->rt_Name
106     set $i = $i + 1
107     end
109 document residentlist
110 List the system resident list
113 define taskready
114     _debug_init
115     set $task = (struct Task *)((struct ExecBase *)$AbsExecBase)->TaskReady.lh_Head
117     printf "Task     SigWait  SigRecvd StkSize   StkUsed Pri Type Name\n"
118     printf "-----------------------------------------------------------------------------\n"
119     while ($task->tc_Node.ln_Succ != 0)
120     printf "%p %p %p %8d %8d %3d  %3ld %s\n", \
121         $task, \
122         $task->tc_SigWait, \
123         $task->tc_SigRecvd, \
124         $task->tc_SPUpper - $task->tc_SPLower, \
125         $task->tc_SPUpper - $task->tc_SPReg, \
126         $task->tc_Node.ln_Pri, \
127         $task->tc_Node.ln_Type, \
128         $task->tc_Node.ln_Name
129     set $task = (struct Task *)$task->tc_Node.ln_Succ
130     end
132 document taskready
133 List of tasks currently ready to run
136 define taskwait
137     _debug_init
138     set $task = (struct Task *)((struct ExecBase *)$AbsExecBase)->TaskWait.lh_Head
140     printf "Task     SigWait  SigRecvd StkSize   StkUsed Pri Type Name\n"
141     printf "-----------------------------------------------------------------------------\n"
142     while ($task->tc_Node.ln_Succ != 0)
143     printf "%p %p %p %8d %8d %3d  %3ld %s\n", \
144         $task, \
145         $task->tc_SigWait, \
146         $task->tc_SigRecvd, \
147         $task->tc_SPUpper - $task->tc_SPLower, \
148         $task->tc_SPUpper - $task->tc_SPReg, \
149         $task->tc_Node.ln_Pri, \
150         $task->tc_Node.ln_Type, \
151         $task->tc_Node.ln_Name
152     set $task = (struct Task *)$task->tc_Node.ln_Succ
153     end
155 document taskwait
156 List of tasks currently waiting for an event
159 define thistask
160     _debug_init
161     set $task = (struct Task *)((struct ExecBase *)$AbsExecBase)->ThisTask
162     printf "Task     SigWait  SigRecvd StkSize   StkUsed Pri Type Name\n"
163     printf "-----------------------------------------------------------------------------\n"
164     printf "%p %p %p %8d %8d %3d  %3ld %s\n", \
165     $task, \
166     $task->tc_SigWait, \
167     $task->tc_SigRecvd, \
168     $task->tc_SPUpper - $task->tc_SPLower, \
169     $task->tc_SPUpper - $task->tc_SPReg, \
170     $task->tc_Node.ln_Pri, \
171     $task->tc_Node.ln_Type, \
172     $task->tc_Node.ln_Name
174 document thistask
175 Print out information about the currently running task.
178 define modlist
179     _debug_init
180     printf "Segment                                   Module\n"
182     if $_debug_ModList
183         printf "---------------------------------------------------------------------\n"
184         set $modnode = (module_t *)$_debug_ModList->mlh_Head
186         while ($modnode->m_node.mln_Succ != 0)
187             set $segidx = 0
188             while ($segidx < $modnode->m_segcnt)
189                 set $segnode = $modnode->m_segments[$segidx]
190                 printf "%p %12s %2u %42s\n", $segnode->s_lowest, $segnode->s_name, $segnode->s_num, $segnode->s_mod->m_name
191                 set $segidx = $segidx + 1
192             end
194             set $modnode = (module_t *)$modnode->m_node.mln_Succ
195         end
196     end
198 document modlist
199 List of all the modules currently loaded in memory
202 # Execute a binary search over sorter array of segments
203 # Keep this function synchronized with decodelocation.c, FindSegmentInModule
204 define find_segment_in_module
205     set $arg_address = $arg0
206     set $arg_mod = $arg1
207     set $find_segment_in_module_result = 0
208     set $local_minsegidx = 0
209     set $local_maxsegidx = $arg_mod->m_segcnt - 1
210     set $_again = 1
212     while ($_again == 1)
213         set $local_segidx = ($local_maxsegidx + $local_minsegidx) / 2
215         if $arg_mod->m_segments[$local_segidx]->s_lowest <= $arg_address
216             if $arg_mod->m_segments[$local_segidx]->s_highest >= $arg_address
217                 set $find_segment_in_module_result = $arg_mod->m_segments[$local_segidx]
218                 loop_break
219             else
220                 set $local_minsegidx = $local_segidx + 1
221             end
222         else
223             set $local_maxsegidx = $local_segidx - 1
224         end
226         if $local_maxsegidx < $local_minsegidx
227             # Not found, aborting
228             loop_break
229         end
230     end
233 # Keep this function synchronized with decodelocation.c, FindSegment
234 define find_segment
235     _debug_init
236     set $find_segment_result = 0
238     if $_debug_ModList
239         set $modnode = (module_t *)$_debug_ModList->mlh_Head
241         while ($modnode->m_node.mln_Succ != 0)
242             if !($modnode->m_gaplowest <= $arg0 && $modnode->m_gaphighest >= $arg0)
243                 if $modnode->m_lowest <= $arg0 && $modnode->m_highest >= $arg0
245                     find_segment_in_module $arg0 $modnode
247                     if $find_segment_in_module_result
248                         set $find_segment_result = $find_segment_in_module_result
249                         loop_break
250                     end
251                 end
252             end
253             set $modnode = (module_t *)$modnode->m_node.mln_Succ
254         end
255     end
258 define findaddr
259     _debug_init
260     set $cont = 1
262     #first search in modules loaded from disk
263     printf "Searching in the loaded modules...\n"
265     find_segment $arg0
267     if $find_segment_result
268         set $segnode = $find_segment_result
269         printf "Address found in %s, in segment %p.\nIf this is an executable, its .text section starts at %p.\n", $segnode->s_mod->m_name, $segnode->s_seg, $segnode->s_lowest
270         set $cont = 0
271     end
273     #then in the resident list
274     if $cont
275         printf "Searching in the resident list...\n"
276     end
278     set $resp = (struct Resident **)((struct ExecBase *)$AbsExecBase)->ResModules
279     set $i = 0
281     while (($resp)[$i] != 0) && $cont
282     set $res = ($resp)[$i]
284         if ($arg0 >= $res) && ($arg0 <= $res->rt_EndSkip)
285             printf "Address found in %s, which resides at %p\n", $res->rt_Name, $res
286             set $cont = 0
287         end
289         set $i = $i + 1
290     end
292     if $cont
293         printf "No matching module for this address\n"
294     end
296 document findaddr
297 -Shows the module that contains the given address
299 -To debug a problem in AROS, do the following:
301 -1. Get a stacktrace with bt or similar.
302 -2. Use findaddr with such an address to find out in which
303 -   module it is:
304 -3. Use add-symbol-file to load that modules symbols.
305 -4. Now you can run bt (or similar) again and you should see the
306 -   addresses resolved as symbols.
308 -Example:
310 -0x4058d45b in ?? ()
312 -(gdb) findaddr 0x4058d45b
314 -Searching in the loaded modules...
315 -Address found in Workbench:contrib/Zune/Libs/muimaster.library, which is loaded at 0x405379a4.
316 -If this is an executable, its .text section starts at 0x405379b0.
317 -(gdb) add-symbol-file contrib/Zune/Libs/muimaster.library 0x405379b0
318 -add symbol table from file "contrib/Zune/Libs/muimaster.library" at
319 -        .text_addr = 0x405379b0
320 -(y or n) y
321 -Reading symbols from contrib/Zune/Libs/muimaster.library...done.
322 -(gdb) bt
323 -#0  0x4058d45b in strlen (ptr=0x80 <Address 0x80 out of bounds>) at strlen.c:45
324 -#1  0x00000000 in lastx.78 ()
327 define printtaglist
328     set $list = (struct TagItem *)$arg0
330     printf "Tag         Data (Hex)     Data (Dec)\n"
331     printf "--------------------------------------\n"
333     while $list->ti_Tag != 0
334     # Handle the possible control tag...
335     if $list->ti_Tag == 1
336         printf "TAG_IGNORE\n"
337     else if $list->ti_Tag == 2
338         printf "TAG_MORE    %p\n", $list->ti_Data
339         set $list = (struct TagItem *)$list->ti_Data
340     else if $list->ti_Tag == 3
341         printf "TAG_SKIP    %d\n", $list->ti_Data
342         set $list = $list + $list->ti_Tag + 1
343     else
344         printf "%p  %p      %9lu\n", $list->ti_Tag, $list->ti_Data, $list->ti_Data
345         set $list = $list + 1
346     end
347     end
348     printf "TAG_DONE\n"
350 document printtaglist
353 define log_to_file
354   set logging file $arg0
355   set logging redirect on
356   set logging overwrite $arg1
357   set logging on
360 define end_log
361   set logging off
364 define loadmod
365     set pagination off
366     set $this_mod = $arg0
368     log_to_file segname.tmp on
369     printf "%s", $this_mod->m_name
370     end_log
371     shell sed -i 's/.*:\(.*\)/\1/' segname.tmp
372     log_to_file loadseg.tmp on
373     printf "add-symbol-file \""
374     end_log
375     shell head -n1 segname.tmp >>loadseg.tmp
376     log_to_file loadseg.tmp off
377     printf "\" %s", $this_mod->m_seggdbhlp
378     end_log
380     source loadseg.tmp
381     set pagination on
384 define loadseg
385     _debug_init
386     dont-repeat
387     if $_debug_ModList
388         set $step = 1
390         find_segment $arg0
392         if $find_segment_result
393             # This is workaround for some flushing problem when loadseg.tmp had module name before command
394             printf "\n"
396             loadmod $find_segment_result->s_mod
398             set $step = 2
399         end
401         if $step < 2
402             printf "No matching module for this address\n"
403         end
405     end
407 document loadseg
408 Loads the module that contains the given address
411 define loadmods
412     _debug_init
414     if $_debug_ModList
415         set $loadmods_modnode = (module_t *)$_debug_ModList->mlh_Head
417         while ($loadmods_modnode->m_node.mln_Succ != 0)
418             loadmod $loadmods_modnode
419             set $loadmods_modnode = (module_t *)$loadmods_modnode->m_node.mln_Succ
420         end
421     end
423 document loadmods
424 Loads all the modules (symbols)
427 define loadframe
428     log_to_file frameinfo.tmp on
429     info frame $arg0
430     end_log
431     
432     shell grep "eip =" frameinfo.tmp >loadseg.tmp
433     shell sed -e 's/eip = \(0x[0-9,a-f]*\).*/loadseg \1/' loadseg.tmp >frameinfo.tmp
434     
435     source frameinfo.tmp
437 document loadframe
438 Loads the symbols for the given stack frame number
441 define loadframes
442     set $_again = 1
443     printf "\n\n"
444     
445     while ($_again == 1)
446         bt
447         printf "\nEnter stack frame number to resolve or RETURN to stop: \n\n"
449         set $_answer = 1000
451         shell read _gdb_answer && echo $_gdb_answer | grep -E "[0-9]+" | sed "s/\(.*\)/set \$_answer=\1/" >frameinfo.tmp
453         source frameinfo.tmp
454         printf "\n\n"
456         if $_answer == 1000
457             set $_again = 0
458         else
459             loadframe $_answer
460         end
461     end
463 document loadframes
464 Interactive multiple loading of symbols for given stack frame numbers
467 define loadbt
468     set $_frame_counter = 0
470     set $_again = 1
471     while ($_again == 1)
472         printf "Checking frame #%d\n", $_frame_counter
474         set $_again = 0
476         log_to_file loadseg.tmp on
477         set pagination off
478         bt
479         end_log
481         # Check if frame exists
483         log_to_file frameinfo.tmp on
484         printf "shell grep \"#%d .*\" loadseg.tmp | sed \"s/#.*/set \\$_again = 1/\" >loadbt.tmp\n", $_frame_counter
485         end_log
487         source frameinfo.tmp
488         source loadbt.tmp
490         if $_again == 1
492             # If frame is unresolved ("in ??") transform to "loadseg address"
493             
494             log_to_file frameinfo.tmp on
495             printf "shell grep \"#%d .* in ??\" loadseg.tmp | sed \"s/#.*\\(0x[0-9,a-f]*\\) .*/loadseg \\1/\" >loadbt.tmp\n", $_frame_counter
496             end_log
498             source frameinfo.tmp
499             source loadbt.tmp
501             set $_frame_counter = $_frame_counter + 1
502             if ($_frame_counter == 1000)
503                 set $_again = 0
504             end
505         end
506     end
508     set pagination on
510 document loadbt
511 Tries to automatically load the symbols for all unresolved stack frames
514 define seglistdump
515     _debug_init
516     set $nextseg = $arg0
517     set $count   = 1
519     printf "Hunk num. | Start addr | Size       \n"
520     printf "----------+------------+------------\n"
521     while $nextseg
522         printf "%9d | %p | %10d\n", $count, $nextseg + sizeof(BPTR), *((ULONG *)$nextseg - 1) - sizeof(BPTR)
524         set $nextseg = *(BPTR *)$nextseg
525         set $count = $count+1
526     end
528 document seglistdump
529 Shows the segments chain of the given seglist
532 define bttask
533     set $task = (struct Task *)$arg0
534     if ($task->tc_Node.ln_Type != 1) && ($task->tc_Node.ln_Type != 13)
535         printf "ERROR: Given address does not look like a task or process!\n"
536     else
537         #FIXME: The following assumes Linux x86
538         set $old_esp = $esp
539         set $old_eip = $eip
540         
541         if $task->tc_State == 2
542             printf "WARNING: TS_RUN Task. Showing normal backtrace!\n"
543         else
544             set $esp = $task->tc_SPReg
545             set $taskcontext = (struct AROSCPUContext *)$task->tc_UnionETask.tc_ETask.et_RegFrame
546             set $eip = $taskcontext.regs.eip
547         end
548         
549         bt
551         set $esp = $old_esp
552         set $eip = $old_eip
553     end
555 document bttask
556 Shows the backtrace of the given TS_READY/TS_WAIT task
559 define semowner
560     set $sem = (struct SignalSemaphore *)$arg0
561     if $sem->ss_Link.ln_Type != 15
562         printf "ERROR: Given address does not look like a semaphore!\n"
563     else
564         if $sem->ss_QueueCount == -1
565             printf "The semaphore is free\n"
566         else
567             if $sem->ss_Owner == 0
568                 printf "The semaphore is locked in shared mode by one or more tasks\n"
569             else
570                     printf "The semaphore is owned by task %p (%s)\n", $sem->ss_Owner, $sem->ss_Owner->tc_Node.ln_Name
571             end
573             printf "\nWait Queue:\n\n"
575             set $waitnode = (struct SemaphoreRequest *)$sem->ss_WaitQueue.mlh_Head
577             while ($waitnode->sr_Link.mln_Succ != 0)
578                 set $waitertask = $waitnode->sr_Waiter
579                 if (int)$waitertask & 1
580                     set $waitertask = $waitertask & ~1
581                     printf " SHARED    "
582                 else
583                     printf " EXCLUSIVE "
584                 end
585                 printf"%p %s\n", $waitertask, $waitertask->tc_Node.ln_Name
586                 set $waitnode = (struct SemaphoreRequest *)$waitnode->sr_Link.mln_Succ
587             end
588         end
589     end
591 document semowner
592 Shows the owner of the given Exec Semaphore and the Wait Queue