General information
In this blog I want to show how to debug the C++ Samples form the SDK for ctrlX AUTOMATION. I want to show three different possibilities to debug C++ Apps. To keep it simple, in this article I use the sample “samples-cpp/datalayer.register.node”, the ctrlX WORKS App Build Environment and the ctrlX COREvirtual with Port Forwarding (127.0.0.1:8443). But of course, it works with different setups as well. If you don't have the App Build Environment setup yet, please have a look at this How to video.
All three debug possibilities have different advantages and disadvantages and therefore different use cases. Please see this documentation of the SDK to get detailed information on this. To sum it up: We recommend to test your application extensively in the build environment (type #2). Testing an application running in it's confined Ubuntu Core snap environment can be done by looking at the snap logs (type #1). Remote Debugging (type #3) while the snap is running in the confined Ubuntu Core snap environment of a ctrlX CORE should only be used as the last step, if the other two debug types don't help to debug your application.
Prerequisites
Minimum:
- App Build Environment with ctrlX AUTOMATION SDK
- Visual Studio Code
- ctrlX CORE or ctrlX COREvirtual
Additional:
- For type 2 and type 3 of debugging you will also need the C/C++ extension of Visual Studio Code installed in your SSH: App Build Environment.
- For type 3 and partly type 1: To establish a ssh connection to the ctrlX CORE you need a system user on the device. For ctrlX COREvirtual the system user is present by default. For the hardware ctrlX CORE you can get a system user only for development purposes. Please contact the support team to get a system user on the real device. In addition you also need to activate SSH in the website of the ctrlX CORE in “Settings->SSH”.
- For type 3: you need to forward the port 12345 in the Port forwarding settings, if you use a ctrlX COREvirtual in Port forwarding mode.
Type 1: Snap logs
The easiest way of debugging: install the snap on your ctrlX CORE(virtual) and look at the log messages (cout) of your snap. Just build the snap in your App Build Environment and install it on a ctrlX CORE or ctrlX COREvirtual.
Now you got two possibilities to see the log messages in of your snap: you can either see them in the web interface in the logbook or in the console, when you login to the ctrlX CORE via ssh. To be able to connect via ssh to the ctrlX CORE (see Prerequisites).
Log Messages in the Logbook
See the Log Messages in the Logbook by clicking on Diagnostics/Logbook. First activate “Show system messages” in the Logbook settings and then set the filter for the unit of your snap (e.g. “snap.sdk-cpp-registernode.registerNode.service”):
Log Messages in the ssh console
To see the log output in the console, log in to your ctrlX CORE via SSH. For port forwarded ctrlX COREvirtual:
ssh -p 8022 [email protected] (password: rexroot)
To see the output use sudo snap logs command:
sudo snap logs -f snapname
Type 2: Debugging in Build Environment + Connection to Data Layer via TCP
It's recommended to debug your application in your ctrlX WORKS integrated App Build Environment. In this type of debugging the application is only running in the App Build Environment and not on the target and not in a snap environment! But you can make a TCP connection from the executable running in your App Build Environment to the Data Layer on the ctrlX CORE(virtual). Prerequisite for using the debugger in Visual Studio Code is the C/C++ Extension from Microsoft, to install this extension just search in the Extension browser for C/C++ and select to install this extension in SSH (see Prerequisites).
Bevor compiling the code please check if the tcp connection to the ctrlX COREvirtual matches with your setup. Have a look at the lines 182 and 183 in main.cpp. I copied out the line 182 used the line 183 instead to establish my provider connection to the port forwarded virtual ctrlX CORE. Please use the right settings for your setup here.
After that we have to build a executable with debug build type. Therefore copy the “build-snap-amd64.sh” call it “build-debug-amd64.sh” and modify it. Set “-DCMAKE_BUILD_TYPE=Debug” and “-DBUILD_SNAP=OFF”. Execute this script to build a debug version. You will find the generated executable at “generated/ubuntu20-gcc-x64/Debug”.
Now the executable in the App Build Environment can be started using a Debugger (these configurations to start the debugging are stored in .vscode/launch.json). Go to “Run and Debug” select the debug configuration “x64 Build Environment (local)” and start the debugging (green start button). Now you can go step by step through the code, run the program or hit breakpoints. Its also possible to at variables to watch and look at these values.
Type 3: Remote Debugging
Detailed Documentation can also be found here. Prerequisite is to have a root user on the ctrlX CORE(virtual) and enabled ssh access. In addition you also need the C/C++ extension in Visual Studio Code (see Prerequisites). If you use a ctrlX COREvirtual in port forwarding mode you also got to forward the port 12345 (see Prerequisites).
- Your C++ code has to raise a SIGSTOP signal in debug mode. See main.cpp line 165 – 173 in datalayer.register.node sample.
- You have to build your C++ snap in debug mode and install it on your ctrlX CORE(virtual). To build the snap in debug mode, modify the build-snap-…sh script for the architecture needed and add “-DCMAKE_BUILD_TYPE=Debug”.
- Restart the snap application manually via ssh with the command:The execution of the application will stop when the SIGSTOP signal is raised so that the gdb server is able to connect.
sudo snap run --experimental-gdbserver=:12345 <Snapname>.<Appname>
(You can also Run the Task “Launch Remote GDB Server”, but here is a bug in line 110 of the task.json you have to change the snapname from “register.node” “sdk-cpp-registernode”.)
Hint: you can setup a SSH Public Key Authentication with the ctrlX CORE so you don’t need to enter the password. - Start the gdb debugger in remote mode so that he connects to the remote gdb server. Therefor in launch.json the configuration set “x64 ctrlX virt. Port Forwarding (remote)” is stored. To start it, go to Run and Debug, select “x64 ctrlX virt. Port Forwarding (remote)” in the Dropdown menu and click on the green start button. Then click on the start button several times until the debugging is attached. Now you can run the code or can go step by step through the code.