Bild1.gif

Debug C++ Apps with SDK for ctrlX AUTOMATION

nickH
Community Moderator
Community Moderator
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.
    Install C/C++ extension in Visual Studio Code in SSH App Build EnvironmentInstall C/C++ extension in Visual Studio Code in 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. 
    port forwarding settings - forward the port 12345port forwarding settings - forward the port 12345 
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 ctrlX CORE LogbookLog Messages in the ctrlX CORE Logbook

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 rexroot@127.0.0.1                                 (password: rexroot)

 

 

To see the output use sudo snap logs command:

 

 

sudo snap logs -f snapname

 

 

snap logs in console logbooksnap logs in console logbook

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”.

build-debug-amd64.shbuild-debug-amd64.sh

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.

Start Debugging in App Build EnvironmentStart Debugging in App Build Environment

 

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). 

  1. Your C++ code has to raise a SIGSTOP signal in debug mode. See main.cpp line 165 – 173 in datalayer.register.node sample.
  2. 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”.
    Build a snap with debug typeBuild a snap with debug type
  3. Restart the snap application manually via ssh with the command:
    sudo snap run --experimental-gdbserver=:12345 <Snapname>.<Appname>
    The execution of the application will stop when the SIGSTOP signal is raised so that the gdb server is able to connect.
    (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. 
  4. 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.
    Remote Debugging attached to GDB ServerRemote Debugging attached to GDB Server

     

Documentation
Support
Related Links
1 Comment
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist