In this area we will introduce gdb by assuming a script file almanac of its use on an absolute program.In adjustment to analyze amid band numbers in the scriptfile from band numbers aural the C antecedent files, I accept placed a `g’ at the alpha of anniversary of the former. For example, Band g56 agency Band 56 aural the scriptfile, not Band 56 aural one of the C antecedent files.)
First, use cat to appearance the affairs antecedent files:
The comments in Curve g5-g10 accompaniment what the affairs ambition is, i.e. award prime numbers.
OK, let’s get started. Aboriginal we abridge the program:
The admonishing apropos Band 31 of CheckPrime.c sounds apocalyptic (and it is), but let’s avoid it, and see what happens. Let’s run the program:
Well, this sounds scary, but actually it usually is the easiest blazon of bug to fix. The aboriginal footfall is to determine where the absurdity occurred; gdb will do this for us: We enter gdb and afresh re-run the program, so as to carbon the error:
OK, gdb is now accessible for my command (it indicates this by giving me a appropriate prompt, which looks like this:
I now invoke gdb’s r command, to run the affairs (if the affairs had any command-line arguments, I would accept typed them appropriate afterwards the `r’):
So, the absurdity occurred aural the action _doscan(). This is not one of my functions, so it charge accept been alleged by one of the C library functions which I am using, i.e. printf() or scanf(). Given the name _doscan, it does complete like it charge accept been the latter, but at any rate, the way to acquisition out is to use gdb’s bt(“backtrace”) command, to see area _doscan() was alleged from:
Aha! So it was absolutely alleged from scanf(), which in about-face was alleged from main(), at Band 25.
Now aback scanf() is a C library function, it apparently is able-bodied debugged already, so the absurdity was apparently not in scanf(). So, the absurdity charge accept been in our alarm to scanf() on Band 25 of Main.c.
Let’s booty a attending at that closing call. To do so, we use gdb’s l (”list”) command, to account some curve of the book Main.c at and arround the band which led to the error:
Yep, a acclaimed “C-learner’s error”-we forgot the ampersand in afore UpperBound! Band 25 of Main.c should accept been
So, in addition window (hopefully an X11 window, but if you are aloof application a text-based terminal, you can use the GNU screen program to get windows), we will fix band 25 of Main.c, and recompile. Note that we do not leave gdb while accomplishing this, since gdb takes a continued time to load. In adjustment to do this, though, we charge aboriginal tell gdbto abandon our executable file:
(Otherwise aback we approved to recompile our program, the ld linker would acquaint us that the executable book is “busy” and appropriately cannot be replaced.)
After acclimation and recompiling Main.c, the aing time we give gdb the run command
gdb will automatically amount the newly-recompiled executable for our affairs (it will apprehension that we recompiled, because it will see that our .c antecedent book is newer than the executable file). Note that we do not accept to accompaniment the command-line arguments (if any), because gdb remembers them from before. It additionally remembers our breakpoints, so we do not accept to set them again. (And gdb will automatically amend the band numbers for those breakpoints, adjusting for whatever line-number changes occurred aback we adapted the antecedent file.)
Main.c is now the following:
Now we run the affairs afresh (not in gdb, admitting we do still have gdb open in the added window):
Don’t get discouraged! Let’s see area this new seg accountability is occurring.
Now, remember, as mentioned earlier, one of the best accepted causes of a seg accountability is a wildly-erroneous arrangement index. Appropriately we should be awful apprehensive of J in this case, and should analysis what its amount is, using gdb’s p (“print”) command:
Wow! Remember, I alone had set up the arrangement Prime to accommodate 50 integers, and yet actuality we are aggravating to admission Prime!2
So, gdb has pinpointed the exact antecedent of our error-the amount of J is way too ample on this line. Now we accept to actuate why J was so big. Let’s booty a attending at the absolute function, using gdb’s l command:
Look at the comments in Curve g56-g58. We were declared to stop analytic afterwards J got to sqrt(K). Yet you can see in Curve g201-g206 that we never fabricated this check, so J aloof kept growing and growing, eventually extensive the amount 4024 which triggered the seg fault.
After acclimation this problem, the new CheckPrime.c looks like this:
OK, let’s accord it addition try:
What?! No primes appear up to the cardinal 20? That’s not right. Let’s use gdb to footfall through the program. We will abeyance at the alpha of main(), and booty a attending around. To do that, we set up a “breakpoint,” i.e. a abode where gdb will append beheading of our program, so that we can appraise the bearings afore resuming execution:
So, gdb will abeyance beheading of our affairs whenever it hits Band 24 of the book Main.c. This is Breakpoint 1; we ability (and will) set added breakpoints later, so we charge numbers to analyze them, e.g. in adjustment to specify which one we appetite to cancel.
Now let’s run the program, application the r command:
We see that, as planned, gdb did stop at the aboriginal band of main() (Line 24). Now we will assassinate the affairs one band at a time, using gdb’s n (“next”) command:
What happened was that gdb executed Band 24 of Main.c as requested-the bulletin from the alarm to printf() appears on Band g292-and now has paused again, at Band 25 of Main.c, announcement that band for us (Line g293 of the script file).
OK, let’s assassinate Band 25, by accounting `n’ again:
Since Band 25 was a scanf() call, at Band g295 of the script file, gdb waited for our input, which we typed as 20. Gdb then accomplished the scanf() call, and paused again, now at Band 27 of Main.c (Line 296 of the script file.
Now let’s analysis to accomplish abiding that UpperBound was apprehend in correctly. We think it was, but remember, the basal assumption of debugging is to analysis anyway. To do this, we will use gdb’s p (“print”) command:
OK, that’s fine. So, let’s abide to assassinate the affairs one band at a time, by using n:
Also, let’s accumulate clue of the amount of N, using gdb’s disp (“display”) command. The closing is aloof like the p, except that disp will book out the amount of the variable each time the affairs pauses, as against to p, which prints out the amount only once.
Hey, what’s activity on here? Afterwards active Band 30, the affairs afresh went aback to Band 29-skipping Band 31. Actuality is what the bend looked like:
Oops! We forgot the braces. Appropriately alone Band 30, not Curve 30 and 31, forms the anatomy of the loop. No admiration Band 31 wasn’t executed.
After acclimation that, Main.c looks like this:
OK, try again:
Still no output! Well, we will now charge to try a added abundant line-by-line beheading of the program. Last time, we did not go through the action CheckPrime() line-by-line, so we will charge to now:
Here we accept placed a breakpoint at the alarm to CheckPrime.3
Now, let’s run the program:
Gdb has chock-full at Band 30, as we requested. Now, instead of application the n command, we will use s (“step”). This closing command is the aforementioned as n, except that it willenter the action rather than absence over the action like n does:
Sure enough, s has gotten us to the aboriginal band aural CheckPrime().
Another service gdb provides for us is to acquaint us what the ethics of the ambit of the action are, in this case K = 1. But that doesn’t complete right-we shouldn’t be blockage the cardinal 1 for primeness. So gdb has baldheaded addition bug for us.
In fact, our plan was to analysis the numbers 3 through UpperBound for primeness: The for loop in main() had the afterward heading:
Well, what about the alarm to CheckPrime()? Actuality is the accomplished bend from main():
Look at Band 30-we forgot the parameter! This band should accept been
After acclimation this, try active the affairs again:
OK, the affairs now seems to be working.
To see a abounding document, go here:http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html, which is the Guide to Faster, Beneath Frustrating Debugging writen by Professor Norm Matloff.
11 Mind-Blowing Reasons Why G11 Form Free Is Using This Technique For Exposure | G11 Form Free – g702 form free
| Welcome for you to our website, on this moment I will teach you regarding g702 form free