GDB: Force through an if statement - c

This is the structure of my code
if(someFunction())
{
// Some code
}
where someFunction() evaluates to 0 most of the time
When GDB is at line 1 above, if I do next then // Some code will not be executed.
Is there a way to tell GDB to execute the code inside the if statement?

I can just propose you a workaround. Have a temporary variable int i=0 and then do the if as
if(i==1){
//some code
}
When you reach the desired position with gdb. Set i to 1 using
set i = 1
and then your loop will be executed. Of course after the loop you will have to reset you i if you don't want it executed every time.

You can jump to // Some code after stopping on if statement in gdb, unless // Some code was not optimized out, see 17.2 Continuing at a Different Address. Assuming you stopped on if, you can:
jump +2

0 means false, so it will not entering into if loop, use
if(1)

Related

Why does this for-loop and while-loop produce different outcomes, even though their values are the same? Btw I am using Javascript

My novice understanding of the difference between for-loops and while-loops is that they differ in format and we use for-loops when we know the exact number of iterations to complete and we use while-loops when we know what condition must be met to stop. That said could someone explain the difference in outcome of the following code?
let countDown = 2;
while(countDown>1){
countDown--;
console.log(countDown)
}
1
for(let countDown = 2; countDown>1; countDown--){
console.log(countDown)
}
2
As U. Windl had commented, for is just "syntactic sugar" for a while loop. The output of your program varied for for and while loop because in while loop countDown is decremented first and then logged, in for loop countDown is logged first and then decremented. With the below change in while loop the output would be same for both and its equivalent to for loop.
let countDown = 2;
while(countDown>1){
console.log(countDown)
countDown--;
}
Lets see what for and while loop says JavaScript For Loop
The For Loop
The for loop has the following syntax:
for (statement 1; statement 2 (condition); statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
While Loop
while (condition) {
// code block to be executed
}
In your program in for loop Statement 3 (i.e countDown--) is executed after the code block (i.e console.log) has been executed, that is the reason the output was 2.
In while loop we have to take care when to execute Statement 3 (i.e countDown-- in this case) and in for loop Statement 3 is always executed after executing code block
for equivalent for while
for (; condition; ) {
// code block to be executed
}
while equivalent for for
Statement 1
while (Statement 2) {
// code block to be executed
Statement 3
}

LLDB - count the number of steps to execute a thread or to execute a function eg. main(), and skip to Xth step

(1)
Is there a way to count the number of steps it takes to execute a function or a thread with LLDB?
As an example,
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); // step count: 1
int number;
int number2;
int number3;
number = 50; // step count: 2 (lldb skips declarations)
number = 60; // step count: 3
/*
Comment comment
*/
if(number < 100) // step count: 4 (lldb skips comments)
printf("Number is less than 100!\n"); // step count: 5
else if(number == 100)
printf("Number is 100!\n");
else
printf("Number is greater than 100!\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n"); // step count: 6 (the lines that are not executed bec of branching are not counted.)
return 0; // step count: 7
}
The step count is provided in the comments in the code above. And I only count step-overs.
The step count is unique to each execution of the code, and is expected to change if command line arguments and environment variables change and affect the control flow of the program.
If there are loops, each iteration will mean counting the lines in the loop again. So if there are 2 steps per iteration and there are 10 iterations, then there are 20 steps for that loop.
(2)
(Although I only count step-overs, I appreciate answers that also tell me how I can configure to include step-ins when I need them, or exclude them when I don't need them.)
(3)
In addition, is there a way to jump to a specific step in that step count? By this, I think of the jump command and How to skip a couple of lines code with lldb?.
However, what if the code has loops? Say:
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
There are 6 static lines of code (counting all those with only the curly brackets as well). However, the number of steps is probably 21. I wish I could jump to the 10th step, for example. Is it possible with lldb?
Another way to count steps is to write a scripted stepping plan that just keeps pushing new "step in" or "step over" plans till you reach whatever your terminating condition is, and updates some python variable each time an individual step is concluded. For more info on writing scripted thread plans, see:
https://lldb.llvm.org/use/python-reference.html#using-the-python-api-to-create-custom-stepping-logic
and there are several examples of scripted stepping plans here:
https://github.com/llvm/llvm-project/blob/master/lldb/examples/python/scripted_step.py
I'm not quite sure what you mean by "jumping to a specific step". If you mean you just what to stop at that point without having to manually intervene, it would be fairly straightforward to make a scripted stepping plan with whatever end conditions you want (the 10th time you hit line 20, or whatever).
But if you mean "get to that stage in the execution w/o going through the intervening code" that's not a trivial problem. How would lldb know what state was modified along the way to that point? For instance to get to a certain iteration of a loop, lldb would have to know to set the loop counter to whatever value it has at that point, which it has no way of knowing.
Instead of counting steps, and executing a counted number of steps, you can use breakpoints in most cases. For example, in the for loop example, a breakpoint can be configured to stop only under the right conditions.
For these examples, I'll assume the printf is on line 23 of file.c.
To stop part way through the loop, instead of executing a number of next commands, a breakpoint condition can be used to stop only when the loop index i is the desired number:
b file.c:23
breakpoint modify --condition 'i == 5'
The condition expression is a evaluated by lldb, so it can be more complex as needed.
In this case, since the condition is a count, another way of doing this is to set an ignore count on the breakpoint:
b file.c:23
breakpoint modify --ignore-count 5
Note: instead of --condition and --ignore-count, you can alternatively use -c and -i.

While Loop waiting for global variable not triggering

I have a flag (as a global variable) which is waiting for a certain time to pass before being set. I have a while loop which waits for the flag to set before proceeding. The global variable does get set to '1' but the while loop does not exit, any ideas what I'm doing wrong:
while (TC3Flag == 0); //Global Flag Variabe TC3Flag, 0 = Not Set, 1 = Set
TurnOnFive();
TurnOnTwelve();
TC3Flag = 0;
Edit i also have tried with the same results, I do want the processor to do nothing while the flag is 0 and only call TurnOnFive and TurnOnTwleve after the flag is set
while (TC3Flag == 0) {}
TurnOnFive();
TurnOnTwelve();
TC3Flag = 0;
EDIT EDIT: Adding volitile to the TC3FLag declaration fixed it.
Unless TC3Flag is marked as volatile, the compiler can aggressively optimize the loop away (or just assume it's an infinite loop). You should mark TC3Flag as volatile in order to force the compiler to read the value at its memory address on every iteration.

What is wrong in this C code ? If its a logical error please suggest the right way

int i;
for(i=7;i<6;i--)//loop does not execute
{
printf("*");
}
The above code should print one * but it does nothing. Shouldn'tit run same as for(i=7;i<8;i++) ?
Is this a logical error ? Please help.
A for loop has 3 parts
for( init ; cond ; step )
When the execution reaches the loop,
init is executed.
cond is evaluated.
If false, break the loop
If true, proceed to the next step
Execute the body of the loop.
Do step(in many cases, this is increment/decrement)
Goto step 2
In your case , i is set to 7. Then the condition i<6 is checked. Obviously, 7<6 is false. So the loop never gets executed.
And No.
for(i=7;i<6;i--)
and
for(i=7;i<8;i++)
aren't the same.
Perhaps you wanted to write
for(i=7;i>6;i--) //'>' instead of '<'
in which the loop will execute once.
Let's look at the loop
for( i = 7 ; i < 6 ; i-- )
i is initialized 7, but you have the condition i < 6, but i is 7, and therefor, it does not satisfy the condition of the loop. So, the code does not even go through one iteration of the loop.
Maybe, you meant i > 6
In this loop
for(i=7;i<6;i--)
at first you set i to 7 and then check whether i is less than 6. As i is equal to 7 then it is not less than 6 and the loop iterate never.
If you want that the loop would iterate one time then you should write
for(i=7; i > 6;i--)
Though with these magic numbers the loop looks strange.:) It is not clear what is the intention of the programmer.
int i;
for(i=7;i<6;i--)//loop does not execute
{
printf("*");
}
In first execution of this loop value of "i" is 7
next we have to check the condition "i<6" it is false because 7<6 is false ,then it not give any output
for(i=7;i<8;i++) is different because first "i" value is 7
then it checks condition it is i<8 (7<8) it is true.then it executes once ,after that it increment from 1 (i++),then it checks condition .then it is false because "i" is 8
then it stops execution.

How to go at particular value of counter in for loop using gdb?

I have a for loop of about 100 odd values. I want to have a breakpoint where I can set some value for the iterator variable and then directly go on that program execution state.
For example
for(int i=0;i<500;i++)
{
doSomething();
}
Here I want to have a breakpoint on i=100; and step through all values from 0 to 99 in one go. Is this possible in gdb and how do I do it?
In gdb you can set conditions on breakpoints.
break line if i == 100
Where "line" is the appropriate line number.
There is probably a better way, but I have gotten a lot of mileage out of doing things like this:
if (i == 100) {
int breakpoint = 10;
}
A conditional breakpoint is one that only transfers control to gdb only when a certain condition is true.
This can be useful when you only want gdb control after say 10 iterations of a loop.
To set a condition on a breakpoint, use the condition command with the number of the breakpoint followed by the condition on which to trigger the breakpoint.
Here is an example where setting a conditional breakpoint that will only be triggered when the "condition (i >= 10) is true":
(gdb) break 28 # set breakpoint at line 28
(gdb) info break # list breakpoint information
Num Type Disp Enb Address What
1 breakpoint keep y 0x080588a3 in loopit at loops.c:28
(gdb) condition 1 (i >= 10) # set condition on breakpoint 1
(gdb) run (or write `continue` if already running)

Resources