Makefiles, GDB, Valgrind

Slides:



Advertisements
Similar presentations
Utilizing the GDB debugger to analyze programs Background and application.
Advertisements

Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
1 The Makefile Utility ABC – Chapter 11,
CSE 303 Lecture 13a Debugging C programs
CS Lecture 11 Outline Compiling C programs using gcc Archiving modules Using Makefiles Debugging using gdb Assignment 3 discussion Lecture 111CS.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CS465 - Unix C Programming (cc/make and configuration control)
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
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.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Copyright © 2009 Techtronics'09 by GCECT 1 Presents, De Code C De Code C is a C Programming competition, which challenges the participants to solve problems.
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.
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.
Old Chapter 10: Programming Tools A Developer’s Candy Store.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
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)
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.
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Makefile Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile"
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
Lecture 6 UNIX Development Tools. Software Development Tools.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Using the GNU Debugger (GDB)‏ Techzemplary Pvt.Ltd February 24 th 2008 Pranav Peshwe.
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.
Lecture 3 Translation.
a.k.a how we test Your code
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
Week 3-4 Control flow (review) Function definition Program Structures
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
The Machine Model Memory
Brief Intro to Make CST494/ Gannod.
Debugging Memory Issues
CSE 374 Programming Concepts & Tools
Valgrind Overview What is Valgrind?
CSE 374 Programming Concepts & Tools
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
Debugging with gdb gdb is the GNU debugger on our CS machines.
Editor, Compiler, Linker, Debugger, Makefiles
Advanced course of C/C++
Checking Memory Management
gdb gdb is the GNU debugger on our CS machines.
Lab: ssh, scp, gdb, valgrind
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Recitation: C Review TA’s 19 Feb 2018.
Lab: ssh, scp, gdb, valgrind
Getting Started: Developing Code with Cloud9
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]
CMPSC 60: Week 4 Discussion
a.k.a how we test Your code
CSE 303 Concepts and Tools for Software Development
Debugging.
Valgrind Overview What is Valgrind?
Presentation transcript:

Makefiles, GDB, Valgrind Unix System Tools Makefiles, GDB, Valgrind

Compiling C We have already been compiling C with gcc GNU compiler In addition to GNU there is cc (the default system compiler), clang (on a mac), and icc (Intel Compiler) The C standard is a minimum specification for these compilers Each compiler can have different additional options and libraries

Compiler flags Compiler flags are additional options to change the way your code compiles Many of you have already used -lm tells the compiler to load the math library Using compiler flags is as simple as preceding the flag code with a dash gcc myfile.c -o myprog -lm

More Compiler flags Other compiler flags we will use: -Wall -g Add all warnings -g Add debugging information -std=gnu11 sets the standard to gnu11 -pedantic Issue all the warnings demanded by strict ANSI C

Compiling with Make Make is a command line program on *nix systems with two modes: Uses system defaults for simple compilation of a single file Looks for a compilation script called a makefile for complex compilation

Makefiles make without arguments looks for ‘makefile’ or ‘Makefile’ script file in the current folder makefile is just another text file A makefile is a script containing Project structure (files, dependencies) Instructions for a program’s creation

Example Makefile P=<program name> OBJECTS= CFLAGS= -g -Wall -std=gnu11 $(P): $(OBJECTS) gcc $(CFLAGS) $(P).c -o $(P)

Makefile Syntax Makefile is a line-oriented, whitespace sensitive file That means each command must fit on a single line Spaces are not the same as tabs Makefiles use whitespace to know what instructions should be run

Makefiles: Targets Executing a make with a parameter jumps to that ‘target’ within the makefile and runs the script target: flags that mark locations in the file targets are fully left justified followed by a ‘:’ Examples: all: main.o: When called without a target, make automatically looks for the target ‘all’ if target ‘all’ is not found, it runs the first target

Makefiles: Dependencies Dependencies follow targets If the dependency is a file name, make will check timestamps to see if the file has changed If the dependency is a target, make will check to see if that target has been run Example: all: prog.o prog.o: prog.c Checks timestamp of prog.c to see if it has been updated

Makefiles: Scripts A short script should follow each target, each command should be a single line Scripts are a series of commands that get executed automatically A command requires one tab (do not use spaces), and immediately follows the target Example: prog: prog.c gcc prog.c -o prog

Object Files What if you have a large program of 10000 files? Do you recompile all 10000 files every time 1 file changes? You can compile down all files to object files first Just the machine code without any additional libraries The -c flag compiles to an object file gcc -c file.c -o file.o You can then link together object files to make an executable gcc file.o -o prog

Only Compile What’s Changed How can this save compilation time? Make checks the timestamps of its dependencies If the dependency target modified time is newer than the target output file, then it needs to be recompiled Only the files with more recent modified timestamps get recompiled The other object files can be left as is Everything then gets linked back together

Makefile Variables make allows us to put variables in the makefile Variables are conventionally uppercase Variable are essentially text replacement, so whatever you set the variable to is what will be put into its place To use a variable, you must use the $ and parenthesis P= #this would be a comment … $(P)

Classwork: Makefiles

GDB gdb is a unix environment debugger Type "gdb <executable>" to use <<executable>> is the name of the compiled file you wish to debug you must compile with the -g flag to use gdb run your program by typing "run" You can display a chunk of code by typing list (or ‘l’) list main displays 10 lines of code centered on the function main list displays the next 10 lines of code

Moving through your Code Stepping through the code Executes the next line without stepping into functions next steps to the next line or into a function if a function call step executes to the next function call or return finish continues execution to the next breakpoint continue

Breakpoints Breakpoints allow you to stop the code during execution and inspect the data You can set breakpoints in various ways: To set a breakpoint at a specific line number within a specific file break <filename>:<linenumber> To set a breakpoint at the beginning of a function declaration break <functionname> List all breakpoints info break Delete breakpoint by number del #

Using gdb to inspect to inspect the value of local variables and parameters info local info args To examine a variable value, print <variablename> Must exist within current scope and be after it was initialized For an array, dereference and use the @num to show that number of elements p *ptr@5 To set the value of a variable set <variablename> = <valuetoset>

More Commands To inspect the stack Command help backtrace Shows the stack trace and the parameter values to each function call #0 reverseInts (n=0x7fffffffe8e0, size=4) at pointers.c:15 #1 0x000055555555480b in main () at pointers.c:25 Command help help - lists command categories help <category> - lists commands example: help stack

Classwork: GDB

Valgrind Valgrind is a set of tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. Call using valgrind command and run command valgrind ./<executable>

Valgrind Output Valgrind offers many different tools and many different options. We will use it for memory checking For full leak checking use the flag --leak-check=full valgrind ./myprog ... HEAP SUMMARY: in use at exit: 4 bytes in 1 blocks total heap usage: 1 allocs, 0 frees, 4 bytes allocated LEAK SUMMARY: definitely lost: 4 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks ... ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

Valgrind Errors we care about Illegal read / Illegal write errors When your program reads to or write from uninitialized or invalid memory Use of uninitialised values When you use values that were never defined or initialized Illegal frees Memory that was never allocated is being freed

Classwork: Valgrind