Lesson 6 Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Module 6: Debugging

Lab 1: Debugging in C

Overview:
This lab enables you to understand the concept and working of a debugger.
After performing the lab you will be able to:
1. Discuss basics of debugging
2. List debugging commands
3. Perform debugging on a core dump

Business Scenario:
The user is new to debugging functionalities, and needs to understand and perform
debugging on a core dump. Hence, they will learn the different commands, and will
demonstrate the tasks with the help of the detailed instructions provided below.

Detailed Instructions:
GDB Commands
1. To start with, you can go through these list of commands to get comfortable with the
gdb debugger.
 print or p: prints value of expression
 break or b: Sets a breakpoint by specifying line number, function_name,
file_name:line_number, file_name:function_name, or address of a function
 info breakpoints or i b: Lists breakpoints that are currently set
 info locals or i l: Lists all variables in the current function stack
 run or r: Runs the program being debugged
 next [N] or n [N]: Steps over current statement and runs all the statement till it
reaches N
 step [N] or s [N]: Steps into the statement after N statements, if it is a function
 continue [N] or c: Continue normal execution
 set args: Set input arguments for the program being debugged
 delete [N]: Deletes a breakpoint identified by break point number N
 delete breakpoints: Deletes all breakpoints
 enable [N]: Enables a breakpoint identified by number N
 disable [N]: Disables a breakpoint identified by number N
 info registers or i r: Lists integer registers and their contents for selected stack
frame
 info registers-all: Lists all registers including platform specific registers and their
contents for selected stack frame
Module 6: Debugging
 backtrace or bt: Shows backtrace of all stack frames, the frames are listed by
frame numbers
 backtrace full: Shows backtrace of all stack frames including local variables
 frame [N]: Shows frame details identified by frame number
 info args or i ar: Prints the arguments supplied to the function of the current
stack frame
 list: Shows ten source lines by default
 set variable <variable_name> <value>: Used to manually set/override value of a
variable in the current scope
 call <function_name>: Calls a function in the program
 disassemble or disas: Disassembles a specified section of memory, defaults to the
function surrounding the current instruction in the selected stack frame

Basics of Debugging
1. Below is a simple program that you can try out.
math_utils.h
int add(int, int);
int multi(int, int);

math_utils.c

int add(int x, int y){


return (x + y);
}

int multi(int x, int y){


return (x * y);
}

math_user.c

#include<stdio.h>
#include <math_utils.h>

float totalAmount(int itemCount, float itemPrice){


float result = itemCount * itemPrice;
return result;
}

int main(void){
int items = 20;
float price = 20.34;
float totalPrice = totalAmount(items, price);
Module 6: Debugging
{
int j = 20;
int m = 10;
}
printf("Total Price: %.02f\n", totalPrice);
printf("ADD: %d %d\n", add(10,40), multi(20,5));
return 0;
}

2. To compile and run the program, copy the source code into a .c file.
3. Once the program is compiled successfully, execute the program.
Module 6: Debugging
Debugging a core dump
1. In this lab, you will simulate a core dump on your system.
2. First, after compiling the program, execute it without setting environment variable
MEM_ALLOC to generate a core dump file.
3. If the file is not generated, set the ulimit to unlimited, by using the command:
ulimit –c unlimited
4. This command will set the core file size to unlimited. Now, execute the binary again
to generate the core file.
5. Below is the program to debug the core dump:
hash.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void hash(unsigned char *str, char** out)


{
unsigned long hash = 5381;
int c;

if(getenv("MEM_ALLOC"))
*out = malloc(sizeof(long) + 1);
memset(*out,0,sizeof(long) + 1);

while (c = *str++)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}

sprintf(*out, "%lu", hash);


}

hash.h

void hash(unsigned char *, char**);

mkhash.c

#include <stdio.h>
#include <stdlib.h>
#include <hash.h>

int main(void){
char *out = NULL;
hash("Hello World", (char**)&out);
Module 6: Debugging
printf("HASH: %s\n", out);
free(out);
return 0;
}

6. To compile and run the program, copy the source code into a .c file and compile it.
7. Once the program is compiled successfully, execute the program.

You might also like