Debugging line by line the code
-
I am 100% sure that this question has been asked and answered million times, but I am not smart, and I want to ask it again. I want an explanation with much details.
I want to track the compiler when it compiles the program, step by step, line by line, instruction by instruction, command by command and molecule by molecule.
I have googled this question many times, I have used many search engines, I have prayed in God, I asked many experienced people, I have read all the Gospels of Qt Net Testament (for example this: http://doc.qt.io/qtcreator/creator-debug-mode.html), but I have not found a solution.... Breakpoints, step over, step into, Local and Expressions etc, make my life very difficult.
At least there is a way to put in all lines the command: ```
qDebug()<<FILE<<LINE;???
-
Hi and welcome.
I'm not clear on what you want to do.
I want to track the compiler when it compiles the program
Is that really what you want? You mean you want to trace the compiler when it compiles your app or your own application when it runs? Those are two very different tasks, achieved very differently.
At least there is a way to put in all lines the command (...)
It's not a command. It's a function call. Modifying the source to debug/trace an app is a bad idea. You are no longer debugging/tracing the app this way. You are debugging/tracing a modification of that app.
-
Hello Chris Kawa and thank you for your help. What I want is to understand very well how a software that was not created by me runs. I want to do this, with the most detailed analysis. For example I only know that the running starts in the first line of main.cpp and it goes to the next line etc... I want to see in every step where (the application? the program? the software? the compiler?) is and then where it goes. So maybe, you are right, I want to trace the application when it runs.
For example if the program is:
...
qDebug()<<"Hello";
qDebug()<<"Hi";
...And I change it to:
...
qDebug()<<FILE<<LINE; qDebug()<<"Hello";
qDebug()<<FILE<<LINE; qDebug()<<"Hi";
...I feel and think that it will help me very much...
This is what I am looking for. Is there a smarter way?
-
@Konstantinos said:
I feel and think that it will help me very much...
You don't need to do that. That's what debuggers are for.
Place a breakpoint on the first line in yourmain()
function. You can do that by either hitting F9 or clicking on the left margin next to that line. A red dot will appear indicating a breakpoint is set.
Now start the program with debugger attached. To do that either hit F5 or click the green arrow with a bug picture in the lower left corner of the IDE.
The program will start and then pause at the line you put the breakpoint in.
In this paused state you can inspect the state of every variable in your code by hovering the mouse over it (the info shows in the tooltip) or in the panel that appears on the right side of the editor.
Now you can go to the next line with F10 or the button above the panel on the bottom of the editor. You can go inside a function hitting F11 or another button in that panel.That's the very basics of debugging. You can go on from here.
-
@Chris-Kawa said:
You don't need to do that. That's what debuggers are for.
Place a breakpoint on the first line in your main() function. You can do that by either hitting F9 or clicking on the left margin next to that line. A red dot will appear indicating a breakpoint is set.
Now start the program with debugger attached. To do that either hit F5 or click the green arrow with a bug picture in the lower left corner of the IDE.
The program will start and then pause at the line you put the breakpoint in.Thanks, but it does not pause in the line that I put the breakpoint. For example I have put in all lines of main a breakpoint but the program starts and ends without any stopping, even I am in debug mode, and I start the program with the debugger attached. Is it possible the program not to use main.cpp at all?
-
Is it possible the program not to use main.cpp at all?
Sure. For example with a static global variable:
struct Foo { Foo() { // the whole app runs here exit(0); } } hahaImTheMainNow; int main(int argc, char *argv[]) { //will never get here }
That's actually pretty common in some frameworks, e.g. Microsoft's MFC, but I doubt that's what is happening here.
Can you create a new simple app from the wizard and verify that the debugger actually works and attaches to the process? -
Hi Chris Kawa and anyone else... Have a nice week.
I created a new simple app and the debuggerS (both that I have) work well. For example I wrote in a line qDebug()<<"HI";, and I put in this line a breakpoint. So the debugging stopped in this line, the "step over" got into the next line and then in 3 Application Output I saw the message: HI. Everything is working well in the new simple app, regardless of the debugger that I use.
But I went to do the same in the complicated app that I have who someone else has created and it did not work. in the first line of int main(int argc, char *argv[]), I wrote: qDebug()<<"HI"; and in the same line I put a breakpoint. The debugging starts, the message: HI appears in 3 Application Output, but the program does not stop running. It does not work regardless of what debugger I use.
In this case what I have to do? Is there a way to understand what is happening? I am wondering if the kind of Kit, Qt Version or Compiler causes this problem.
-
Maybe, it is something like this: http://doc.qt.io/qtcreator/creator-troubleshooting-debugging.html#debugger-does-not-hit-breakpoints
But I have no idea, how and where exactly to put the -g option. And even if I am in debug mode, and I start the program with the debugger attached, the 4. Compile Output says: C:/mingw32/bin/mingw32-make -f Makefile.Release
How to change it to -g Makefile.Debug? In Projects -> Build Steps or Projects -> Build Environment what exactly I have to do?
Or is there a way to put the -g option to .pro file?
Google really does not help me.
-
Hi Konstantinos,
maybe the author has addedCONFIG += release
in the .pro file, so the debug build configuration is not working.
If it´s there, delete it. In general, if you switch a .pro to debug build configuration, Creator just addsCONFIG += debug
to qmake step. You can check this in debug build setting in project settings. So the next thing you can try is to add this to your .pro file.
And always run qmake before you build the application if you made changes to the .pro file.