Is it possible to dump the entire program steps to a file? I don't mean the value of the variables, I mean the jumping from on instruction to another, and if it's possible to use both, steps and the var values
I've used both gdb and GUI(ddd), and I use the regular step all the time but sometimes I have a loop and network application so I don't want to affect the flaw of the program, I just want to see what happened exactly after everything is done.
For instance,
1 #include<stdio.h>
2
3 int main()
4 {
5
6 int i = 0, y = 0;
7
8 for (y; y< 10; y++) {
9 i++;
10 printf("%d\n", i)
11 }
12 return 0;
13
14 }
So the dump file will contain all the steps from int main() to return 0, like below for example
1. on 1
2. on 2
...
...
8. on 8
9. on 9
10. on 10
11. on 8
12. on 9
... and so on until the loop finishes and then hit return and exit.
Using GDB you could use process record and replay. Another option is Jockey which functions as a record/replay preload library.
Related
I want to make timing tests for learning how to benchmark using "time.h". But I noticed the first test is always longer.
0 1 2 3 4 5 6 7 8 9
time 0.000138
0 1 2 3 4 5 6 7 8 9
time 0.000008
0 1 2 3 4 5 6 7 8 9
time 0.000007
If I want to do several tests in the same main() function the results will be unreliable.
Here is the stupid code who prints the output above.
#include <stdio.h>
#include <time.h>
const int COUNT = 10;
void test() {
clock_t start = clock();
for(int i = 0; i < COUNT; i++) {
printf("%d ", i);
}
printf("\ntime %lf\n", (double)(clock() - start) / (double)CLOCKS_PER_SEC );
}
int main() {
test();
test();
test();
return 0;
}
I solved this by ignoring the first "test" function. Also, writing a first "printf" who prints some integer before the tests works too. But I guess it's not a proper solution.
CPU has cache. When code and data are not in cache, the code takes longer to run.
It's standard practice to discard the result of first run (or first few runs) when measuring performance. It's sometimes called "cache warmup".
This program takes as an input the following lines:
23 12 33 19 10 8
5
23 19 8 12 60 18
14 60 12 44 54 10
8 3 12 19 33 10
33 15 7 60 12 10
22 12 19 23 33 11
23 12 33 19 10 8 ( The first line ) are the lottery results.
n ( in this specific case, 5 ) informs how many lines will follow below.
Each line has 6 numbers. The number order doesn't matter.
The rules are: numbers range from 1 to 60 ( including 1 and 60 ) and they never repeat themselves in the same line.
The variable "quadra" stores how many lines have got 4 numbers right.
The variable "quina" stores how many lines have got 5 numbers right.
The variable "sena" stores how many lines have got 6 numbers right.
So, a computer program is running some tests over my code below and it's claiming that it goes wrong for most of them, but I can't see what's the problem here. Does anybody have a clue? Is this code wrong, or is there something wrong with the software that's testing this code?
#include <stdio.h>
int main(){
int mega[6];
int v[50500][6];
int n,swap;
int i,j,k; //counters
int quadra,quina,sena;
quadra = 0;
quina = 0;
sena = 0;
for(i=0;i<6;++i) scanf("%i",&mega[i]); //first line, lottery results
scanf("%i",&n);
for(i=0;i<n;++i){
for(j=0;j<6;++j){
scanf("%i",&v[i][j]);
}
}
for(i=0;i<n;++i){
for(j=0;j<6;++j){
for(k=0;k<6;++k){
if(v[i][j] == mega[k]){
v[i][j] = 61;
}
}
}
}
//reverse bubble sort
for(i=0;i<n;++i){
for(j=0;j<6;++j){
for(k=j+1;k<6;++k){
if(v[i][j] < v[i][k]){
swap = v[i][k];
v[i][k] = v[i][j];
v[i][j] = swap;
}
}
}
}
for(i=0;i<n;++i){
for(j=0;v[i][j] == 61 && j<6;++j);
if(j == 4) ++quadra;
else if(j == 5) ++quina;
else if(j == 6) ++sena;
}
return 0;
}
Your code is true, I understood and tried the flow of it. Looks fine but if you dont need to sort everyline (and use j as a counter in this loop for(j=0;v[i][j] == 61 && j<6;++j); ), you can use simpler ifstatements to compare real lottery results with the ones that entered. What I mean is that your algorithm is a little complex. Try a simple one and see how it works.
Yes, there are a couple of noteworthy issues with your code:
Compile time indicates possibility of uninitialized variable:
But, run-time results in fatal run-time at unknown source location. Stack overflow. It is likely due to this line:
int v[50500][6];
Increase your stack size. It needs to be about 2.5Mbytes for v alone.
Also, this line may not be what you intended:
for(i=0;i<6;++i) scanf("%i",&mega[i]); //first line, lottery results
^
If you meant to loop around the remainder of the code, remove the ; after the for() statement, and use curly braces:
for(i=0;i<6;++i) scanf("%i",&mega[i]) //first line, lottery results
{
scanf("%i",&n);
....
I like to make GDB set a break point when a variable equal some value I set, I tried this example:
#include <stdio.h>
main()
{
int i = 0;
for(i=0;i<7;++i)
printf("%d\n", i);
return 0;
}
Output from GDB:
(gdb) break if ((int)i == 5)
No default breakpoint address now.
(gdb) run
Starting program: /home/SIFE/run
0
1
2
3
4
5
6
Program exited normally.
(gdb)
Like you see, GDB didn't make any break point, is this possible with GDB?
in addition to a watchpoint nested inside a breakpoint
you can also set a single breakpoint on the 'filename:line_number' and use a condition.
I find it sometimes easier.
(gdb) break iter.c:6 if i == 5
Breakpoint 2 at 0x4004dc: file iter.c, line 6.
(gdb) c
Continuing.
0
1
2
3
4
Breakpoint 2, main () at iter.c:6
6 printf("%d\n", i);
If like me you get tired of line numbers changing, you can add a label
then set the breakpoint on the label like so:
#include <stdio.h>
main()
{
int i = 0;
for(i=0;i<7;++i) {
looping:
printf("%d\n", i);
}
return 0;
}
(gdb) break main:looping if i == 5
You can use a watchpoint for this (A breakpoint on data instead of code).
You can start by using watch i.
Then set a condition for it using condition <breakpoint num> i == 5
You can get the breakpoint number by using info watch
First, you need to compile your code with appropriate flags, enabling debug into code.
$ gcc -Wall -g -ggdb -o ex1 ex1.c
then just run you code with your favourite debugger
$ gdb ./ex1
show me the code.
(gdb) list
1 #include <stdio.h>
2 int main(void)
3 {
4 int i = 0;
5 for(i=0;i<7;++i)
6 printf("%d\n", i);
7
8 return 0;
9 }
break on lines 5 and looks if i == 5.
(gdb) b 5
Breakpoint 1 at 0x4004fb: file ex1.c, line 5.
(gdb) rwatch i if i==5
Hardware read watchpoint 5: i
checking breakpoints
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004004fb in main at ex1.c:5
breakpoint already hit 1 time
5 read watchpoint keep y i
stop only if i==5
running the program
(gdb) c
Continuing.
0
1
2
3
4
Hardware read watchpoint 5: i
Value = 5
0x0000000000400523 in main () at ex1.c:5
5 for(i=0;i<7;++i)
There are hardware and software watchpoints. They are for reading and for writing a variable. You need to consult a tutorial:
http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html
To set a watchpoint, first you need to break the code into a place where the varianle i is present in the environment, and set the watchpoint.
watch command is used to set a watchpoit for writing, while rwatch for reading, and awatch for reading/writing.
I need to teach C to children (10-15 years old, teaching is through a website) and I want to be able to show them a step by step execution of a program but I don't want them to use a debugger directly (too complex for them, they are total beginners).
My idea was to pre-compute all the needed data and to show it to them (with a cool javascript animation, with the current line in the code, the values of the variables and the standard output).
What I need is a way to run a debugger on a C code and to export the values of the variables at each possible step (no struct, just basic variables and arrays).
Is there any interface to gdb or some other debugger that can to that ?
For some context : we are training students for the IOI (International Olympiad in Informatics) though a website with courses, exercices (automatically corrected)...
The code (in C) can be edited , compiled, tested and submitted online (with a javascript editor). This way no need to install anything (at first) so more people can just "try it".
The basic "step by step" debugging was only to show the beginners how variables are modified, how a "for" or a "while" are working. The kind of stuff you can do on a whiteboard as a teacher. More advanced students will install some IDE and will/or not use the debugger.
So for the beginners we want them to be able to play, on the website, with some basic code (affectations, maths operations, function call,for,while,if) to "see things".
If you're limited to programs with specific input, or without input at all, you can maybe use gdb scripting, in something like this:
try.c (the input program):
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("the number now is %d\n", i);
i++;
}
return 0;
}
trace.gdb (a basic gdb script):
break main
run
while 1
info locals
step
end
quit
the results of gdb -x trace.gdb -batch try
Breakpoint 1 at 0x40053c: file try.c, line 6.
Breakpoint 1, main () at try.c:6
6 for (i = 0; i < 10; i++)
i = 0
8 printf("the number now is %d\n", i);
i = 0
the number now is 0
9 i++;
i = 0
6 for (i = 0; i < 10; i++)
i = 1
8 printf("the number now is %d\n", i);
i = 2
the number now is 2
9 i++;
i = 2
6 for (i = 0; i < 10; i++)
i = 3
8 printf("the number now is %d\n", i);
i = 4
the number now is 4
9 i++;
i = 4
6 for (i = 0; i < 10; i++)
i = 5
8 printf("the number now is %d\n", i);
i = 6
the number now is 6
9 i++;
i = 6
6 for (i = 0; i < 10; i++)
i = 7
8 printf("the number now is %d\n", i);
i = 8
the number now is 8
9 i++;
i = 8
6 for (i = 0; i < 10; i++)
i = 9
11 return 0;
i = 10
12 }
i = 10
0x000000300161ebbd in __libc_start_main () from /lib/libc.so.6
No symbol table info available.
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
trace.gdb:6: Error in sourced command file:
No frame selected.
There are ways to change gdb's output so you can perhaps tune the script to make the output parsable in a way that will allow you to make it something playable by javascript.
And you'll also need to make sure the program does not loop endlessly, probably by using convenience variables to limit number of while loops in the script.
hello i am trying to write to a FILE in a wanted line number using c programming language
and for some unknown reasons it doesnt get written
this is my checking code:
int main()
{
int x;
int counter = 0;
char buffer[MAX];
FILE* fp = fopen("sale_day.txt","w");
fprintf(fp,"5 orange 11\n");
fprintf(fp,"4 pelephone 222\n");
fprintf(fp,"3 mirs 4000\n");
fprintf(fp,"2 cellcom 302\n");
fprintf(fp,"1 tmobile 500\n");
fclose(fp);
fp = fopen("sale_day.txt","r+");
while (counter < 2)
{// jumping two rows
fgets(buffer,MAX,fp);// i tried using fscanf which didnt help aswell
counter++;
}
fflush(fp); // i tried with and without still doesnt work
fputs("$",fp);
fflush(fp); // i tried with and without still doesnt work
fclose(fp);
}
i expect to get :
5 orange 11
4 pelephone 222
$ mirs 4000
2 cellcom 302
1 tmobile 500
for some reason it stays as the following in "sale_day.txt" file
5 orange 11
4 pelephone 222
3 mirs 4000
2 cellcom 302
1 tmobile 500
even tho when i debug it it shows a "$" instead of the 3 digit
thanks in advance for your help !
The code works just fine, also without the fflush-lines. After running the program, line 3 is changed as follows:
Before:
3 mirs 4000
After:
$ mirs 4000
I ran your code like it is, only with this on top:
#include <stdio.h>
#define MAX 255
What are you expecting? When I run your code, I get:
5 orange 11
4 pelephone 222
$ mirs 4000
2 cellcom 302
1 tmobile 500
This is exactly right given what you've coded: read two lines (leaving you positioned at the third line), and write a $ char.
Note that file write operations overwrite existing file data, or append new data to the end of a file. They don't insert data (which may have been the operation you're expecting).