David Notkin Autumn 2009 CSE303 Lecture 16 #preprocessor Debugging is twice as hard as writing the code in the first place. Therefore, if you write the.

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Utilizing the GDB debugger to analyze programs Background and application.
CSE 303 Lecture 13a Debugging C programs
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
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.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
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.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
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 PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
CPT: Debug/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to talk about the various approaches to testing.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
C++ Functions A bit of review (things we’ve covered so far)
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.
DEBUG.
13 C Preprocessor.
Instructions for test_function
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CSE 374 Programming Concepts & Tools
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Testing and Debugging.
Debugging with gdb gdb is the GNU debugger on our CS machines.
14. THE PREPROCESSOR.
gdb gdb is the GNU debugger on our CS machines.
Pre-processor Directives
ניפוי שגיאות - Debugging
Common C Programming Errors, GDB Debugging
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
GNU DEBUGGER TOOL. What is the GDB ? GNU Debugger It Works for several languages – including C/C++ [Assembly, Fortran,Go,Objective-C,Pascal]
When your program crashes
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Debugging.
Makefiles, GDB, Valgrind
Presentation transcript:

David Notkin Autumn 2009 CSE303 Lecture 16 #preprocessor Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian W. Kernighan

C preprocessor Part of the C compilation process; recognizes special # statements, modifies your source code before it is compiled This is generally considered to be a “macro processing” tool functiondescription #include insert a library file's contents into this file #include "filename" insert a user file's contents into this file #define name [value] create a preprocessor symbol ("variable") #if test if statement #else else statement #elif test else if statement #endif terminates an if or if/else statement #ifdef name if statement; true if name is defined #ifndef name if statement; true if name is not defined #undef name deletes the given symbol name

Constant replacement The preprocessor can be used to create constants: #define NUM_STUDENTS 100 #define DAYS_PER_WEEK 7... double grades[NUM_STUDENTS]; int six_weeks = DAYS_PER_WEEK * 6; // 42 printf("Course over in %d days", six_weeks); When the preprocessor runs before compilation, 7 is literally inserted into the code wherever DAYS_PER_WEEK is seen: DAYS_PER_WEEK does not exist in the eventual program int six_weeks = 7 * 6; // 42

Optional debugging code #define DEBUG... #ifdef DEBUG // debug-only code printf("Size of stack = %d\n", stack_size); printf("Top of stack = %p\n", stack); #endif stack = stack->next; // normal code How is this different from declaring a bool/int named DEBUG ?

Advanced definitions #define can be used to modify the C language (No different in mechanism than constants) #define AND && #define EQUALS == #define DEREF ->... Point p1 = (Point*) malloc(sizeof(Point)); p1 DEREF x = 10; p1 DEREF y = 10; if (p1 DEREF x EQUALS p1 DEREF y AND p1 DEREF y > 0) { p1 DEREF x++; } Good idea? Bad idea? Neutral idea?

Preprocessor macros Similar to a function, but created inline before compilation #define SQUARED(x) x * x #define ODD(x) x % 2 != 0 int a = 3; int b = SQUARED(a); if (ODD(b)) { printf("%d is an odd number.\n", b); } The above literally converts the code to the following and compiles: int b = a * a; if (b % 2 != 0) {...

Subtleties the preprocessor just replaces tokens with tokens #define foo 42 int food = foo; // int food = 42; ok int foo = foo + foo; // int 42 = ; bad preprocessor macros can do a few things functions cannot: #define NEW(t) (t*) calloc(1, sizeof(t))... Node* list = NEW(Node);

Caution with macros since macros are expanded directly, strange things can happen if you pass them complex values #define ODD(x) x % 2 != 0... if (ODD(1 + 1)) { printf("It is odd.\n"); // prints! } The above converts the code to the following if (1 + 1 % 2 != 0) { Fix: Always surround macro parameters in parentheses. #define ODD(x) (x) % 2 != 0

Running the preprocessor to define a preprocessor variable, use the -D argument $ gcc -D DEBUG -o example example.c to run only the preprocessor, use the -E argument to gcc $ gcc -E example.c int main(void) { if ((1 + 1) % 2 != 0) { printf("It is odd.\n"); } return 0; } rarely used in practice, but can be useful for debugging / learning

gdb gdb : GNU debugger. Helps you step through C programs. –absolutely essential for fixing crashes and bad pointer code –your program must have been compiled with the -g flag usage $ gdb program GNU gdb Fedora ( fc9) Copyright (C) 2008 Free Software Foundation, Inc... (gdb) run parameters... redirecting input: $ gdb program (gdb) run parameters < inputfile

gdb commands commanddescription run or r parameters run the program break or b place sets a breakpoint at the given place: - a function's name - a line number - a source file : line number print or p expression prints the given value / variable step or s advances by one line of code ("step into") next or n advances by one line of code ("step over") finish runs until end of function ("step out") continue or c resumes running program backtrace or bt display current function call stack quit or q exits gdb

A gdb session $ gdb intstack GNU gdb Copyright 2002 Free Software Foundation, Inc. (gdb) b 34 Breakpoint 1 at 0x4010ea: file intstack.c, line 34. (gdb) r Starting program: /home/user/intstack Breakpoint 1, main () at intstack.c:34 34 Node* oldFront = stack; (gdb) p stack $1 = (Node *) 0x4619c0 (gdb) n 35 printf("%d\n", stack->data); (gdb) n 36 stack = stack->next; (gdb) n 37 free(oldFront); (gdb) p stack $4 = (Node *) 0x (gdb) p oldFront $2 = (Node *) 0x4619c0 (gdb) p *oldFront $3 = {data = 10, next = 0x462856} (gdb) c Continuing.

ddd ddd (Data Display Debugger): Graphical front-end for gdb –allows you to view the values of your variables, pointers, etc. –$ ddd programName

nemiver nemiver : Another graphical debugger front-end –design goal: Be usable even if you don't know gdb commands – $ nemiver programName arguments

Other debuggers Eclipse CDT (C/C++ Development Toolkit) –create a new Managed Make C Project –right-click project name, choose Debug As, Local C/C++ Application

valgrind valgrind : A memory-leak detector and debugging tool valgrind programName arguments (1) push, (2) pop, (3) clear, or (0) quit? 2 ==3888== Conditional jump or move depends on uninitialised value(s) ==3888== at 0x80484E7: main (intstack.c:28) ==3888== ==3888== Use of uninitialised value of size 4 ==3888== at 0x80484F2: main (intstack.c:30) ==3888== ==3888== Use of uninitialised value of size 4 ==3888== at 0x : main (intstack.c:31) ==3888== ==3888== Invalid free() / delete / delete[] ==3888== at 0x4025DFA: free (vg_replace_malloc.c:323) ==3888== by 0x : main (intstack.c:32) ==3888== Address 0x is in the Text segment of /home/stepp/intstack

Valgrind: leaks, too stats about leaked memory on program exit (1) push, (2) pop, (3) clear, or (0) quit? 1 Number to push? 10 (1) push, (2) pop, (3) clear, or (0) quit? 1 Number to push? 20 (1) push, (2) pop, (3) clear, or (0) quit? 2 20 (1) push, (2) pop, (3) clear, or (0) quit? 2 10 (1) push, (2) pop, (3) clear, or (0) quit? 0 ==5162== LEAK SUMMARY: ==5162== definitely lost: 16 bytes in 2 blocks. ==5162== possibly lost: 0 bytes in 0 blocks. ==5162== still reachable: 0 bytes in 0 blocks. ==5162== suppressed: 0 bytes in 0 blocks.

lint / splint: checks for possible errors famously picky (sometimes should be ignored) but good for helping you find potential sources of bugs/errors not installed on attu, but can install it on your Linux: $ sudo apt-get install splint $ splint *.c Splint May 2008 part2.c: (in function main) part2.c:8:2: Path with no return in function declared to return int There is a path through a function declared to return a value on which there is no return statement. This means the execution may fall through without returning a meaningful result to the caller. (Use -noret to inhibit warning) use_linkedlist.c:5:5: Function main defined more than once A function or variable is redefined. One of the declarations should use extern. (Use -redef to inhibit warning) part2.c:8:1: Previous definition of main

Questions? CSE303 Au0919