PDA

View Full Version : Basic questions to debugging



emigrand
23rd February 2012, 20:05
Hi!

First of all my background: I am a biologist interested in programming, not a programmer. Nonetheless I worked my way into C++ and Qt in order to write a programm which plots A/D converter data, sets triggers etc. for a scientific application. After installing Qt and Qwt, which was very laborious for me, I managed to produce a programm which works suprisingly well. But when having a closer look at the log-file where adc data is exported to, I realised a sample interval of >= 4 ms, which is not enough for our applications (at least 0.1 ms should be reached). So now my idea was to perform a profiling in order to find the bottleneck in my code - and this is where the actual problem starts!

I quickly stumbled over a profiler called callgrind (or valgrind) which would do the job for me - but it seems to be only for linux. I couldn't find any other freeware which seems to work well with Qt. When I tried "verysleepy", I couldn't find my functions in the statistics. Anyway, more and more I realized that I have to build a debug version of my code in order to use any profiling software(!?). But I'm not sure if this is possible with my installation of Qt/Qwt. I was just happy when I got it running, but didn't spend any attention on how I build Qt/Qwt or what this means for debugging, respectively. To give you an idea of how I installed Qt/Qwt, please have a look at this protocoll, which I luckily wrote in order to reproduce installation:


Download qt-sdk-win-opensource-2010.05.exe from http://qt.nokia.com/downloads/sdk-windows-cpp
This contains mingw, the Qt libraries version 4.7.0 and Qt Creator IDE version 2.0.1
Install this C:\Qt\2010.05\
Download qt-creator-2.0.1-src.zip
Unpack this to C:\qt-creator-2.0.1-src\
Add the line "CONFIG += release" to qtcreator.pro (after "CONFIG += ordered"), just to be sure
Create a new folder C:\qt-creator-2.0.1-build\
Enter the "Qt Command Prompt" via the Start menu and type:
cd C:\qt-creator-2.0.1-build
qmake ..\qt-creator-2.0.1-src\qtcreator.pro
mingw32-make release

Create new system environment variable QTDIR and assign value C:\Qt\2010.05\qt
Add C:\Qt\2010.05\mingw\bin to system PATH (to make Mingw available. If you install Mingw seperately, than adjust path accordingly)
Add %QTDIR%\bin to system PATH (avoids copying of required Qt-dlls)
Download the newest version of qwt (6.0.1 ?) and unzip to C:\qwt-6.0.1
Add the line "CONFIG += release" to qwt.pro (after "CONFIG += ordered")
Follow qwt install manual. Briefly:
Open command shell, cd to Qwt-folder and type
qmake qwt.pro -spec win32-g++ -r CONFIG+=release
mingw32-make
mingw32-make install
Type qmake -set QMAKEFEATURES C:/Qwt-6.0.1 so that QT can find the qwt.prf file
Copy qwt_designer_plugin.dll to C:\Qt\2010.05\qt\plugins\designer\
Copy qwt.dll to C:\qt-creator-2.0.1-build\bin\

Start C:\qt-creator-2.0.1-build\bin\qtcreator.exe
Create new project
Change project to release mode (left panel, near to bottom)
Add following lines to every .pro file:
CONFIG += qwt (essential, points to qwt.prf file)
CONFIG += release (probably essential)
INCLUDEPATH += C:\Qwt-6.0.1\src (probably essential)
DEFINES += QT_DLL \ (not sure)
QWT_DLL (not sure)

If QWT-Wigets are not visible, try one or more of following things (not sure about their necessity):
Create new system environment variable INCLUDE and assign value %QTDIR%\include
Create new system environment variable LIB and assign value %QTDIR%\lib
Create new system environment variable QWTDIR and assign value C:\qwt-6.0.1
Create new system environment variable QMAKESPEC and assign value win32-g++
Enter the "Qt Command Prompt" via the Start menu and type:
qmake -set Q_PLUGIN_PATH C:\qwt-6.0.1\designer\plugins\designer


Here are are questions:

Am I right with my assumption, that I installed Qt and Qwt in release mode?
Does this mean, that I cannot build debug versions of my application?
I have got the debug button, so do I have a debugger which was provided with Qt-SDK (like gdb or so)?
Why does the program not stop at breakpoints?
Why does is say "process couldn't be started" when I choose debug instead of release?
What must my .pro file look like in order to debug or build a debug version? Do I have to add something like config += debug?
If I am on the wrong track, where do I have to start to get to my goal (find the bottlenecks)?



Any help would be appreciated. Please respect my background, so that I can understand you!

Thank you!

ChrisW67
24th February 2012, 05:01
1. Am I right with my assumption, that I installed Qt and Qwt in release mode?
Qt SDK: no, you get both sets of libraries. Qwt: yes, you went out of your way to build release.

2. Does this mean, that I cannot build debug versions of my application?
No. The compiler in the SDK can build either. You will need to make a debug version of Qwt if you need to debug/profile that code. Your program can be built for debugging either way.

3. I have got the debug button, so do I have a debugger which was provided with Qt-SDK (like gdb or so)?
Yes, gdb. It also included Qt Creator so you did not have to build your own.

4. Why does the program not stop at breakpoints?
Your program (not Qt or Qwt) is built in release mode or the code is never reached.

5. Why does is say "process couldn't be started" when I choose debug instead of release?
The path to the executable may have changed (shadow builds into different directory). Check your Build and Run Settings in Qt Creator. Given this, I guess the answer to 4. is "it's built in release mode."

6. What must my .pro file look like in order to debug or build a debug version? Do I have to add something like config += debug?
You could do that:


CONFIG += debug

or you can tell Qt Creator to create a debug version and it will add the relevant option to the qmake command on your behalf:


qmake CONFIG+=debug project.pro
My PRO files generally contain neither release nor debug and allow the qmake invokation to set.

If I am on the wrong track, where do I have to start to get to my goal (find the bottlenecks)?
You need a better grasp of your tools before you can go much further. Valgrind is Linux only. You could have a read here (http://stackoverflow.com/questions/4394606/beyond-stack-sampling-c-profilers) (or perhaps a MingW compatible gprof?).

You should also take a long look at the code that handles the frequent events. Look for where you are doing things every time that need only be done once or more than the necessary minimum to handle the event.

emigrand
24th February 2012, 10:22
You will need to make a debug version of Qwt if you need to debug/profile that code.


OK, first of all: This helped me understand a lot more than I did before, thanks! I can work with the debugger now, it stops at breakpoints, I can monitor variables, etc. - that's great. The error message "cannot find -lqwtd" I received when I tried to debug my plotting program was due to having built Qwt in release mode - this wasn't obvious to me at all :o. In the meantime, I found the bottleneck in my code (which I reduced to non-Qwt elements for testing): verysleepy says that a lot of time (82 %) is spent in the function cbVIn(), which is from the AD-converter library. Timecostly is as well replotting, I think.

Anyway, in order to be able to debug my code which contains Qwt elements, what do I have to do? As you said: make a debug version of Qwt - right? How do I do that? According to my protocoll mentioned above:


Download the newest version of qwt and unzip to C:\qwt-6.0.1
Add the line "CONFIG += debug" to qwt.pro (after "CONFIG += ordered")
Open command shell, cd to Qwt-folder and type qmake qwt.pro -spec win32-g++ -r CONFIG+=debug
type mingw32-make
type mingw32-make install


Do I have to remove the release version beforehand? What else do I have to keep in mind? Do I run the risk of destroying my previous Qwt build and spending weeks with reestablishing the system as it was?

Thank you for your patience...

ChrisW67
25th February 2012, 07:24
To debug your code using Qwt components does not require a debug Qwt: to debug Qwt code does.

Your build of the debug Qwt looks OK (the second step is unneeded because the third step supplied the debug option anyway). You should not need to remove the release version, which you can link using "-lqwt" or similar, only the end library should differ and it will generally have a different name. (BTW: you can install the release and debug version into entirely different locations if you wish).

In your code's PRO file you should link to the debug version of the Qwt library: i.e. "-lqwtd". This can be automated using qmake using scopes in your PRO file:


CONFIG(release, debug|release) {
message(Release build!)
LIBS+=-Lc:/qwt-6.0.1/bin -lqwt
...
}
CONFIG(debug, debug|release) {
message(Debug build!)
LIBS+=-Lc:/qwt-6.0.1/bin -lqwtd
...
}



Timecostly is as well replotting, I think.
Since no human is going to keep up with 1000+ samples/second anyway, you should consider capturing the data every cycle but only redrawing the plot every 500 samples or the like.

emigrand
25th February 2012, 13:42
Since no human is going to keep up with 1000+ samples/second anyway, you should consider capturing the data every cycle but only redrawing the plot every 500 samples or the like.


I implemented something similar already, so replotting efforts are minimized, problem vanished!


To debug your code using Qwt components does not require a debug Qwt:
This is what I want to do. Writing my own program using Qwt classes. Then I want to debug the whole thing.


to debug Qwt code does.
Where is the difference? Or would this mean debugging the Qwt libraries and stuff?? I wouldn't dare, should I? So according to my goal outlined above, I still assume that I don't need to make a debug vesion of Qwt in order to be able to debug code that uses Qwt classes. But: If I do so, I get the error messages already mentioned:

:: error: cannot find -lqwtd
:: error: collect2: ld returned 1 exit status


Your build of the debug Qwt looks OK Which build? I just asked if this is the way I would have to build it. But if I don't need to...(see above). But maybe I got this whole thing wrong again and I am supposed to build the debug version, otherwise I will not get rid of the errors??

I really would like to add these lines to my .pro file, because it looks clear and obvious:


CONFIG(release, debug|release) {
message(Release build!)
LIBS+=-Lc:/qwt-6.0.1/bin -lqwt
...
}
CONFIG(debug, debug|release) {
message(Debug build!)
LIBS+=-Lc:/qwt-6.0.1/bin -lqwtd
...
}


But I don't have a /bin-folder!!! Should this observation tell me something? Does it tell you something about my installation or mental condition?

I hope for the first...
Thank you Chris for your patience so far...

ChrisW67
25th February 2012, 22:39
If you need to single step through your code code then your code must be built with debug symbols. As you step through your code and it enters Qwt functions in the library you will either:

See a low level disassembly of the release mode Qwt library routine, or
A high level source code view of the debug mode Qwt library routine. (The source is embedded in the library to enable this.)

It seems unlikely that you want/need to debug the Qwt code. You can Step Over the Qwt calls to avoid the disassembly.

I am not intimately familiar with Windows Qwt builds. I assumed that the debug mode Qwt library would follow the same convention as the Qt libraries; appending a 'd' so that release and debug can exist side-by-side. If the debug library gets the same name, i.e. qwt.dll, then you are better off building release mode and installing into Path A, and debug mode installed into Path B and adjust INCLUDEPATH and LIBS accordingly.

I also assumed a directory structure. You will have to adjust paths to suit what has actually happened on your machine.