How to use GDB? (Example)

This section will demonstrate how to use GDB commands by going through example.

Here is program which is buggy in its behavior. 
This program is writtten to compute no. of years taken to reach target balance provided current balance and  yearly interest rate.
But it gives 1 year as result for any value of balance or target.
/* File: buggy.c */
#include <stdio.h>
int main()
{ 
	int balance=100;
	int target=1000;
	float rate = 0.1;
	int year = 0;
	do
	{
		float interest = balance * rate;
		balance = balance + interest;
		year++;
	} while ( balance >= target );
	printf("%d No. of years to achieve target balance.\n", year);
	return 0;
}
 
Here are steps to debug this program.
Step 1: Compile and Build program with debugging symbols $ gcc -g buggy.c
 
Step 2: Run program with GDB$ gdb a.out
 
Step 3: Set a breakpoint on main function.(gdb) b main
Breakpoint 1 at 0x400535: file buggy.c, line 5.
 
Step 4: Run program(gdb) run
Starting program: a.out
 
Breakpoint 1, main () at buggy.c:5
5 int balance=100;
As breakpoing was set on main function, program will stop at main function and wait for gdb command.
 
Step 5: Step until we reach till line 13(gdb) s
6 int target=1000; 
(gdb) s
7 float rate = 0.1; 
(gdb) s
8 int year = 0; 
(gdb) s
11 float interest = balance * rate; 
(gdb) s
12 balance = balance + interest; 
(gdb) s
13 year++; 
 
Step 6: Print value of balance, rate, interest(gdb) p balance
$1 = 110
(gdb) p rate
$2 = 0.100000001
(gdb) p interest
$3 = 10
 
Step 7: Step until we reach to line 15(gdb) s
14 } while ( balance >= target );  
(gdb) s
15 printf("%d No. of years to achieve target balance.\n", year);
 
Step 8: Print value of target, year, balance(gdb) p target
$5 = 1000
(gdb) p year
$6 = 1
(gdb) p balance
$7 = 110
Value of balance is 110 and target is 1000. That seems ok.
Codition in while loop i.e. balance >= target, will be flase. And loop will break.
So condition is wrong here. It should be balance < target.
Congratulation! You have debugged your first buggy program.

Comments

Doctors have many challenges to face as they are perennially surrounded by patients, diseases, hospital duties and over-extended or odd shift timings. Universally, doctor is considered to be a noble profession and respectable one at that, but a doctor also has to work under immense pressures, emotional strains and other physical challenges. A regular physician like most of us at some point face will have to deal with personal situations such as important family affairs, family holidays, sickness or pregnancy that may force them to abandon medical duties. At the same time, a hospital or a healthcare facility is also constantly faced with emergency situations that demand all hands on deck round-the-clock. Therefore, every hospital, clinic or nursing home is compelled to hire locum tenens or substitute doctor in order to keep the staffing under control at all times. In fact, locum doctors are the most valuable asset for the medical community because they provide quality medical care and act as a helping-hand in emergency situations when the medical facilities need them the most. Unlike regular or permanent doctors, locum doctor jobs are also ideal career options for medical interns and graduates because they offer a wide array of medical exposure in varied medical specialties, work cultures and healthcare systems. Locum jobs are challenging and flexible, thus an increasing number of medical professionals have benefitted from these jobs, so whether one is looking for a family physicians position or in a hospital or in a clinic, locum jobs for doctors are available at all levels and in different healthcare systems. In addition, being a locum doctor gives a medical professional the control over their working hours, location of work and choice of area of specialisation. Technically, locum positions are not restricted to general physicians but they are also extended to other fields of medical specialisations such as cardiology, neurology and many more. Travelling can be an integral part of locum jobs, and these distinctive features are a boon for many dedicated medical professionals who are eager to expand their medical careers with loads of multi-cultural medical experiences. The fact that locum agencies in the UK recruit tens of thousands of locums from across the globe in various NHS hospitals, private clinics, nursing homes and other public hospitals speaks volume of the popularity of locum jobs. Locating or getting a locum tenens job is a simple task as long as you are registered with one of the many reputable locum agencies. These agencies act as the middle man between locum tenens and medical facilities, and they also look after all the details pertaining to travel for locum tenens, accommodation and the nature of locum work. Thus, maintaining a healthy locum doctor-agency relationship benefits both the parties, and it also increases the probability of getting recommendable employment opportunities and businesses or vice-versa.

generic viagra soft tabs reviews london <a href="http://viagrafanlux.com">generic viagra available</a> virketid viagra online <a href=http://viagrafanlux.com#viagra+genГ©rico>cost of viagra</a>