1030b9a7cSGreg Lehey# Assembler-level macros for i386 2030b9a7cSGreg Lehey# Disassemble the next 10 instructions. 3030b9a7cSGreg Leheydefine xi 4030b9a7cSGreg Leheyx/10i $eip 5030b9a7cSGreg Leheyend 6030b9a7cSGreg Lehey 7030b9a7cSGreg Lehey# Top 12 words on stack 8030b9a7cSGreg Leheydefine xs 9030b9a7cSGreg Leheyx/12x $esp 10030b9a7cSGreg Leheyend 11030b9a7cSGreg Lehey 12030b9a7cSGreg Lehey# Top 12 words from frame pointer 13030b9a7cSGreg Leheydefine xb 14030b9a7cSGreg Leheyx/12x $ebp 15030b9a7cSGreg Leheyend 16030b9a7cSGreg Lehey 17030b9a7cSGreg Lehey# single step through calls and disassemble the next instruction 18030b9a7cSGreg Leheydefine z 19030b9a7cSGreg Leheyni 20030b9a7cSGreg Leheyx/1i $eip 21030b9a7cSGreg Leheyend 22030b9a7cSGreg Lehey 23030b9a7cSGreg Lehey# single step over calls and disassemble the next instruction 24030b9a7cSGreg Leheydefine zs 25030b9a7cSGreg Leheysi 26030b9a7cSGreg Leheyx/1i $eip 27030b9a7cSGreg Leheyend 28030b9a7cSGreg Lehey 29030b9a7cSGreg Lehey# show current stack frame and first 4 parameters 30030b9a7cSGreg Leheydefine xp 31030b9a7cSGreg Leheyprintf " esp: " 32030b9a7cSGreg Leheyoutput/x $esp 33030b9a7cSGreg Leheyecho ( 34030b9a7cSGreg Leheyoutput (((int)$ebp)-(int)$esp)/4-4 35030b9a7cSGreg Leheyprintf " words on stack)\n ebp: " 36030b9a7cSGreg Leheyoutput/x $ebp 37030b9a7cSGreg Leheyprintf "\n eip: " 38030b9a7cSGreg Leheyx/1i $eip 39030b9a7cSGreg Leheyprintf "Saved ebp: " 40030b9a7cSGreg Leheyoutput/x *(int*)$ebp 41030b9a7cSGreg Leheyprintf " (maximum of " 42030b9a7cSGreg Leheyoutput ((*(int*)$ebp)-(int)$ebp)/4-4 43030b9a7cSGreg Leheyprintf " parameters possible)\nSaved eip: " 44030b9a7cSGreg Leheyx/1i *(int*)($ebp+4) 45030b9a7cSGreg Leheyprintf "\nParm 1 at " 46030b9a7cSGreg Leheyoutput/x (int) ($ebp+8) 47030b9a7cSGreg Leheyprintf ": " 48030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+8) 49030b9a7cSGreg Leheyprintf "\nParm 2 at " 50030b9a7cSGreg Leheyoutput/x (int) ($ebp+12) 51030b9a7cSGreg Leheyprintf ": " 52030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+12) 53030b9a7cSGreg Leheyprintf "\nParm 3 at " 54030b9a7cSGreg Leheyoutput/x (int) ($ebp+16) 55030b9a7cSGreg Leheyprintf ": " 56030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+16) 57030b9a7cSGreg Leheyprintf "\nParm 4 at " 58030b9a7cSGreg Leheyoutput/x (int) ($ebp+20) 59030b9a7cSGreg Leheyprintf ": " 60030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+20) 61030b9a7cSGreg Leheyecho \n 62030b9a7cSGreg Leheyend 63030b9a7cSGreg Leheydocument xp 64030b9a7cSGreg LeheyShow the register contents and the first four parameter 65030b9a7cSGreg Leheywords of the current frame. 66030b9a7cSGreg Leheyend 67030b9a7cSGreg Lehey 68030b9a7cSGreg Lehey# show current stack frame and first 10 parameters 69030b9a7cSGreg Leheydefine xxp 70030b9a7cSGreg Leheyprintf " esp: " 71030b9a7cSGreg Leheyoutput/x $esp 72030b9a7cSGreg Leheyprintf "\n ebp: " 73030b9a7cSGreg Leheyoutput/x $ebp 74030b9a7cSGreg Leheyprintf "\n eip: " 75030b9a7cSGreg Leheyx/1i $eip 76030b9a7cSGreg Leheyprintf "Saved ebp: " 77030b9a7cSGreg Leheyoutput/x *(int*)$ebp 78030b9a7cSGreg Leheyprintf " (maximum of " 79030b9a7cSGreg Leheyoutput ((*(int*)$ebp)-(int)$ebp)/4-4 80030b9a7cSGreg Leheyprintf " parameters possible)\nSaved eip: " 81030b9a7cSGreg Leheyx/1i *(int*)($ebp+4) 82030b9a7cSGreg Leheyprintf "\nParm 1 at " 83030b9a7cSGreg Leheyoutput/x (int) ($ebp+8) 84030b9a7cSGreg Leheyprintf ": " 85030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+8) 86030b9a7cSGreg Leheyprintf "\nParm 2 at " 87030b9a7cSGreg Leheyoutput/x (int) ($ebp+12) 88030b9a7cSGreg Leheyprintf ": " 89030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+12) 90030b9a7cSGreg Leheyprintf "\nParm 3 at " 91030b9a7cSGreg Leheyoutput/x (int) ($ebp+16) 92030b9a7cSGreg Leheyprintf ": " 93030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+16) 94030b9a7cSGreg Leheyprintf "\nParm 4 at " 95030b9a7cSGreg Leheyoutput/x (int) ($ebp+20) 96030b9a7cSGreg Leheyprintf ": " 97030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+20) 98030b9a7cSGreg Leheyprintf "\nParm 5 at " 99030b9a7cSGreg Leheyoutput/x (int) ($ebp+24) 100030b9a7cSGreg Leheyprintf ": " 101030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+24) 102030b9a7cSGreg Leheyprintf "\nParm 6 at " 103030b9a7cSGreg Leheyoutput/x (int) ($ebp+28) 104030b9a7cSGreg Leheyprintf ": " 105030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+28) 106030b9a7cSGreg Leheyprintf "\nParm 7 at " 107030b9a7cSGreg Leheyoutput/x (int) ($ebp+32) 108030b9a7cSGreg Leheyprintf ": " 109030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+32) 110030b9a7cSGreg Leheyprintf "\nParm 8 at " 111030b9a7cSGreg Leheyoutput/x (int) ($ebp+36) 112030b9a7cSGreg Leheyprintf ": " 113030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+36) 114030b9a7cSGreg Leheyprintf "\nParm 9 at " 115030b9a7cSGreg Leheyoutput/x (int) ($ebp+40) 116030b9a7cSGreg Leheyprintf ": " 117030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+40) 118030b9a7cSGreg Leheyprintf "\nParm 10 at " 119030b9a7cSGreg Leheyoutput/x (int) ($ebp+44) 120030b9a7cSGreg Leheyprintf ": " 121030b9a7cSGreg Leheyoutput (char*) *(int*)($ebp+44) 122030b9a7cSGreg Leheyecho \n 123030b9a7cSGreg Leheyend 124030b9a7cSGreg Leheydocument xxp 125030b9a7cSGreg LeheyShow the register contents and the first ten parameter 126030b9a7cSGreg Leheywords of the current frame. 127030b9a7cSGreg Leheyend 128030b9a7cSGreg Lehey 129030b9a7cSGreg Lehey# Show first to fifth parameters of current frame as int, int * and char *. 130030b9a7cSGreg Leheydefine xp0 131030b9a7cSGreg Leheyx/12x *(int*)$esp 132030b9a7cSGreg Leheyp *(int*)$esp 133030b9a7cSGreg Leheyp (char*)*$esp 134030b9a7cSGreg Leheyend 135030b9a7cSGreg Leheydefine xp1 136030b9a7cSGreg Leheyx/12x *(int*)($ebp+4) 137030b9a7cSGreg Leheyp *(int*)($ebp+4) 138030b9a7cSGreg Leheyp (char**)($ebp+4) 139030b9a7cSGreg Leheyend 140030b9a7cSGreg Leheydefine xp2 141030b9a7cSGreg Leheyx/12x *(int*)($ebp+8) 142030b9a7cSGreg Leheyp *(int*)($ebp+8) 143030b9a7cSGreg Leheyp *(char**)($ebp+8) 144030b9a7cSGreg Leheyend 145030b9a7cSGreg Leheydefine xp3 146030b9a7cSGreg Leheyx/12x *(int*)($ebp+12) 147030b9a7cSGreg Leheyp *(int*)($ebp+12) 148030b9a7cSGreg Leheyp (char**)($ebp+12) 149030b9a7cSGreg Leheyend 150030b9a7cSGreg Leheydefine xp4 151030b9a7cSGreg Leheyx/12x *(int*)($ebp+16) 152030b9a7cSGreg Leheyp *(int*)($ebp+16) 153030b9a7cSGreg Leheyp (char**)($ebp+16) 154030b9a7cSGreg Leheyend 155030b9a7cSGreg Leheydocument xp0 156030b9a7cSGreg LeheyShow the first parameter of current stack frame in various formats 157030b9a7cSGreg Leheyend 158030b9a7cSGreg Leheydocument xp1 159030b9a7cSGreg LeheyShow the second parameter of current stack frame in various formats 160030b9a7cSGreg Leheyend 161030b9a7cSGreg Leheydocument xp2 162030b9a7cSGreg LeheyShow the third parameter of current stack frame in various formats 163030b9a7cSGreg Leheyend 164030b9a7cSGreg Leheydocument xp3 165030b9a7cSGreg LeheyShow the fourth parameter of current stack frame in various formats 166030b9a7cSGreg Leheyend 167030b9a7cSGreg Leheydocument xp4 168030b9a7cSGreg LeheyShow the fifth parameter of current stack frame in various formats 169030b9a7cSGreg Leheyend 170030b9a7cSGreg Lehey 171030b9a7cSGreg Lehey# Select frame 0 to 5 and show stack information. 172030b9a7cSGreg Leheydefine f0 173030b9a7cSGreg Leheyf 0 174030b9a7cSGreg Leheyxp 175030b9a7cSGreg Leheyend 176030b9a7cSGreg Leheydefine f1 177030b9a7cSGreg Leheyf 1 178030b9a7cSGreg Leheyxp 179030b9a7cSGreg Leheyend 180030b9a7cSGreg Leheydefine f2 181030b9a7cSGreg Leheyf 2 182030b9a7cSGreg Leheyxp 183030b9a7cSGreg Leheyend 184030b9a7cSGreg Leheydefine f3 185030b9a7cSGreg Leheyf 3 186030b9a7cSGreg Leheyxp 187030b9a7cSGreg Leheyend 188030b9a7cSGreg Leheydefine f4 189030b9a7cSGreg Leheyf 4 190030b9a7cSGreg Leheyxp 191030b9a7cSGreg Leheyend 192030b9a7cSGreg Leheydefine f5 193030b9a7cSGreg Leheyf 5 194030b9a7cSGreg Leheyxp 195030b9a7cSGreg Leheyend 196030b9a7cSGreg Leheydocument f0 197030b9a7cSGreg LeheySelect stack frame 0 and show assembler-level details 198030b9a7cSGreg Leheyend 199030b9a7cSGreg Leheydocument f1 200030b9a7cSGreg LeheySelect stack frame 1 and show assembler-level details 201030b9a7cSGreg Leheyend 202030b9a7cSGreg Leheydocument f2 203030b9a7cSGreg LeheySelect stack frame 2 and show assembler-level details 204030b9a7cSGreg Leheyend 205030b9a7cSGreg Leheydocument f3 206030b9a7cSGreg LeheySelect stack frame 3 and show assembler-level details 207030b9a7cSGreg Leheyend 208030b9a7cSGreg Leheydocument f4 209030b9a7cSGreg LeheySelect stack frame 4 and show assembler-level details 210030b9a7cSGreg Leheyend 211030b9a7cSGreg Leheydocument f5 212030b9a7cSGreg LeheySelect stack frame 5 and show assembler-level details 213030b9a7cSGreg Leheyend 214030b9a7cSGreg Leheydocument z 215030b9a7cSGreg LeheySingle step 1 instruction (over calls) and show next instruction. 216030b9a7cSGreg Leheyend 217030b9a7cSGreg Leheydocument zs 218030b9a7cSGreg LeheySingle step 1 instruction (through calls) and show next instruction. 219030b9a7cSGreg Leheyend 220030b9a7cSGreg Leheydocument xi 221030b9a7cSGreg LeheyList the next 10 instructions from the current IP value 222030b9a7cSGreg Leheyend 223030b9a7cSGreg Leheydocument xs 224030b9a7cSGreg LeheyShow the last 12 words on stack in hex 225030b9a7cSGreg Leheyend 226030b9a7cSGreg Leheydocument xb 227030b9a7cSGreg LeheyShow 12 words starting at current BP value in hex 228030b9a7cSGreg Leheyend 229030b9a7cSGreg Lehey 230030b9a7cSGreg Lehey# pcb <pid> 231030b9a7cSGreg Lehey# show contents of pcb, currently only i386. 232030b9a7cSGreg Leheydefine pcb 233030b9a7cSGreg Lehey set $nproc = nprocs 234030b9a7cSGreg Lehey set $aproc = allproc.lh_first 235030b9a7cSGreg Lehey set $proc = allproc.lh_first 236030b9a7cSGreg Lehey while (--$nproc >= 0) 237030b9a7cSGreg Lehey set $pptr = $proc.p_pptr 238030b9a7cSGreg Lehey if ($proc->p_pid == $arg0) 239030b9a7cSGreg Lehey set $pcba = $proc->p_threads.tqh_first->td_pcb 240030b9a7cSGreg Lehey printf "ip: %08x sp: %08x bp: %08x bx: %08x\n", $pcba->pcb_eip, $pcba->pcb_esp, $pcba->pcb_ebp, $pcba->pcb_ebx 241030b9a7cSGreg Lehey x/1i $pcba->pcb_eip 242030b9a7cSGreg Lehey set $nproc = 0 243030b9a7cSGreg Lehey end 244030b9a7cSGreg Lehey set $aproc = $proc.p_list.le_next 245030b9a7cSGreg Lehey if ($aproc == 0 && $nproc > 0) 246030b9a7cSGreg Lehey set $aproc = zombproc 247030b9a7cSGreg Lehey end 248030b9a7cSGreg Lehey set $proc = $aproc 249030b9a7cSGreg Lehey end 250030b9a7cSGreg Leheyend 251030b9a7cSGreg Leheydocument pcb 252030b9a7cSGreg LeheyShow some pcb contents of process whose pid is specified. 253030b9a7cSGreg Leheyend 254030b9a7cSGreg Lehey 255030b9a7cSGreg Lehey# btr <frame> 256030b9a7cSGreg Lehey# primitive backtrace. frame is a memory address. 257030b9a7cSGreg Leheydefine btr 258030b9a7cSGreg Leheyset $frame = $arg0 259030b9a7cSGreg Leheyset $fno = 0 260030b9a7cSGreg Leheywhile (*(int *) $frame > 0xc0000000) 261030b9a7cSGreg Lehey set $myebp = *(int *) $frame 262030b9a7cSGreg Lehey set $myeip = *(int *) ($frame + 4) 263030b9a7cSGreg Lehey printf " frame %d at %p: ebp %8x, eip ", $fno, $frame, $myebp 264030b9a7cSGreg Lehey x/1i $myeip 265030b9a7cSGreg Lehey set $frame = $myebp 266030b9a7cSGreg Lehey set $fno = $fno + 1 267030b9a7cSGreg Leheyend 268030b9a7cSGreg Leheyend 269030b9a7cSGreg Leheydocument btr 270*8ce070c1SUlrich SpörleinShow a backtrace from the ebp address specified. This can be used to get a backtrace from any stack resident in memory. It's the user's responsibility to ensure that the address is meaningful. 271030b9a7cSGreg Leheyend 272030b9a7cSGreg Lehey 273030b9a7cSGreg Lehey# btp <pid> 274030b9a7cSGreg Lehey# backtrace for process <pid>. Uses btr (machine dependent) to perform the backtrace. 275030b9a7cSGreg Lehey# may produce nonsense. 276030b9a7cSGreg Leheydefine btp 277030b9a7cSGreg Lehey set $nproc = nprocs 278030b9a7cSGreg Lehey set $aproc = allproc.lh_first 279030b9a7cSGreg Lehey set $proc = allproc.lh_first 280030b9a7cSGreg Lehey while (--$nproc >= 0) 281030b9a7cSGreg Lehey if ($proc->p_pid == $arg0) 282030b9a7cSGreg Lehey btr $proc->p_threads.tqh_first->td_pcb->pcb_ebp 283030b9a7cSGreg Lehey set $nproc = 0 284030b9a7cSGreg Lehey else 285030b9a7cSGreg Lehey set $aproc = $proc.p_list.le_next 286030b9a7cSGreg Lehey if ($aproc == 0 && $nproc > 0) 287030b9a7cSGreg Lehey set $aproc = zombproc 288030b9a7cSGreg Lehey end 289030b9a7cSGreg Lehey set $proc = $aproc 290030b9a7cSGreg Lehey end 291030b9a7cSGreg Lehey end 292030b9a7cSGreg Leheyend 293030b9a7cSGreg Leheydocument btp 294030b9a7cSGreg LeheyShow a backtrace for the process whose pid is specified as a parameter. 295030b9a7cSGreg Leheyend 296030b9a7cSGreg Lehey 297030b9a7cSGreg Lehey# Do backtraces for all processes in the system. 298030b9a7cSGreg Lehey# Uses btr (machine dependent) to perform the backtrace. 299030b9a7cSGreg Leheydefine btpa 300030b9a7cSGreg Lehey set $nproc = nprocs 301030b9a7cSGreg Lehey set $aproc = allproc.lh_first 302030b9a7cSGreg Lehey set $proc = allproc.lh_first 303626ff208SDavid Schultz printf " pid proc uid ppid pgrp flag stat comm wchan\n" 304030b9a7cSGreg Lehey while (--$nproc >= 0) 305030b9a7cSGreg Lehey set $pptr = $proc.p_pptr 306030b9a7cSGreg Lehey if ($pptr == 0) 307030b9a7cSGreg Lehey set $pptr = $proc 308030b9a7cSGreg Lehey end 309030b9a7cSGreg Lehey if ($proc.p_stat) 310626ff208SDavid Schultz printf "%5d %08x %4d %5d %5d %06x %d %-10s ", \ 311030b9a7cSGreg Lehey $proc.p_pid, $aproc, \ 312626ff208SDavid Schultz $proc.p_cred->p_ruid, $pptr->p_pid, \ 313030b9a7cSGreg Lehey $proc.p_pgrp->pg_id, $proc.p_flag, $proc.p_stat, \ 314030b9a7cSGreg Lehey &$proc.p_comm[0] 315030b9a7cSGreg Lehey if ($proc.p_wchan) 316030b9a7cSGreg Lehey if ($proc.p_wmesg) 317030b9a7cSGreg Lehey printf "%s ", $proc.p_wmesg 318030b9a7cSGreg Lehey end 319030b9a7cSGreg Lehey printf "%x", $proc.p_wchan 320030b9a7cSGreg Lehey end 321030b9a7cSGreg Lehey printf "\n" 322030b9a7cSGreg Lehey if ($proc->p_flag & 4) 323030b9a7cSGreg Lehey btr $proc->p_threads.tqh_first->td_pcb->pcb_ebp 324030b9a7cSGreg Lehey else 325030b9a7cSGreg Lehey echo (not loaded)\n 326030b9a7cSGreg Lehey end 327030b9a7cSGreg Lehey end 328030b9a7cSGreg Lehey set $aproc = $proc.p_list.le_next 329030b9a7cSGreg Lehey if ($aproc == 0 && $nproc > 0) 330030b9a7cSGreg Lehey set $aproc = zombproc 331030b9a7cSGreg Lehey end 332030b9a7cSGreg Lehey set $proc = $aproc 333030b9a7cSGreg Lehey end 334030b9a7cSGreg Leheyend 335030b9a7cSGreg Leheydocument btpa 336030b9a7cSGreg LeheyShow backtraces for all processes in the system. 337030b9a7cSGreg Leheyend 338030b9a7cSGreg Lehey 339030b9a7cSGreg Lehey# Show backtrace for process selected with "defproc" 340030b9a7cSGreg Leheydefine btpp 341030b9a7cSGreg Leheybtr $myvectorproc->p_threads.tqh_first->td_pcb->pcb_ebp 342030b9a7cSGreg Leheyend 343030b9a7cSGreg Leheydocument btpp 344030b9a7cSGreg LeheyShow a backtrace for the process previously selected with 'defproc'. 345030b9a7cSGreg Leheyend 346030b9a7cSGreg Lehey 347030b9a7cSGreg Lehey# Specific stack fram of process selected with "defproc". 348030b9a7cSGreg Leheydefine fr 349030b9a7cSGreg Leheyset $fno = 0 350030b9a7cSGreg Leheyset $searching = 1 351030b9a7cSGreg Leheyset $frame = $myvectorproc->p_threads.tqh_first->td_pcb->pcb_ebp 352030b9a7cSGreg Leheywhile (($searching == 1) && (*(int *) $frame > 0xc0000000)) 353030b9a7cSGreg Lehey set $myebp = *(int *) $frame 354030b9a7cSGreg Lehey set $myeip = *(int *) ($frame + 4) 355030b9a7cSGreg Lehey if ($fno == $arg0) 356030b9a7cSGreg Lehey printf " frame %d at %p: ebp %8x, eip ", $fno, $frame, $myebp 357030b9a7cSGreg Lehey x/1i $myeip 358030b9a7cSGreg Lehey printf "Called from %8x, stack frame at %8x\n", *(int *) ($myebp+4), *(int *) $myebp 359030b9a7cSGreg Lehey printf "last 20 local variables:\n" 360030b9a7cSGreg Lehey x/20x ($myebp-80) 361030b9a7cSGreg Lehey printf "call parameters:\n" 362030b9a7cSGreg Lehey x/8x ($myebp+8) 363030b9a7cSGreg Lehey set $searching = 0 364030b9a7cSGreg Lehey else 365030b9a7cSGreg Lehey set $frame = $myebp 366030b9a7cSGreg Lehey set $fno = $fno + 1 367030b9a7cSGreg Lehey end 368030b9a7cSGreg Leheyend 369030b9a7cSGreg Leheyif ($searching == 1) 370030b9a7cSGreg Lehey echo frame not found\n 371030b9a7cSGreg Leheyend 372030b9a7cSGreg Leheyend 373030b9a7cSGreg Leheydocument fr 374030b9a7cSGreg LeheyShow the frame of the stack of the process previously selected with 'defproc'. 375030b9a7cSGreg Leheyend 376