GDB Scripting – Hidden Gems

Slides:



Advertisements
Similar presentations
® 4-2 CrossWind 4.1Overview Starting A Debugging Session Basic Debugging System-Level Debugging.
Advertisements

Memory & Storage Architecture Seoul National University Computer Architecture “ Bomb Lab Hints” 2nd semester, 2014 Modified version : The original.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Recitation: Bomb Lab June 5, 2015 Dipayan Bhattacharya.
Game Scripting By: Nicholas Haines. Aurora Neverwinter Toolset.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Game Scripting by: Nicholas Haines. What is Scripting? Interpreted Language Interpreted Language –As the game runs.
A Lightweight Interactive Debugger for Haskell Simon Marlow José Iborra Bernard Pope Andy Gill.
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Unit - V. Debugging GNU Debugger helps you in getting information about the following: 1.If a core dump happened, then what statement or expression did.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
Lab 9 Department of Computer Science and Information Engineering National Taiwan University Lab9 - Debugging I 2014/11/4/ 28 1.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Using the GNU Debugger (GDB)‏ Techzemplary Pvt.Ltd February 24 th 2008 Pranav Peshwe.
Java Thread Programming
DEBUG.
Computer System Laboratory
Instruction Set Architecture
Recitation 5: Attack Lab
A variable is a name for a value stored in memory.
Static and dynamic analysis of binaries
similar concepts, different syntax
CSCE 212 Computer Architecture
Recitation: Bomb Lab _______________ 18 Sep 2017.
More GDB, Intro to x86 Calling Conventions, Control Flow, & Lab 2
Dynamic Analysis ddaa.
CSE 374 Programming Concepts & Tools
Debugging Dwight Deugo
Recitation: Attack Lab
Debugging with gdb gdb is the GNU debugger on our CS machines.
Recitation: Bomb Lab _______________ 06 Feb 2017.
The Stack & Procedures CSE 351 Spring 2017
Introduction to Computer Systems
Machine-Level Programming III: Procedures
Recitation: Attack Lab
Important terms Black-box testing White-box testing Regression testing
Computer Architecture “Bomb Lab Hints”
Important terms Black-box testing White-box testing Regression testing
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Recitation: Attack Lab
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
GDB Scripting – Hidden Gems
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
The Stack & Procedures CSE 351 Winter 2018
Testing, debugging, and using support libraries
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Reverse Engineering for CTFs
Module 6: Debugging a Windows CE Image
Debugging Dwight Deugo
Some Assembly (Part 2) set.html.
General Computer Science for Engineers CISC 106 Lecture 03
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Computer Architecture and System Programming Laboratory
Recitation: Bomb Lab Your TAs September 16th 2019.
By Hugues Leger / Intro to GDB debugger By Hugues Leger / 11/16/2019.
Presentation transcript:

GDB Scripting – Hidden Gems Pierre-Marie de Rodat 2018-10-04

Print people Put_Line (“IN CONNECT...”); Put_Line (“ credentials: “ & Image (Credentials)); if Check_Credentials (Session, Credentials) then Put_Line (“ credentials accepted”); Session.Authenticated := True; end if; Put_Line (“<--”); The ones that instrument/add printing code, recompile and re-run to find out what’s wrong

Debugger people $ gdb ./test_connect (gdb) break connect (gdb) run (gdb) print credentials (gdb) next The ones that just run a debugger and just look at what happens as it happens

GDB Tour GDB lets you inspect the program as it runs Low-level aspects (registers, assembly, …) Source-level aspects (source locations, variables, …)

GDB Tour: low-level view (1/3): registers (gdb) info registers rax 0x1 1 rbx 0x0 0 rcx 0xfbad0087 4222419079 rdx 0x0 0 rsi 0x7ffff7f8e710 140737353672464 […] r15 0x0 0 rip 0x403d01 0x403d01 <main+9> eflags 0x206 [ PF IF ]

GDB Tour: low-level view (2/3): ASM code (gdb) disassemble Dump of assembler code for function _ada_main: 0x0000000000403cf8 <+0>: push %rbp 0x0000000000403cf9 <+1>: mov %rsp,%rbp 0x0000000000403cfc <+4>: push %rbx 0x0000000000403cfd <+5>: sub $0x8,%rsp => 0x0000000000403d01 <+9>: mov $0x428248,%eax 0x0000000000403d06 <+14>: mov $0x428258,%edx 0x0000000000403d0b <+19>: mov %rax,%rcx 0x0000000000403d0e <+22>: mov %rdx,%rbx […]

GDB Tour: low-level view (3/3): memory (gdb) x/3gx $rax 0x428248: 0x77202c6f6c6c6548 0x00000021646c726f 0x428258: 0x0000000d00000001 (gdb) x/14bo $rax 0x428248: 0110 0145 0154 0154 0157 054 040 0167 0x428250: 0157 0162 0154 0144 041 0 (gdb) x/1s $rax 0x428248: "Hello, world!["00"]"

GDB Tour: source-level view Corresponding location in sources: (gdb) list 8 9 procedure Initialize 10 (R : out Record_Type; 11 I : Integer; C : Character) is 12 begin 13 R.A := I; 14 R.B := 2 * I; 15 R.C := C; 16 end Initialize;

GDB Tour: source-level view Inspection of arguments and local variables, evaluation of arbitrary expression: (gdb) info args r = (a => -8864, b => 32767, c => 84 'T') i = 10 c = 65 'A' (gdb) info local b = 75 (gdb) print r.b $1 = 32767

GDB Tour: source-level view Inspection of the call stack: (gdb) info local b = 75 (gdb) backtrace #0 main.initialize (r=..., i=10, c=65 'A') at main.adb:17 #1 0x0000000000403dd3 in main () at main.adb:23 (gdb) up 23 Initialize (R, 10, 'A'); r = (a => -8864, b => 32767, c => 100 'd')

GDB Tour: source-level view Pause on interesting events: when specific code is executed… (gdb) catch exception (gdb) break engine.adb:549 (gdb) break Display.Text.Show_Line (gdb) run Breakpoint 3, display.text.show_line (line=...) at display-text.adb:123 123 Line_Length : constant Natural := Line’Length;

GDB Tour: source-level view Pause on interesting events: … when data changes (gdb) watch Display.Text.Current_Line (gdb) continue Hardware watchpoint 4: Display.Text.Current_Line Old value = 56 New value = 57 display.text.show_line (line=...) at display-text.adb:130 130 Current_Line := Current_Line + 1;

GDB Scripting (finally) GDB decodes the state of your program You can extend it with scripts! Programmatic access to program state Custom breakpoints, custom commands Pretty-printers Auto-loading extensions You can also run Python code directly inside GDB: (gdb) python-interpreter ‘Hello, {}!’.format(‘world’) ‘Hello, world!’ (gdb) pi import antigravity (gdb) source my_gdb_extensions.py

GDB Scripting: programmatic inspection For instance, somehow re-implement the “info local” command: (gdb) pi frame = gdb.selected_frame() (gdb) pi {symbol.name: str(symbol.value(frame)) for symbol in frame.block() if symbol.is_variable} {'r': "(a => 10, b => 75, c => 65 'A')"}

GDB Scripting: custom breakpoints Have you ever wanted to do something like this? (gdb) break buggy_function if the caller is some_function i.e. put a breakpoint on “buggy_function” but don’t stop there unless “some_function” is the caller. The Python API makes this kind of feature available.

GDB Scripting: custom breakpoints class BreakIfCaller(gdb.Breakpoint): def __init__(self, spec, caller_name): super(BreakIfCaller, self).__init__(spec) self.caller_name = caller_name def stop(self): try: caller_name = (gdb.selected_frame() .older().function().name) except gdb.error: return False return caller_name == self.caller_name # And then in GDB: (gdb) pi BreakIfCaller(‘buggy_function’, ‘other_function’)

GDB Scripting: custom commands The Python API lets you create new commands Subclass gdb.Command and override .invoke The previous example could be exposed as: (gdb) break-if-caller buggy_function some_function

GDB Scripting: pretty-printers Have you ever tried to inspect the content of a vector? -- In your Ada source code… package Int_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Integer); V : Int_Vectors.Vector; # … then in GDB: (gdb) print v $1 = (elements => 0x459260, last => 3, tc => (busy => 0, lock => 0)) # Whereas we want: $1 = break.int_vectors.vector of length 3 = {1, 2, 3}

GDB Scripting: pretty-printers GDB pretty-printers are Python objects Register them to GDB and override their methods to: make them match objects to pretty-print yield text to display They automatically compose (e.g. nested containers) Pretty-printers for standard containers in GNAT Pro (https://github.com/AdaCore/gnat-gdb-scripts)

GDB Scripting: auto-loading extensions Writing extensions is cool Have GDB automatically load them is even better: my_program-gdb.py OR my_shared_lib-gdb.py .debug_gdb_script section in executables Requires some “safe path” tuning for security

GDB Scripting The Python API offers much more API reference in GDB’s documentation Sky is the limit!