Common C Programming Errors, GDB Debugging

Slides:



Advertisements
Similar presentations
Debugging ACL Scripts.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Debugging Introduction to Computing Science and Programming I.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Homework Reading Programming Assignments
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
DEBUGGING CHAPTER Topics  Getting Started with Debugging  Types of Bugs –Compile-Time Bugs –Bugs Attaching Scripts –Runtime Errors  Stepping.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
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.
Creating your first C++ program
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Computer Programming I Hour 2 - Writing Your First C Program.
Errors “Computer says no..”. Types of Errors Many different types of errors new ones are being invented every day by industrious programming students..
1 SEEM3460 Tutorial Compiling and Debugging C programs.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
C++ crash course Class 9 flight times program, using gdb.
CMSC 1041 Introduction to C Creating your first C program.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
DEBUG.
Last week: We talked about: History of C Compiler for C programming
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
Introduction to Computing Science and Programming I
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
CSC201: Computer Programming
Completing the Problem-Solving Process
CS1101X Programming Methodology
Testing and Debugging.
Introduction to C# Applications
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
EGR 2261 Unit 4 Control Structures I: Selection
Introduction to C Topics Compilation Using the gcc Compiler
gdb gdb is the GNU debugger on our CS machines.
COMP 2710 Software Construction Introduction to GDB
Arrays in C.
Intro to PHP & Variables
CS 240 – Lecture 11 Pseudocode.
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Cracking the Coding Interview
Formatted Input/Output
Files I/O, Streams, I/O Redirection, Reading with fscanf
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Introduction to C++ Programming
Exceptions and files Taken from notes by Dr. Neil Moore
Govt. Polytechnic,Dhangar
Creating your first C program
Using a Debugger 1-Jan-19.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Software Development Process
Formatted Input/Output
Homework Reading Programming Assignments Finish K&R Chapter 1
An Introduction to Debugging
Formatted Input/Output
An Overview of C.
Chapter 1 c++ structure C++ Input / Output
Chapter 15 Debugging.
WJEC GCSE Computer Science
Debugging.
CHAPTER 6 Testing and Debugging.
Chapter 15 Debugging.
Presentation transcript:

Common C Programming Errors, GDB Debugging CS 240 – Lecture 10 Common C Programming Errors, GDB Debugging

Troubleshooting Broken Code – In C There are a number of differences between the Coding  Running cycle of C and languages like Python. The a C program goes through the following stages before it is a proper executable. Source Code Preprocessor/Precompiler Compilation Linking An error in your code can belong to any one of these stages in the process.

Preprocessor Errors – Syntax Errors Often times, these will be self explanatory. hello.c:1:2: error: invalid preprocessing directive #incdude #incdude <stdio.h> Above we have an example of error output from the compiler when we try to compile hello world. As you can see #include is spelled incorrectly. The compiler does its best to tell us where the error is and what is wrong, but it's not perfect. "On line 1, starting at character 2, I found something invalid." Compilers aren't smart enough to say "You spelled include wrong."

Preprocessor Errors – Syntax Errors hello.c:1:20: warning: extra tokens at end of #include directive #include <stdio.h> <string.h> Above, we have extra things in the #include directive that aren't understood by the specification. While this may make sense to us, the compiler only understands the C language specification. The language "extra tokens", if found anywhere, means the line is wrong because you have written extra content where it doesn't belong. Likewise, "expects" means something is missing when it shouldn't be. hello.c:1:10: error: #include expects "FILENAME" or <FILENAME> #include

Preprocessor Errors – Invisible Syntax Errors There are some syntax mistakes in your code which do not raise any flags for the compiler. #define MAXLINE 100; Above, we have the symbolic constant MAXLINE which is a replacement for 100; It's not until you try to use it in code where it would cause a crash that the compiler complains. int a = MAXLINE; Compiler is actually fine with this. printf("%d", MAXLINE); Compiler complains because of ; prog.c:2:20: error: expected ‘)’ before ‘;’ token

Compilation Errors – Syntax Errors More often than not, the Syntax Error you'll get is going to be of the form: expected X before 'Y' token X and Y are placeholders for other tokens like Parenthesis, Braces, Semicolons, or even entire expressions or variable names. This type of error is often associated with a missing semicolon, which results in the C compiler reading parts of the next line of code. int a = 1 a = 2; prog.c:6:5: error: expected ',' or ';' before 'a' It simply means "for this code to be compile-able, X must come before Y."

Compilation Errors – Semantic Errors A semantic error is a type of error in which, for the most part, you've written correct pieces of C code, but the combination of them makes no sense. a + b is a valid C expression c + d is a valid C expression a + b = c + d is not a valid C statement For these types of errors, you'll get a number of cryptic responses from the compiler. prog.c:6:11: error: lvalue required as left operand of assignment a + b = c + d; This one states that the left side of an assignment statement must be a location to store the value on the right.

Runtime Errors Runtime errors are issues with the program which are not apparent to the compiler and won't be noticed until the program runs. Runtime errors usually result in the entire program crashing because it is impossible to continue after reaching such an error. "I was asked to divide by 0 and use the result for the rest of the program." int a = 0, b = 1/a; Floating point exception (core dumped) These errors will not tell you where in the code they were encountered. They may not even happen all the time.

Runtime Errors The runtime error you're more likely to deal with is the Segmentation Fault. Segmentation fault (core dumped) Segmentation faults indicate memory access to addresses that do not belong to you. Very likely, you've accessed the NULL address without checking for NULL The solution to these is to make sure that any array code you've written receives an actual buffer and stays entirely within the length of that buffer. Also, make sure that any file operations succeed by checking for the NULL address.

Logic Errors Logic errors are the most prevalent of all errors. Like runtime errors, they are not apparent to the compiler at compile time. int average = a + b / 2; only divides b Logic errors are a mistake in the program which leads it to produce incorrect results or behave otherwise incorrectly, but do not in-and-of themselves cause a crash. The only solution to logic errors is to fix the mistake in your logical thinking and make sure that your code matches exactly what you want the program to do. Finding them yourself requires patience and careful reading of your code.

Debugging – My Code Won't Work and Now I Know Why Debugging is the process of efficiently detecting errors, figuring out their effects on the program, and replacing those errors with working code. On the right, a famous pair of quotes spreading across the internet programming communities. There are tools which allow you to go step-by- step through the execution of a program called debuggers. If you actually put in the effort to use one, you'll be able to say "I know why my code does/doesn't work."

Debugger – gdb utility The gdb utility is a debugger for C programs available on the Unix system. However, to use it with your program, it must specifically be compiled with debugging information included. gcc –g –o program source.c The –g flag indicates that the program should include in it's binary executable the names of functions, variables, and lines of code that constitute it. Cheatsheet for gdb to have out while you're using it. https://www.cs.umb.edu/~kamaral/cs240/gdb-refcard.pdf

Debugger – Attaching gdb to a Program To use the debugger to go through the program, it needs to be attached to the program. gdb prog This will attach gdb to the program and it will be waiting for you to issue gdb commands. To get the program to run normally at this point, you can type the following into the prompt. (gdb) run "(gdb)" prompt should be there You can also issue I/O redirections from here. (gdb) run <input.file >output.file

Debugger – Setting Breakpoints in gdb Breakpoints are places in the code where the gdb debugger is told to stop execution and wait for commands. Without breakpoints, the gdb debugger would run without stopping until it hit all the errors you were trying to find. To set a breakpoint at the beginning of a function, you can use break with the function's name: (gdb) break main With this, the debugger will run the program up to the beginning of the main function and wait for you to give additional commands. You can also give breakpoints by line number and delete breakpoints by index (breakpoints are numbered starting from 1). (gdb) break 4 (gdb) delete 1

Debugger – Stepping with gdb Once you've reached a breakpoint, your screen should look something like this: Breakpoint 1, main () at prog.c:4 4 int a = 0; To execute this line of code and move to the next line, use the step command. (gdb) step (gdb) step 2 5 a = a + 1; 6 a = a / (a - 1); You can also step a number of lines by using step n Also, at any point you can add more breakpoints with the break

Debugger – Listing lines of code around you The list command will display the lines surrounding the current position in the execution. (gdb) list 1 #include <stdio.h> 2 3 void main() { 4 int a, b, c, d = 10; 5 a = 1; 6 b = 4; 7 c = 7; 8 printf("%d", a); 9 } 10 This will always show 10 lines by default.

Debugger – Checking the variables in gdb The print command allows us to check the value of variables at the current point in execution. 6 a = a / (a - 1); (gdb) print a $1 = 1 The print command may default to numeric printing. To get character or hex output, use the following (respectively): (gdb) print /c a (gdb) print /x a $1 = '\001' $1 = 0x1

Debugger – Displaying local variables in gdb To display all local variables, use the info locals command. (gdb) info locals a = 32767 b = 0 c = 0 d = 10 For all global and local variables, use the info variables command. For larger projects, this will list too many variables you probably don't care about.

Debugger – Modifying Variables in gdb Sometimes, you'll want to make minor changes once you've found an issue to test for another problem. Without rerunning your code, you can change variables with the set variable command. (gdb) set variable b = 10 (gdb) info locals a = 32767 b = 10 c = 0 d = 10

Debugger – Continuing and Leaving After you've solved a problem at a specific location, it's often worth letting the gdb debugger continue until it hits another breakpoint. As opposed to hitting the step command until you're dizzy. The continue command does just that and makes the program continue running until it hits another breakpoint. (gdb) continue To leave the debugger, use the quit command. (gdb) quit