Visual Studio Code에서 GDB를 사용하여 원격 디버깅하는 방법을 다룹니다.
진행을 위해 호스트는 윈도우가 설치된 노트북, 타겟은 라즈베리파이3를 사용했습니다.
2021. 2. 28 최초작성
관련 포스팅
GDB를 사용한 원격 디버깅
https://webnautes.tistory.com/1469
1. 아래 링크를 참고하여 Visual Studio Code를 설치합니다.
Visual Studio Code 설치하는 방법( Windows / Ubuntu )
https://webnautes.tistory.com/1197
Visual Studio Code에서 C/C++ 프로그래밍하는 방법은 아래 글을 참고하세요.
Visual Studio Code에서 C/C++ 프로그래밍( Windows / Ubuntu)
https://webnautes.tistory.com/1158
2. 원격 GDB를 사용하기 위해 필요한 작업이 있습니다. 아래 글을 먼저 진행하세요.
GDB를 사용한 원격 디버깅
https://webnautes.tistory.com/1469
3. Visual Studio Code에서 새로운 폴더를 열어서 다음 2개의 파일을 추가합니다.
helloWorld.cpp
#include <iostream> int main() { int a = 1; int b = 2; int c = a + b; std::cout << c << std::endl; } |
Makefile
Makefile의 CFLAGS 옵션에 -g 옵션을 추가해야 합니다.
CC=arm-linux-gnueabihf-g++.exe CFLAGS=-g all: helloWorld.cpp $(CC) helloWorld.cpp $(CFLAGS) -o helloWorld clean: rm -f helloWorld |
4. Visual Studio Code의 메뉴에서 “터미널 > 작업 구성”을 선택합니다.
템플릿에서 tasks.json 파일 만들기를 클릭합니다.
Others를 선택합니다.
탐색기에 tasks.json 파일이 추가됩니다.
tasks.json 파일을 다음 내용으로 대체합니다.
{ "version": "2.0.0", "tasks": [ { "label": "Make: run project's Makefile", "command": "make", "group": "build", "problemMatcher": { "fileLocation": [ "relative", "${workspaceRoot}" ], "pattern": { // The regular expression. //Example to match: helloWorld.c:5:3: warning: implicit declaration of function 'prinft' "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } },
] }
|
5. helloWorld.cpp 파일 내용 이 보이는 상태에서 Ctrl + Shift + B를 누릅니다.
Make: run project’s Makefile을 선택합니다.
빌드됩니다.
> Executing task: C:\SysGCC\raspberry\bin\make.exe <
arm-linux-gnueabihf-g++.exe helloWorld.cpp -g -o helloWorld
터미널에 helloWorld 파일이 추가됩니다.
다음과 같은 에러가 난 경우 Makefile에서 들여쓰기 한 부분을 tab 키 입력 한번으로 수정해주세요.
C:\Users\webnautes\test>make
Makefile:5: *** missing separator. Stop.
6. 메뉴에서 “터미널 > 새 터미널”을 선택합니다.
터미널에서 다음 명령으로 실행파일을 타켓으로 복사합니다.
helloWorld는 실행 파일 이름입니다.
192.168.43.141는 타겟의 아이피입니다.
/home/pi 위치에 실행파일이 복사됩니다.
scp.exe helloWorld pi@192.168.43.141:/home/pi
C:\Users\webnautes\helloWorld>scp.exe helloWorld pi@192.168.43.141:/home/pi
pi@192.168.43.141's password:
helloWorld 100% 29KB 1.0MB/s 00:00
7. F5키를 누릅니다.
“C++ (GDB/LLDB)”를 선택합니다.
“make”를 선택합니다.
launch.json 파일을 다음 내용으로 대체합니다. .
{ // IntelliSense를 사용하여 가능한 특성에 대해 알아보세요. // 기존 특성에 대한 설명을 보려면 가리킵니다. // 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요. "version": "0.2.0", "configurations": [ { "name": "make - 활성 파일 빌드 및 디버그", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\helloWorld", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\SysGCC\\raspberry\\bin\\arm-linux-gnueabihf-gdb.exe", "miDebuggerServerAddress": "192.168.43.141:2001", "setupCommands": [ { "description": "gdb에 자동 서식 지정 사용", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "Make: run project's Makefile" } ],
} |
program은 디버그할 프로그램 이름입니다.
miDebuggerPath는 호스트에 있는 크로스 디버거 이름입니다.
miDebuggerServerAddress는 타겟의 아이피와 포트로 변경합니다.
8. 퍼미션을 수정한 후, 타겟에서 gdbserver를 실행합니다.
chmod +x helloWorld
gdbserver localhost:2001 helloWorld
pi@raspberrypi:~ $ chmod +x helloWorld
pi@raspberrypi:~ $ gdbserver localhost:2001 helloWorld
Process /home/pi/helloWorld created; pid = 927
Listening on port 2001
9. 호스트의 Visual Studio Code에서 소스 코드 파일이 보이는 상태에서 브레이크 포인트를 추가합니다.
5번째 줄 왼쪽을 마우스 클릭하여 브레이크 포인트를 추가합니다. 빨간원이 추가됩니다.
10. 다시 한번 F5를 누르고 F10을 눌러 한줄씩 실행할 수 있습니다.
타겟에서 확인해보면 3이 출력됩니다.
pi@raspberrypi:~ $ gdbserver localhost:2001 helloWorld
Process /home/pi/helloWorld created; pid = 998
Listening on port 2001
Remote debugging from host 192.168.43.50
3
참고
https://opencoursehub.cs.sfu.ca/bfraser/grav-cms/cmpt433/guides/files/DebuggingGuide.pdf