nasa / trick

Trick Simulation Environment. Trick provides a common set of simulation capabilities and utilities to build simulations automatically.
Other
32 stars 16 forks source link

Confused about debugging the trick source code #1619

Closed yusufcananar closed 8 months ago

yusufcananar commented 9 months ago

Hello,

I would like to develop a feature for Nasa Trick software according to my needs where I need to record datas to MongoDB on sim run time. Just like DataRecord service records data to .csv. I will use this data to real-time plot on a react based frontend. I have developed C++ project for mongodb connections and crud operations but I am not sure how can I implement it to trick. I don't know how to debug trick source code.  My working environment is Ubuntu 22.04 and I am using Vscode(with makefiletools extension). 

What I have tried so far is:

  1. I wasn't able to use cmake to build source code. So I am using make.
  2. I thought about attaching to the process. Created an S_main_Linux_11.4_x86_64.exe file from cannon_analytics example and tried to attach it. But my debug points (even though they are called in an input file such as dataRecord.add_variable()) are not entered. However when I shutdown the S_main_Linux_11.4_x86_64.exe my debugger waits for me to step in. 

I am very confused about whether I am wrong about the debugging approach or am I facing IDE's environment settings or debugging issues. But I am able to debug my other C++ projects with the current settings.

I have looked through the documentations but I could not resolve the issue by myself. Would you help me to find out how can I debug Nasa Trick source?

tastangh commented 9 months ago

Hello everyone,

I am developing a code that writes simulation values ​​to influxdb and visualizes the data in Grafana. I could not debug the trick source code in VsCode either. If you solve the problem, I would be happy if you write it as an answer.

GITburakdeniz commented 9 months ago

Hi, I have been trying to debug the NASA Trick source for a while. On my other C++ projects, I use GDB for debugging which can be used for breakpoints but for NASA Trick can't figure out how to debug. I planned to develop a new feature for NASA Trick to use in simulation time but couldn't add the feature to the source.

@Fjolnirr @tastangh If you can figure out how to debug, Could you share it with me?

yusufcananar commented 9 months ago

I saw this issue #1500 I guess cmake is still broken.

yusufcananar commented 9 months ago

I have managed to debug whole trick code. Trick sources(services), trick ICG, and simulation model source codes. I will explain what you need to do to debug trick step by step. @tastangh @GITburakdeniz

Before making any changes we will set few env variables:

export CFLAGS="$CFLAGS $1 -O0 -g"
export CXXFLAGS="$CXXFLAGS $1 -O0 -g"
export LDFLAGS="$LDFLAGS $1 -O0 -g"
export TRICK_CFLAGS="$TRICK_CFLAGS $1 -O0 -g"
export TRICK_CXXFLAGS="$TRICK_CXXFLAGS $1 -O0 -g"
export TRICK_LDFLAGS="$TRICK_LDFLAGS $1 -O0 -g"
export TRICK_SYSTEM_CFLAGS="$TRICK_SYSTEM_CFLAGS $1 -O0 -g"
export TRICK_SYSTEM_CXXFLAGS="$TRICK_SYSTEM_CXXFLAGS $1 -O0 -g"
export TRICK_SYSTEM_LDFLAGS="$TRICK_SYSTEM_LDFLAGS $1 -O0 -g"

save and exit.

  1. First you need to add this lines to the Makefile of the trick located at trick/Makefile

    CXX = g++
    CXXFLAGS = -std=c++11 -g
  2. ./configure and then make the trick at TRICK_HOME dir. (You can use make -j8 for faster compilation time)

  3. You need to have simulation like SIM_cannon_analytic (I have used the one located in trick/trick_sims/Cannon/SIM_cannon_analytic

  4. Open S_overrides.mk and paste this lines:

TRICK_CFLAGS += -I../models -I/home/backend/trick/trick_source -g 
TRICK_CXXFLAGS += -I../models -I/home/backend/trick/trick_source -g 
  1. In terminal, use trick-CP -g command:
    backend@backend-VirtualBox:~/trick/trick_sims/Cannon/SIM_cannon_analytic$ trick-CP -g

  2. Open vscode at TRICK_HOME dir.

  3. Create a launch.json file. This is for attaching to process on launch.

    {
    "version": "0.2.0",
    "configurations": [
        {   
            "name": "(gdb) Launch SimExe",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/trick_sims/Cannon/SIM_cannon_analytic/S_main_Linux_11.4_x86_64.exe",  
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",  
            "additionalSOLibSearchPath": "${workspaceFolder}/lib",
    
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]            
        }
    ]
    }
  4. make sure you have makefile tools extension installed.

  5. In extension editor, select launch target "${workspaceFolder}/trick_sims/Cannon/SIM_cannon_analytic/S_main_Linux_11.4_x86_64.exe"

  6. Open "Run&Debug" menu.

  7. select your launch in this case mine was "(gdb) Launch SimExe"

  8. place your break points all over your model source, or trick sources.

  9. Finally, click the Start Debugging(F5) button.

This is how I have managed to debug trick source and my own model sources. I hope it works for you guys as well. If trick people approve I can turn this answer into tutorial.

hchen99 commented 9 months ago

Thanks for all the details. Trick does have a couple of docs listed below as examples that contain info on debugging need although not how to use a specific IDE such as vscode to debug. We'll discuss whether to add an IDE specific tutorial as there are various ways.