VSCode와 Ubuntu GDB연결하기(C Debug)

hooooon·2022년 10월 21일
9

vscode-C-Debug

목록 보기
2/3

참고

EC2 Ubuntu(Version 22.04)기준으로 진행됩니다.
기존 gdb(Version 12.0.9)에서 printf()관련 Debug문제가 발생합니다.
gdb(Version 12.1)로 업그레이드 방법도 아래에서 확인하실 수 있습니다.

0. VSCode와 EC2연결을 우선 진행해야합니다.

관련 글은 해당 링크를 참고해 주세요
https://velog.io/@hoon25/AWS-EC2-%EC%99%80-VScode%EC%97%B0%EA%B2%B0%ED%95%98%EA%B8%B0

1. C관련 VScode Extension 설치

C/C++C/C++Extension Pack 2개를 설치해줍니다.

2. vscode 설정파일 입력

.vscode 폴더에 파일3개를 생성해줍니다.
(경로 : vsCode 좌측 Workspace 최상단에 .vscode 폴더가 존재해야합니다.)

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - 활성 파일 빌드 및 디버그",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry":false,
            "cwd": "${fileDirname}",
            "environment":[],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/local/bin/gdb",
            "setupCommands": [
                {
                    "description": "gdb에 자동 서식 지정 사용",
                    "text": "-enable-pretty-printing",
                    "ignorefailures": true
                },
                {
                    "description":  "디스어셈블리 버전을 att(으)로 설정",
                    "text": "-gdb-set disassembly-flavor att",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc build"
        }
    ]
}

변경사항) 디스어셈블러 CSAPP책에 맞춰 변경
before

"description":  "디스어셈블리 버전을 Intel(으)로 설정",
                    "text": "-gdb-set disassembly-flavor intel",

after

"description":  "디스어셈블리 버전을 att(으)로 설정",
                    "text": "-gdb-set disassembly-flavor att",

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "gcc build",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
                "${fileDirname}/${fileBasenameNoExtension}.c",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": ["$gcc"],
            "group": "build"
        },
        // 실행
        {
            "label": "execute",
            "command": "cmd",
            "group": "test",
            "args": [
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ]
        }
    ]
}

주의사항)
아래 gdb업그레이드 설치 시 /usr/local/bin/gdb로 생성됨에 따라
launch.json의 miDebuggerPath를 /usr/local/bin/gdb로 설정했습니다.

gdb업그레이드 없이 사용하시는 분들은
초기 설치 경로인 /usr/bin/gdb로 입력해두시면 됩니다.

3. gdb 12.1 업그레이드

gdb 12.1 설치 명령어를 순서대로 진행해 주시면 됩니다.

sudo apt update
sudo apt upgrade
sudo apt install gcc make valgrind g++

mkdir tmp
cd tmp
wget http://ftp.gnu.org/gnu/gdb/gdb-12.1.tar.gz
sudo apt-get install libgmp-dev
tar -xvzf gdb-12.1.tar.gz
cd gdb-12.1
./configure
make
sudo make install

주의) make 명령어 동작시 시간이 오래 소요됩니다.
ec2 CPU사용률이 100%을 넘어서 멈출 경우 ec2 재부팅을 해주세요
그 다음에 make명령어 다시 입력하면 이어서 진행됩니다.

참고) Ubuntu 22.04 gdb 버그리포트
https://github.com/microsoft/vscode-cpptools/issues/9253

4. 디버그 진행

디스어셈블러로 어셈블러도 확인할 수 있습니다.
문제가 되었던 printf()부분도 정상 진행되는 것을 확인할 수 있습니다.

make 관련 디버깅 설정은 아래 링크를 참고해주세요

https://velog.io/@hoon25/VSCode%EC%99%80-Ubuntu-GDB%EC%97%B0%EA%B2%B0%ED%95%98%EA%B8%B0C-Debug-Make-%EB%B9%8C%EB%93%9C-Debug

profile
더 나은 내일을 위해

4개의 댓글

comment-user-thumbnail
2022년 10월 22일

json 파일 설정하고 upgrade까지만 했는데도 잘 돌아가네요 감사합니다!

답글 달기
comment-user-thumbnail
2022년 10월 22일

좋은 자료 고맙읍니다..

답글 달기
comment-user-thumbnail
2022년 10월 22일

디버거 셋팅 어렵습니다. 옆반 에서 유학을 왔음니다. 당신의 좋은 글 잘 읽고 갑니다.

1개의 답글