Debugging
One of the most frequently used tools in the traditional design-develop-debug cycle is the source-level debugger. In the QNX Momentics IDE, this powerful tool provides an intuitive debugging environment that's completely integrated with the other workbench tools, giving you the flexibility you need to address the problems that you encounter when developing your apps.
To take advantage of the debugging capabilities of the IDE, you must use application executables compiled for debugging. These executables contain additional debug information that lets the debugger make direct associations between the source code and the binaries generated from that original source. In the IDE, you'll see different icons: an arrowhead icon for executables that weren't compiled for debugging, or a bug for those that were.
The IDE debugger uses GNU Debugger (GDB) as the underlying debug engine. The debugger translates each GUI action into a sequence of GDB commands, and then processes the output from GDB to show the current state of the application being debugged.
The IDE updates the views in the Debug perspective only when the application is suspended (for example, when the debugger hits a breakpoint, or when you're stepping through the code line by line).
Debugging an application
Debugging is now supported for both C++ and QML/JavaScript. Before you can start the debugger, you must first build binaries using the correct build configuration (either Device-Debug or Simulator-Debug) and you must create a debug launch configuration for the target you want to debug the application on.
To debug QML/JavaScript code, you need to add the following lines into existing code in your main.cpp file:
#include <QLocale> #include <QTranslator> #define QT_DECLARATIVE_DEBUG #include <Qt/qdeclarativedebug.h>
You must also enable JavaScript debugging in the QNX Momentics IDE.
- In the QNX Momentics IDE, click Window > Preferences > JavaScript > Debug.
- Click QML and JavaScript.
- Select Enable JavaScript Debugging.
- Click Apply.
- Click OK.
To start the debugger, right-click the project and select Debug As > BlackBerry C/C++ Application.
The IDE now switches to the Debug perspective and transfers your application from your computer across the network to your simulator or device, and then starts it under the control of the debugger. You will see that the debugger stops on the first line of your application. In the Debug view, you'll see an overview of your process, including the call stack. Using the buttons in the main bar of the Debug view, you can control the debugger. In an application that contains both C++ and QML the debugger is able to pass back and forth between the two.
For more information on debugging, see the QNX Momentics IDE User's Guide.
Other debugging techniques
Sending output to the device log
Besides running the debugger there are some other ways that you can debug your Cascades application. In JavaScript, you can use the console.log() function to output text and variables to the slogger2.log. You can now view all slogger2.log information in the Device Log view in the QNX Momentics IDE. To open the Device Log view, on the Window menu, click Show View > Other. Expand Debug and select Device Log.
For example, within the CircularSlider application, we capture the valueChanged() signal and output the slider value to the slogger2.log when the value changes:
CircularSlider {
layoutProperties: DockLayoutProperties {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
}
onValueChanged: {
console.log("Slider value: " + value);
}
}
Using qDebug()
In C++, a common way to output text and variables to the slogger2.log is by using qDebug(). Here's an example of how QCompass readings are displayed:
QCompassReading *reading = m_CompassSensor->reading(); qreal azimuth = reading->azimuth(); qDebug() << "The azimuth is " << azimuth << " degrees.";
To see debug information that is output from qDebug(), you can access the slogger2 logs directly as follows:
- In the QNX Momentics IDE, in the Target Navigator view, right-click the device target. Click Launch SSH Session.
- In the terminal that appears, do one of the following:
- To view the current slogger2 logs, type slog2info.
- To view real-time output for the processes that are being debugged (for applications that are running in development mode), type slog2info -w.
- To view help information on slogger2, type slog2info -h.
You can also access the log files directly in the /tmp/slogger22 on the device and run these logs through slog2info at a later time.
Using fprintf() to send output directly to the console
You can use the fprintf() function with stdout or stderr as the output stream to receive output directly to the console. Or, you can use a function similar to the following:
void myMessageOutput(QtMsgType type, const char* msg){
fprintf(stdout, "%s\n", msg);
fflush(stdout);
}
Then, you can register this handler function with qDebug by calling the qInstallMsgHandler() function in your main function after the default Application creation, similar to the following:
int main(int argc, char **argv)
{
Application app(argc, argv);
qInstallMsgHandler(myMessageOutput);
...
}
After this, qDebug() calls will be logged to the console.
Last modified: 2013-06-12