debugger principle overview & gdb tricks
Post on 21-Oct-2014
571 views
DESCRIPTION
TRANSCRIPT
![Page 2: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/2.jpg)
Outline
1 Debugger Under the Hood
2 GDB the Basics
3 Bonus
4 A Little Assembly
![Page 3: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/3.jpg)
Outline
1 Debugger Under the Hood
2 GDB the Basics
3 Bonus
4 A Little Assembly
![Page 4: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/4.jpg)
Outline
1 Debugger Under the Hood
2 GDB the Basics
3 Bonus
4 A Little Assembly
![Page 5: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/5.jpg)
Outline
1 Debugger Under the Hood
2 GDB the Basics
3 Bonus
4 A Little Assembly
![Page 6: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/6.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
What and How
I what debuggers doI how to take control of the targetI how to set up breakpointsI how to map instructions with source code
dutor Something On GDB And Debugging
![Page 7: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/7.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
ptrace
I long ptrace(request, pid, addr, data)I tracer/traceeI signal/waitpidI PTRACE_TRACEMEI PTRACE_ATTACHI PTRACE_CONT/SINGLESTEPI PTRACE_PEEKDATA/POKEDATA
dutor Something On GDB And Debugging
![Page 8: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/8.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
breakpoints
I hardware, the debug registersI software, INT 3
dutor Something On GDB And Debugging
![Page 9: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/9.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Debug Info
I DWARF, designed for various languages and ABIsI source lines and instructionsI names and adressesI utilities
I readelfI stripI nm/stringsI c++filt
dutor Something On GDB And Debugging
![Page 10: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/10.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
$ cc test.cpp -g$ gdb a.out$ gdb a.out core .1234$ gdb --args a.out arg1 arg2
$ gdb(gdb) file a.out(gdb) start arg1 arg2(gdb) run arg1 arg2(gdb) run <if >of
dutor Something On GDB And Debugging
![Page 11: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/11.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
(gdb) next(gdb) step(gdb) continue(gdb) nexti(gdb) stepi(gdb) finish(gdb) until LINE
dutor Something On GDB And Debugging
![Page 12: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/12.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
(gdb) break(gdb) break test.cpp :32(gdb) break foo if p == NULL(gdb) break 32 thread 0(gdb) info breakpoints(gdb) disable 1(gdb) enable 2(gdb) delete 3(gdb) ignore 4 123(gdb) commands 5set p = malloc (1024)end
dutor Something On GDB And Debugging
![Page 13: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/13.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
(gdb) print n(gdb) print/x p(gdb) print object(gdb) x &n
dutor Something On GDB And Debugging
![Page 14: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/14.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
(gdb) backtrace(gdb) info locals(gdb) info threads(gdb) thread 16(gdb) list(gdb) list foo(gdb) list -
dutor Something On GDB And Debugging
![Page 15: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/15.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Basic Usage of GDB
I startI runningI breakpointsI print/xI othersI help
(gdb) help print(gdb) help set(gdb) help set print
dutor Something On GDB And Debugging
![Page 16: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/16.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly
Miscs and Bonus
I p $ripI i regI p {tair::StorageManager}0x608048I p *array@10I display/i $ripI x/40a $rspI l *0x608048I watch exprI return 0I enable breakpointsI gcore
I set follow-fork-mode childI set scheduler-locking onI symbol-fileI add-symbol-fileI i sharedlibraryI gcc test.cpp -g -g3I maintenance info sectionsI set logging onI set print pretty onI gdb -p 1234 -ex ‘set n=0’ -batchI Ctrl-X Ctrl-A
dutor Something On GDB And Debugging
![Page 17: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/17.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Syntax
I instruction src, des The first operand is the source, the second is the destinationI %register Register names are prefixed with a %, %% in inlined assemblyI $literal Literal values are prefixed with $I instruction{b,w,l,q} The instruction suffix denotes the operand sizeI seg:off(base, index, scale) Memory access
dutor Something On GDB And Debugging
![Page 18: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/18.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Common Used Registers
I rax, eax, ax, alI rbx, ebx, bx, blI rcx, ecx, cx, clI rdx, edx, dx, dlI rsi, esi, siI rdi, edi, diI rbp, rspI r8-r15I xmm0-xmm7
dutor Something On GDB And Debugging
![Page 19: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/19.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Stack Frame
+------+ +-----------------------+ || | | return address | || v +-----------------------+ || %ebp----> | old %ebp | || +-----------------------+ || %esp----> | local variables | || +-----------------------+ stack || | argument 2 | | growing| +-----------------------+ direction || | argument 1 | || +-----------------------+ || | return address | || +-----------------------+ |+--------------+ old %ebp | <-----%ebp |
+-----------------------+ || local variables | <-----%esp |+-----------------------+ || | _|_| Red Zone | \ /| | ’+-----------------------+
dutor Something On GDB And Debugging
![Page 20: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/20.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Calling Conventions
I Arguments, rdi, rsi, rdx, rcx, r8, r9, xmm0-xmm7I Arguments on stackI Return, rax, xmm0I Stack Frame, rbp, rsp
dutor Something On GDB And Debugging
![Page 21: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/21.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Data Representatoin
I IntegersI FloatsI StructuresI ClassesI ArraysI PC-relative Reference
dutor Something On GDB And Debugging
![Page 22: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/22.jpg)
That’s All.
![Page 23: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/23.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Basic Format
1 asm ( <assembler template >2 : ["constraints"(var)] [,"constraints"(var)] /* output operands */3 : ["constraints"(var)] [,"constraints"(var)] /* input operands */4 : ["register"] [,"register"] [,"memory"] /* clobbered registers */5 );
Examplesasm("nop":::) nop
asm("incl %%eax") Access register directlyasm("movl $1, %0":"m"(ret)) Write to C variable
asm("movl %0, %%eax"::"m"(ret)) Read from C variableasm("addl %1, %0":"+"(a):"r"(b)) a = a + b
asm("incl global_var":::"memory") Write to arbitrary memory
dutor Something On GDB And Debugging
![Page 24: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/24.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Constraints
r register operands, any of the followings.a %rax, etc.b %rbx, etc.c %rcx, etc.d %rdx, etc.S %rsi, etc.D %rdi, etc.
q register operands, any of a, b, c, d.m memory operands.f floating poing register.
[0-9] matching constraints, both for inputand output.
dutor Something On GDB And Debugging
![Page 25: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/25.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Constraints
r register operands, any of the followings.a %rax, etc.b %rbx, etc.c %rcx, etc.d %rdx, etc.S %rsi, etc.D %rdi, etc.
q register operands, any of a, b, c, d.m memory operands.f floating poing register.
[0-9] matching constraints, both for inputand output.
asm ("int $0x80\n\t": "=a"(ret): "0"(4), "b"(fd), "c"(buf), "d"(n));
dutor Something On GDB And Debugging
![Page 26: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/26.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Constraints Modifiers
= operand is write-only.+ operand is used as both read and write.
Write-Only Exampleasm ("movl $1, %0" : "=r"(ret));
dutor Something On GDB And Debugging
![Page 27: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/27.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Constraints Modifiers
= operand is write-only.+ operand is used as both read and write.
Read-Write Exampleasm ("incl %0" : "+r"(ret));
dutor Something On GDB And Debugging
![Page 28: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/28.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
Call C Functions
1 int2 main()3 {4 char *fmt = "Hello , %s\n";5 char *s = "World";6 int ret = 0;7 asm ("callq printf\n\t"8 : "=a"(ret)9 : "D"(fmt), "S"(s));
10 printf("ret: %d\n", ret);11 return 0;12 }
dutor Something On GDB And Debugging
![Page 29: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/29.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
ExamplesDo System Call
1 /* syscall write */2 inline int as_write(int fd , char *buf , size_t n) {3 int ret;4 asm (5 "int $0x80\n\t"6 : "=a"(ret)7 : "0" (1), "D"(fd), "S"(buf), "d"(n)8 );9 return ret;
10 }
dutor Something On GDB And Debugging
![Page 30: Debugger Principle Overview & GDB Tricks](https://reader030.vdocument.in/reader030/viewer/2022020720/544594e7b1af9fdf068b45cc/html5/thumbnails/30.jpg)
Debugger Under the Hood Basics Bonus A Little Assembly Inlined Assembly Examples Disassembling
DisassemblingUsing GDB
dutor Something On GDB And Debugging