Debugging and Debugger. Terminology. GNU gcc and gdb. Debugging C programs in Unix and Windows Environments - Part One

Similar documents
CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

CS354 gdb Tutorial Written by Chris Feilbach

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

CSE 351. GDB Introduction

Software Development With Emacs: The Edit-Compile-Debug Cycle

Using gdb to find the point of failure

Introduction to Cygwin Operating Environment

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

CMPSC 311- Introduction to Systems Programming Module: Debugging

Lecture 07 Debugging Programs with GDB

Your code must have been compiled with the -g compiler option. Example:

Program Design: Using the Debugger

Understanding the Program Run

CMPSC 311- Introduction to Systems Programming Module: Debugging

1 A Brief Introduction To GDB

CSE 374 Programming Concepts & Tools

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Programming Studio #9 ECE 190

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

C Program Development and Debugging under Unix SEEM 3460

gdbtui - Linux Command

Problem Set 1: Unix Commands 1

Your code must have been compiled with the -g compiler option. Example:

Basic functions of a debugger

A Tutorial for ECE 175

Debugging. John Lockman Texas Advanced Computing Center

18-600: Recitation #3

Exercise Session 6 Computer Architecture and Systems Programming

Source level debugging. October 18, 2016

Using the Command Line

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

GDB QUICK REFERENCE GDB Version 4

CS2141 Software Development using C/C++ Debugging

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Class Information ANNOUCEMENTS

CS 314 Principles of Programming Languages. Lecture 9

Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Chapter 2. Basics of Program Writing

Debugging Techniques. CEFET Engineering Week

Scientific Programming in C IX. Debugging

Laboratory 1 Semester 1 11/12

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

PROGRAMMAZIONE I A.A. 2017/2018

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference

Structured programming

Lab6 GDB debugging. Conventions. Department of Computer Science and Information Engineering National Taiwan University

Today s presentation. Git gdb Project 1

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CS 11 C track: lecture 6

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

The Dynamic Debugger gdb

PRACE Autumn School Basic Programming Models

We first learn one useful option of gcc. Copy the following C source file to your

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Computer Labs: Debugging

Programming Tips for CS758/858

Supplement: Visual C++ Debugging

CSE 410: Systems Programming

Using a debugger. Segmentation fault? GDB to the rescue!

Computer Science II Lab 3 Testing and Debugging

Introduction to the Command line. Introduction to the command line. Introduction to the Command line. GNU/Linux at South Wales

Intro to Segmentation Fault Handling in Linux. By Khanh Ngo-Duy

DEBUGGING ON FERMI PREPARING A DEBUGGABLE APPLICATION GDB. GDB on front-end nodes

CSE 124 Discussion (10/3) C/C++ Basics

CS/COE 0449 term 2174 Lab 5: gdb

Reviewing gcc, make, gdb, and Linux Editors 1

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

dbx90: Fortran debugger March 9, 2009

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Using the GNU Debugger

Data and File Structures Laboratory

Using the GNU Debugger

GDB 1 GDB 2 GDB. Fortran Pascal GDB 4. hoge.c. Fig. 1. calc.c. Fig GDB. GDB Debian. # apt-get install gdb

Hands-on Workshop on How To Debug Codes at the Institute

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

EE 355 Lab 3 - Algorithms & Control Structures

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

Figure 1: 128-bit registers introduced by SSE. 128 bits. xmm0 xmm1 xmm2 xmm3 xmm4 xmm5 xmm6 xmm7

Debugging OpenFOAM implementations with GDB

CS201 Lecture 2 GDB, The C Library

ECE 3210 Laboratory 1: Develop an Assembly Program

GCC: the GNU Compiler Collection

Code::Blocks Student Manual

The CS-220 Development Environment

a translator to convert your AST representation to a TAC intermediate representation; and

Programming in C in a UNIX environment

Development Environment & Linux Guide

Computer Labs: Debugging

Compiling and Linking

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Transcription:

Debugging C programs in Unix and Windows Environments - Part One ICT106 Fundamentals of Computer Systems Eric Li Debugging and Debugger The process of finding bugs in code called Debugging; A must-have skill for every programmer; Debugger is a utility tool to assist a programmer to debug code; A compulsory component included in Integrated Development Environment (IDE), such as Visual C, Borland Turbo C etc. ICT106_Pract_Week 3 1 ICT106_Pract_Week 3 2 Terminology Breakpoints Stop the execution of the program at a specific source line; Enable to monitor internal changes at runtime; Stepping Walk-through lines in code; Functions: Step Over, Step Into, Step Out; Variable watches Examine variable contents during program execution; GNU gcc and gdb gdb is a command line Unix debugger; Use debugging information generated by gcc: gcc -g source.c -o executable.exe gdb executable.exe ICT106_Pract_Week 3 3 ICT106_Pract_Week 3 4 1

Essential Commands Installing gdb under Cygwin break [file:]function run [arglist] bt print expr c next, step watch info locals help - set breakpoint - run program - print function stack - print the result of expr - continue execution - next instruction/line - watch an expr - local variables - built-in help Go to Cygwin homepage: www.cygwin.com Click on one of the install Cygwin now icons and download setup.exe ICT106_Pract_Week 3 5 ICT106_Pract_Week 3 6 Installation (2) Click Run button Installation (3) Run the program as a local user ICT106_Pract_Week 3 7 ICT106_Pract_Week 3 8 2

Installation (4) Choose Install from Internet Installation (5) Choose Install for Just Me ICT106_Pract_Week 3 9 ICT106_Pract_Week 3 10 Installation (6) - IMPORTANT Select the local package directory (H:\ in Murdoch network) Installation (7) Choose Use IE5 Settings for Internet Connection ICT106_Pract_Week 3 11 ICT106_Pract_Week 3 12 3

Installation (8) Add ICT106 Murdoch Download Site http://ftp.it.murdoch.edu.au/ict106/ict106%20utility%20programs/cygwi Installation (9) Check gdb package under Devel Category nrelease/ ICT106_Pract_Week 3 13 ICT106_Pract_Week 3 14 Using gdb /* File: test2-d.c ICT106 test program Purpose: To demonstrate passing array arguments to a function */ #include <stdio.h> int Sum (int[], int); // prototype int main ( ) { int coins[6] = { 5, 10, 20, 50, 100, 200 }; int sumofcoins; sumofcoins = Sum(coins, 6); printf("\nthe sum of coins is %d\n", sumofcoins); return 0; } // end main int Sum(int A[ ], int size ) { int i = 0; int total = 0; while (i < size) { total = total + A[i]; i = i+1; } /* end while */ return total; } /* end Sum */ ICT106_Pract_Week 3 15 ICT106_Pract_Week 3 16 4

Compile with -g option Note the size difference between a normal executable file and a debug-ready one; Use strip command to remove debug information; The new output file is 3.5KB (even smaller!) Read strip s manual to find out why. ICT106_Pract_Week 3 17 ICT106_Pract_Week 3 18 Using gdb Type gdb lect_3.exe to start Choosing a breakpoint #include <stdio.h> int Sum (int[], int); // prototype int main ( ) { int coins[6] = { 5, 10, 20, 50, 100, 200 }; int sumofcoins; sumofcoins = Sum(coins, 6); // Add a breakpoint HERE printf("\nthe sum of coins is %d\n", sumofcoins); return 0; } // end main ICT106_Pract_Week 3 19 ICT106_Pract_Week 3 20 5

Setting breakpoints Command to add a breakpoint break lect_3.c:13 Execute run lect_3.exe causing the program stops at line 13: Monitoring local variables Option 1: Use info locals to display ALL locally defined variables Or, Option 2: Use print command to show a specified variable 13 sumofcoins = Sum(coins, 6); ICT106_Pract_Week 3 21 ICT106_Pract_Week 3 22 Printing variables Step & Next commands next" - causes the debugger to execute the current line, and stop again. It steps over a function call. "step" - similar to next, but it steps into a function call - break at the beginning of that function. step is useful for debugging nested code. ICT106_Pract_Week 3 23 ICT106_Pract_Week 3 24 6

finish command To step out from a function call. If it is main function, it causes termination of the program ICT106_Pract_Week 3 25 ICT106_Pract_Week 3 26 ICT106_Pract_Week 3 27 7