CSI605 Introduction to gdb. Compiling for gdb Execution I often compile as follows gcc -g -Wall -omyprog myprog.c g++ -g -Wall -omyprog myprog.c It suffices.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

CS 450 Module R1. R1 Introduction In Module R1, you will implement a user interface (command handler). There are a couple of options: ▫Command Line: interface.
Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Utilizing the GDB debugger to analyze programs Background and application.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Chapter 5: Loops and Files.
CSE 303 Lecture 13a Debugging C programs
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
Gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and.
Debugging Cluster Programs using symbolic debuggers.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Introduction to Shell Script Programming
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Linux Operations and Administration
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
UBI >> Contents Chapter 2 Software Development tools Code Composer Essentials v3: Code Debugging Texas Instruments Incorporated University of Beira Interior.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
Chapter Three The UNIX Editors.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Data Display Debugger (DDD)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
Chapter Six Introduction to Shell Script Programming.
S OME USEFUL D EBUG C OMMANDS FOR C LEAR -S PEED S OFTWARE D EVELOPMENT K IT -- COMMANDS FROM CHAP.7 By: Pallav Laskar.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 What we'll cover here l Using the debugger: Starting the debugger Setting.
Loops and Files. 5.1 The Increment and Decrement Operators.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CSI605 Introduction to ddd. ddd ddd stands for the Data Display Debugger ddd is a graphical environment that resides on top of gdb We recall that gdb.
COP 3530 Spring 12 Discussion Session 1. Agenda 1.Introduction 2.Remote programming 3.Separate code 4.Compile -- g++,makefile 5.Debug -- gdb 6.Questions?
THE C PROGRAMMING ENVIRONMENT. Four parts of C environment  Main menu  Editor status line and edit window  Compiler message window  “Hot Keys” quick.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
17/02/2016S. Ponce / EP-LBC1 Debugging Under Linux Sebastien Ponce Friday, 8 March 2002.
1 Week # 4 Introduction to PDM PDM is a workbench environment that lets programmers and system operators navigate the three levels of the AS/400’s object-based.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
 Prepared by: Eng. Maryam Adel Abdel-Hady
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
 Wind River Systems, Inc Chapter - 4 CrossWind.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
GDB Introduction And Lab 2
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
CSCI 4061 Recitation 2 1.
DEBUG.
Computer System Laboratory
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
Dynamic Analysis ddaa.
CSCE 212Honors Computer Organization
Debugging with gdb gdb is the GNU debugger on our CS machines.
gdb gdb is the GNU debugger on our CS machines.
Debugging with Eclipse
Topics Introduction to File Input and Output
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
Topics Introduction to File Input and Output
CSCE 212Honors Computer Organization
Debugging.
Makefiles, GDB, Valgrind
Debugging with Eclipse
Presentation transcript:

CSI605 Introduction to gdb

Compiling for gdb Execution I often compile as follows gcc -g -Wall -omyprog myprog.c g++ -g -Wall -omyprog myprog.c It suffices to use gdb -g -omyprogram myprogram.c We note that -g and -O are incompatiable

Launching gdb We launch via gdb program gdb program core-dump ~/CPP]$ gdb lab1 GNU gdb with Linux support Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... Note that the core file may be inserted later with the core command gdb launch options -d dir (tells gdb where to look for the source file) -x file (tells gdb to execute the commands in file during startup, can include multiple files) -nx (don’t execute commands from the initialization file(normally.gdbinit)) -q(don’t print the introductory messages during startup) -help(print a message showing all of the command line options and then quit) -batch(run in batch mode; execute any startup files(unless they are surpressed),followed by any files specified with the -x options, and then exit with status 0)

Basic gdb Commands gdb like virtually all programs supports a set of internal help files help aliases help breakpoints help data help files help internals help obscure help running help stack help status help support help users

Listing a File We use the command list command listing a file gdb lab4 GNU gdb 4.18 Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-mandrake-linux"... (gdb) list 30 void print_information (char name[], char ID[],char address[], char city[],char state[], char zip_code[],double total_balance); void exit(void); int main() 36 { 37 //Variable Declarations double total_balance=0; // listing by line numbers list 1,30 listing a routine list myroutine listing quirks gdb lists 10 lines each time list is called gdb can’t take illustrate conditional compilation they will appear verbatim Non existent function file message Function name not defined

Executing a Program We execute our program within the gdb environment using the run command Here is a typical session of running a program called myprogram with an input file of input values and an output file of outputfile gdb myprogram (gdb) run outputfile Program exited with code n. (gdb) Repeating the run command directs gdb to repeat the run with the same parameters set args (allows us to change the args command) show args (allows us to show the current argument settings) To terminate a running program one executes CTRL-C This returns control to the gdb environment quit quits the gdb environment backtrace is used to backtrap the steps in an abnormally terminated program

Printing Data gdb provides us with a rich set of printing capabilities We effectively can print any value that is valid in the programming language that we are debugging in function calls data structures value history artificial arrays whatis x This tells us what type x is print x This prints the variable x If p is a pointer it makes sense to execute the following to see the value that p points to print *p

Some More Esoteric Prints When we print something we may see a $1 or $2 or $3 next to the outputted value We may identify these values in future expressions using these identifiers print $1 - 1 Examining arrays To examine the next 10 entries after h execute print Given an identifier on this array of $14 we may then examine its sixth entry with print $14[5] Printing Pretty Sometimes vertical columns may be preferable This are obtainable with set print array

Breakpoints This allows one to temporarily stop execution of the program to allow examination or modification of the variables Some of the ways that it can be called include break line-number Stop the program just before executing the given line. break function-name Stop the program just before entering the named function. break line-or-function if condition If the following condition is true, stop the program when it reaches the given line or function. break routine-name Set a breakpoint at the entrance to the specified routine. When the program is executing gdb temporarily halts the program at the first executable line of the given function.

More About Breakpoints Programs built from multiple source files break filename:line-number break filename:function-name Conditional break points break line-or-function if expr Good for stopping in the middle of loops watch points (like conditional breakpoints) watch x <500

More About Watch Points On virtually all workstations watchpoints slow execution down by a factor of around 100 Here is an effective strategy for watchpoint use Use regular breakpoints to get the program as close as possible to the point of failure Set a watchpoint Let the program continue execution with the continue command Let your program run overnight

Continuing to Run After a Breakpoint We continue execution after a breakpoint with the continue command Program execution will continue until another breakpoint or error is hit One can abbreviate continue as c One can tell gdb to continue execution through a number of breakpoints using c n c 3

Managing Breakpoints info breakpoints Shows us what breakpoints are currently active delete # This deletes the breakpoint whose identification number is given by # clear # (clear function_name) Deletes all breakpoints on line # of the code or deletes all breakpoints at the entry to function_name

Enabling and Disabling Breakpoints disable # disable breakpoint identified by # enable # enable breakpoint identified by # enable once # enable breakpoint identified by # only once

Examining and Modifying Variable Values whatis Identifies the type of a variable or array set variable Assigns a value to a variable print This is used for assigning and printing values print myfunction(x,y,z) works

Example of Examination and Modification -I ~/CPP]$ gdb lab1 GNU gdb with Linux support Copyright 1998 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux"... (gdb) break 44 Breakpoint 1 at 0x8048b07: file lab1.cpp, line 44. (gdb) run Starting program: /home/jsolka/CPP/lab1 Welcome to Sister Joan Hospital Database Written by Beth Solka. This database will store patient information. It will bill the patient and print out information. Press enter to continue. Menu Screen. 1. Enter Patient Information. 2. Bill Patient. 3. Print patient Information. 4. Exit. Enter choice: 1

Example of Examination and Modification -II Enter Patient Information. Enter name:Jeff Solka Breakpoint 1, main () at lab1.cpp:44 44 cout << "Enter social security number: "; (gdb) where #0 main () at lab1.cpp:44 #1 0x4008ecb3 in __libc_start_main (main=0x80489b0, argc=1, argv=0xbffffb04, init=0x , fini=0x8048d8c, rtld_fini=0x4000a350, stack_end=0xbffffafc) at../sysdeps/generic/libc-start.c:78 (gdb) whatis name type = char [30] (gdb) print name $1 = "Jeff Solka\000¿\001\000\000\000¸úÿ¿\233\211\004\b\224 \004\b\f¡" (gdb) whatis choice type = int (gdb) print choice $2 = 1 (gdb) set choice = 2 (gdb) print choice $3 = 2

Single-Step Execution next Takes a single step but executes an entire function if it encounters a call step Takes a single step

Function Debugging call function_name Call and execute a function Will not print the function return value if it is void finish Finish executing the current function and print its return value if any. return value Cancel execution of the current function, returning value to the caller.

Automatic Execution of Commands Here is the syntax of this type of beast commands # … list_of_commands end # is the breakpoint number that we would like to link our commands to ex. commands 23 echo value of x \n print x end

Convenience Variables gdb lets you define your own local variables They are typically of the form $name They are assigned using set variable = expression They can be initialized and printed at the same time using print print $x=12

Working with Source Files in gdb search text Searches forward for the next occurrence of text reverse-search text Searches backwards for the next occurrence of text We may add directories directory1 and directory2 to the gdb directory search path at launch time using gdb -d directory1 -d directory2 myprogram After launch the directory search path may be modified to add the directory directory1 using directory directory1 To revert the search path back to the default, execute] directory

User Defined Command Sequences Using the define Command Here is the standard format of a user defined command sequence using define define my_command … command1 … … command2 … … command3 … end One can also use hooks and script files

Execution of UNIX Commands Within gdb To execute a UNIX command within gdb execute shell command_name For example shell pwd * There is also a nice relationship between gdb and emacs *

Command Abbreviations in gdb Here are most of the gdb commands along with there associated abbreviations breakb continuec deleted framef helph infoi listl nextn printp quitq runr steps

Attaching to an Existing Process gdb allows one to attach to a program that is already running gdb my_program my_program_pid Note that gdb will initially inform you that there is no file by the name of your process id. This is just gdb checking to see if there is a core file available. Alternatively one can start gdb and then use the attach command to attach to a running process The process is then restarted with detach

gdb Flash Card backtracePrints location and stack trace. breakpointSets breakpoint. cd clearDelete the breakpoint where you just stopped. commandsProvide commands to be executed at breakpoint deleteDelete breakpoint or watchpoint. displayDisplay variables or expressions at program stop. downMove down the stack frame. frameSelect a frame for the next continue. infoDisplay program information. jumpStart execution at another point in the program. killAbort the process under gdb’s control. listList the source file for the executing process. nextExecute the next source line executing a function in its entirety. printPrint the value of a variable or an expression. pwd ptypeShow the contents of a data type. quitExit gdb. reverse-searchSearch backwards. runExecute the program. searchSearch forwards. set variableAssign a value to a variable. signalSend a signal to the running process. stepExecute the next source line stepping into the function if necessary. undisplayReverse a display command. untilFinish the current loop. upMove up the stack frame to make another function the current one. watchSet a watchpoint in the program. whatisPrint the type of a variable or function.