go 디버깅시 빌드/링커 옵션

Cute_Security15·2024년 4월 27일
0

설정

목록 보기
19/22

상황

go 프로그램(containerd, nerdctl) 을 dlv 로 디버깅 하기위해, debuginfo 섹션을 바이너리에 추가해야 한다

목적

필요한 빌드/링커 옵션을 확인하고, 주의사항을 체크

주의사항

go 를 apt-get 으로 설치하는 대신, go 사이트에서 직접 받아서 설치하는걸 추천

dlv 도 apt-get 으로 설치하지 않고, 직접 빌드해서 설치하는걸 추천

dlv 설치

go work init
go work use .
make build
make install   // PATH 에 바이너리 없는 경우 수동으로 이동 ( cp dlv /usr/bin )

빌드, 테스트 도구 버전

go : 1.22.2
delve : v1.22.0

nerdctl 디버그빌드

nerdctl : v1.7.0

gcflags 에 all=-N -l 추가
ldflags 에서 -s -w 둘다 제거

root@user-virtual-machine:~/nerdctl# git diff
diff --git a/Makefile b/Makefile
index 8978d22b..c6a459dc 100644
--- a/Makefile
+++ b/Makefile
@@ -36,9 +36,9 @@ VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.m' --always --tags)
 VERSION_TRIMMED := $(VERSION:v%=%)
 REVISION ?= $(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)

-GO_BUILD_LDFLAGS ?= -s -w
+GO_BUILD_LDFLAGS ?=
 GO_BUILD_FLAGS ?=
-export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 GOOS=$(GOOS) $(GO) build -ldflags "$(GO_BUILD_LDFLAGS) -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"
+export GO_BUILD=GO111MODULE=on CGO_ENABLED=0 GOOS=$(GOOS) $(GO) build -gcflags "all=-N -l" -ldflags "$(GO_BUILD_LDFLAGS) -X $(PACKAGE)/pkg/version.Version=$(VERSION) -X $(PACKAGE)/pkg/version.Revision=$(REVISION)"

 ifdef VERBOSE
        VERBOSE_FLAG := -v
root@user-virtual-machine:~/nerdctl#
root@user-virtual-machine:~/nerdctl# dlv exec _output/nerdctl
Type 'help' for list of commands.
(dlv) 

containerd 디버그빌드

containerd : v1.6.12

export GODEBUG="http2debug" 시 Makefile 에서 알아서 플래그를 세팅해주므로 Makefile 을 건들진 않아도 된다.

  • (http2debug 외에도 go 차원에서 직접적으로 지원하는 여럿 옵션들도 있는듯 하다)
go work init
go work use .
export GODEBUG="http2debug"

//// workspace 정리차원에서 넣은 명령
unset GO111MODULE
unset GOROOT

//// containerd 가 지원하는 snapshotter 중에 btrfs 가 있으므로, btrfs 헤더 설치 필요
apt-get install libbtrfs-dev -y

make
root@user-virtual-machine:~/containerd# dlv exec bin/containerd
Type 'help' for list of commands.
(dlv)
profile
관심분야 : Filesystem, Data structure, user/kernel IPC

1개의 댓글

comment-user-thumbnail
2024년 4월 27일

arr := []string{"apple", "banana", "cherry"}

//// _ : index 는 무시(_) 하고 item 만 사용
for _, item := range arr {
fmt.Println(item)
}

답글 달기