CS201 Lecture 2 GDB, The C Library - Raoul Rivas · CS201 –Lecture 2 GDB, The C Library....
Transcript of CS201 Lecture 2 GDB, The C Library - Raoul Rivas · CS201 –Lecture 2 GDB, The C Library....
RAOUL RIVAS
PORTLAND STATE UNIVERSITY
CS201 – Lecture 2GDB, The C Library
Announcements
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
2
Multidimensional Dynamically Allocated Arrays▪ Direct access support.▪ Same as Multidimensional Static Arrays
▪ No direct allocation support▪ Declare a single dimension array of pointers (Rows)
▪ For each row allocate an array (Columns)
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
3
0x100
0x123
0x230
0x510
array
Char** Char*
What’s the value of array[1][2]?
X Y Z
A B C
D E F
G H I
Multidimensional Dynamically Allocated Arrays
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
4
0x100
0x123
0x230
0x510
array X Y Z
A B C
D E F
G H I
Char** Char*
char** array;int i;
array = (char**)malloc(sizeof(char*)*4);
for(i=0; i < 4; i++){array[i] = (char*)malloc(sizeof(char)*3);
}
array[2][0]=‘D’;
for(i=0; i < 4; i++) {free (array[i]);
}
free(array);
Alternate Method▪ Store the data in a single dimension array▪ Flatten array into a single dimension
▪ Compute offsets during access
▪ Can’t use multiple subscripts
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
5
array = (char*)malloc(sizeof(char*)*4*3);idx=2*3+1;array[idx]=‘E’; free(array);
X Y ZA B CD E FG H I
X Y Z A B C D E F G H I
array
0 11
The C Library▪ Standard library available across all C compilers▪ Part of ANSI C
▪ Many implementations▪ Microsoft C Runtime Library
▪ GNU C library
▪ Provides many services▪ File Access
▪ Read and write to console
▪ String Manipulation
▪ Mathematical functions
▪ Time and Date functions
▪ Divided into modules▪ Include header file with definitions before you can use them
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
6
#include <header.h>
Back to Hello, world
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
7
#include <stdio.h>
main()
{
printf(“hello, world”);
}
Include stdio.h header file (so we can use printf)
String Manipulation▪ Assumes all strings are C-Strings▪ char arrays with NULL character at the end (‘\0’)
▪ strlen(char *s1)▪ Returns the number of character in the string not including the
NULL character
▪ strncpy(char *dest, char *src, int n)▪ Copies at most n characters of src on top of dest
▪ strncmp(char *s1, char *s2, int n)▪ Compares up to n characters of s1 with s2
▪ Returns 0 if s1== s2. Returns less than 0 if s1 < s2. Returns more than 0 if s1 > s2
▪ strncat(char* s1, char *s2, int n)▪ Appends up to n characters of s2 at the end of s1
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
8
String Manipulation
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
9
#include <stdio.h>#include <string.h>
int main (){char str1[]= "To be or not to be";char str2[40];char str3[6];
/* copy to sized buffer */strncpy ( str2, str1, sizeof(str2) );
/* partial copy (only 5 chars): */strncpy ( str3, str2, 5 );/* null character manually added */str3[5] = '\0';
return 0;}
Is the manually added NULL
necessary? Why?
File I/O▪ FILE *fopen(const char *filename, const char *mode);▪ Opens/Creates a file for read or write. Returns a pointer to a FILE structure
▪ Mode: “r” - Read, “w”- Write, “a” - Append, “b” - Binary mode
▪ int fclose(FILE *a_file);▪ Close the file
▪ ASCII Read/Write▪ fprintf(), fscanf(), fgets()
▪ Usually read one line at a time
▪ Good for Text, CSV files and files with line breaks
▪ Binary Read/Write▪ Write of data in binary format
▪ Read/Write M-blocks of N-bytes at a time
▪ Use fwrite() and fread()
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
10
Console I/O▪ Console I/O is like reading from a file. You can use the same file functions just specify “stdin” and ‘stdout” as the file pointers
▪ There are some specific functions for stdin and stdout are provided in the C library▪ Most used function for console writing is printf
▪ Sometimes the File and Console versions have different behaviors or parameters
▪ fgets() does not behave the same a gets()
▪ Data read from the file/console must be validated▪ fscanf() and scanf() are subject to many attacks
▪ fgets() is the safest way to read from the file/console
▪ Parse the string afterwards and convert to the specific types
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
11
stdin
stdout
String to Numbers▪ Various functions based on the type▪ strtol, strtoll, stroul, etc
▪ long int strtol (const char* str, char** endptr, int base);▪ Parses a number inside a string and outputs it as a long
▪ str contains the string to parse
▪ endptr is an optional return value to the remaining of the string after the number
▪ base is the Base of the number (hex, octal, decimal, etc). Use zero for autodetect based on the format.
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
12
int main (){char str1[]=“16734893”;long number;
number=strtol(str1, NULL, 10);return 0;
}
Numbers to Strings▪ Use sprint to create a string with the format you need▪ sprintf(char* str, char *format, …)▪ str contains the destination string. Make sure is pre-allocated to the maximum
possible length.
▪ Limits.h contains macros with the largest and smallest number supported for each type.
▪ log10(𝑀𝑎𝑥𝑁𝑢𝑚) + 1, not counting the sign
▪ Format is the format string used by the printf family of functions
▪ printf, fprintf, sprint
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
13
int main (){char str1[12];int number= 16734893;
sprintf(str1, “%d”, number);return 0;
}
Assertions▪ Assertions are runtime checks placed on a program that are only checked during development
▪ They are ignored for “production” code.▪ We define the macro NDEBUG to indicate that our code is “production”
code
▪ An Assertion must be true at the time of its execution or the program will stop.
▪ To check an assertion we call the assert() function
▪ Do not use assertions to catch user or runtime errors
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
14
#define NDEBUG
assert(condition);
Assertions
▪ Not a user or runtime error
▪ Assert Fails and memcpy never executes
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
15
int main(){
int str1[] = {1,2,3,4,5}int str2[3];
assert(sizeof(str1) == sizeof(str2));memcpy(str2, str1, sizeof(str1));
}
GDB Overview
▪ Inspect program execution and state at any point in time.▪ Set breakpoints▪ Read variable, dump stack
▪ Useful to find a wide range of bugs
▪ Must compile the program to include “Symbols”▪ Use the –g or –ggdb flag▪ If possible use –O0 to disable optimizations
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
16
GDB Interactive Shell▪ GDB is based on an interactive shell▪ The shell prompt symbol is “(gdb)”
▪ History of past command
▪ TAB for autocompletion
▪ Integrated help▪ Call the help command followed by the name of the command you
need info about
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
17
(gdb) help mycommand
Using GDB▪ You can specify the program to load in the command line
▪ Or you can load the program using the “file” command in the gdb prompt “(gdb)”
▪ To run your program use the “run” command
▪ If your code crashes you will see additional crash information
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
18
gdb myprogram
(gdb) file myprogram
(gdb) run
How to debug a program▪ Debugging is a lot like fishing
▪ Stop execution at possible suspecting points or functions
▪ Look for the values of each variable until you can determine the problem
▪ Step through specific functions or loops to find any problems in your code
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
19
Breakpoints▪ Breakpoints stop execution of a program at a specific point▪ Use the “break” command
▪ Stop at a specific line in a source file
▪ Stop at line 67 of file5.c
▪ Stop at a specific function
▪ Stop at function my_func()
▪ After setting a breakpoint start your program using the “run” command
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
20
(gdb) break file5.c:67
(gdb) break my_func
Navigating the Code▪ GDB stops the execution of your program at a breakpoint▪ GDB command prompt will appear
▪ To step through the code use the “step” command
▪ Executes one instruction and stops. If the line is a function it will step into your function
▪ Visual Studio calls this “Step Into”
▪ To execute an entire function as if it was a single instruction use the command “next”
▪ Visual Studio calls this “Step Over”
▪ For assembly use stepi and nexti!
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
21
(gdb) step
(gdb) next
Next vs. Step
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
22
int main(){
int val=5, result;
res=factorial(val);printf(“%d\n”, result);return 0;
}
int factorial(int n){
int i, retv=1;
for(i=1; i<= n; i++) {
retv*= 1; }
return retv;}
Step
Next
Printing a variable▪ To print a variable use the command “print”
▪ GDB can print variables members of structures
▪ Dereference pointer
▪ Support self-referential structures
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
23
(gdb) print myvar
(gdb) print mystruct.myvar
(gdb) print *myptr
(gdb) print mylist->next->next
Watchpoint▪ A watchpoint is similar to a breakpoint, except that it stops the execution when the value of a variable change▪ No associated position in the code
▪ Variable is the stopping trigger
▪ Use the “watch” command to set a watchpoint on a variable
▪ Pause execution when the variable myvar changes
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
24
(gdb) watch myvar
int main(){
int val=5, result;result= val + 1;val= 25;
}
When GDB stops if we set a Watchpoint on variable val? Val=25
Conditional Breakpoints▪ Stop in a breakpoint only if certain condition is met▪ Good to debug array or loop boundaries
▪ Cases where code fails with certain values
▪ Use the following syntax:
▪ Example
▪ Stop at line 6 of file1 if i > ARRAYSIZE
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
25
(gdb) break breakpoint if condition
(gdb) break file1:6 if i > ARRAYSIZE
Debugging in Windows▪ GDB is the most popular debugger in Linux
▪ In Windows you can use WinDBG or the more user friendly Visual Studio Debugger▪ Breakpoints, Watchpoints, Conditional Breakpoints
▪ Windows keeps Symbols in separate Program Database Files (PDB)
▪ PDB files must be loaded into WinDbg to produce any meaningful information
▪ A PDB file is specific to a particular build!
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
26
Summary▪ Dynamically allocated multidimensional arrays are usually declared as an array of pointers to one dimension arrays
▪ GDB is the GNU Debugger.▪ Inspect variables, breakpoints, watchpoints, step by step execution
▪ The C library is a standard ANSI library▪ String Manipulations, File Access, Console I/O, Memory Allocation
CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)
27