|
| 1 | +# Debug Setup in VS Code |
| 2 | + |
| 3 | +Debugging NS-3 with VSCode (built with waf) is divided into the following steps: first compile NS-3, then configure the debugging tasks in VSCode (launch.json and tasks.json), and finally you can set breakpoints, single-step debugging, and view variables in the IDE. |
| 4 | + |
| 5 | +## 1. Compiling NS-3 |
| 6 | +Before debug NS-3 make sure you have successfully compile the NS-3 by follwing the steps in [ vm-env.md](#vm-env.md) |
| 7 | + |
| 8 | +## 2. Configuring Debugging Tasks for VS Code |
| 9 | + |
| 10 | +Configuring `tasks.json`: `tasks.json` is used to define build tasks that can automatically invoke build commands before starting debugging. Create a `.vscode/tasks.json` file with content similar to the following: |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "version": "2.0.0", |
| 15 | + "tasks": [ |
| 16 | + { |
| 17 | + "label": "Build NS-3", |
| 18 | + "type": "shell", |
| 19 | + "command": "./ns3", |
| 20 | + "args": [ |
| 21 | + "build" |
| 22 | + ], |
| 23 | + "options": { |
| 24 | + "cwd": "${workspaceFolder}" |
| 25 | + }, |
| 26 | + "group": { |
| 27 | + "kind": "build", |
| 28 | + "isDefault": true |
| 29 | + }, |
| 30 | + "problemMatcher": [] |
| 31 | + } |
| 32 | + ] |
| 33 | +} |
| 34 | +``` |
| 35 | +Configuring `launch.json`: `launch.json` is used to tell VSCode how to launch the debugger to run the NS-3 emulation program. Since NS-3 uses waf scripts to manage execution, you can launch the emulation via waf and debug the emulation scripts you specify internally. Create a `.vscode/launch.json` file with content similar to the following (change the `exampleA` to the code name, which you want to debug): |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "version": "0.2.0", |
| 40 | + "configurations": [ |
| 41 | + { |
| 42 | + "name": "Debug NS-3: exampleA", |
| 43 | + "type": "cppdbg", |
| 44 | + "request": "launch", |
| 45 | + "program": "${workspaceFolder}/ns3.39/build/contrib/p4sim/examples/exampleA", |
| 46 | + "args": [], |
| 47 | + "stopAtEntry": false, |
| 48 | + "cwd": "${workspaceFolder}/ns3.39", |
| 49 | + "environment": [], |
| 50 | + "externalConsole": false, |
| 51 | + "MIMode": "gdb", |
| 52 | + "setupCommands": [ |
| 53 | + { |
| 54 | + "description": "Enable GDB beautification printing", |
| 55 | + "text": "-enable-pretty-printing", |
| 56 | + "ignoreFailures": true |
| 57 | + }, |
| 58 | + { |
| 59 | + "description": "Set the disassembly format to Intel", |
| 60 | + "text": "-gdb-set disassembly-flavor intel", |
| 61 | + "ignoreFailures": true |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + ] |
| 66 | +} |
| 67 | +``` |
| 68 | + **program**: here you specify the path to the waf script. Since NS-3 simulation is started by waf, debugging the waf process directly will start the corresponding executable file inside waf. |
| 69 | + |
| 70 | +**args**: the running parameter passed to waf. You can change it to the path of the script you want to run or other parameters according to the actual situation. |
| 71 | + |
| 72 | +## 3.Starting Debugging |
| 73 | +In VSCode's debugging panel (the “Run and Debug” icon in the sidebar), select the “Debug NS-3 Simulation” configuration you just configured. |
| 74 | +Set the breakpoints you want. |
| 75 | +Click on the Start Debug button. The debugger calls the pre-configured build task (tasks.json), then starts waf and executes the specified NS-3 simulation program. |
| 76 | +When it reaches the breakpoint, VSCode pauses the program and you can view the call stack, variable values, and single-step execution code. |
0 commit comments