안녕하세요.
QEMU + GDB를 통해서 디버깅 환경을 구축하여 활용하려는 찰나에
GDB 내에서 "p Variable" 로 변수값 출력하려고 하면 <Optimizaed Out> 이라고 출력됩니다.
하여, GCC 최적화 옵션을 제거하려고 -O2 -> -O0, -Og 2가지 방법으로 변경하여 빌드해봤지만 컴파일 에러가 납니다.
구글링 해본 결과 커널에서는 GCC 최적화 옵션 제거가 안되는 것 같습니다.
이럴 경우, 어떻게 컴파일해야 변수 값을 보면서 디버깅이 가능할까요?
알고계신 방법이 있으시면 도와주세요
[환경]
Kernel : v5.1
Arch : arm64
toolchain : linaro/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
QEMU : 4.2.50
댓글 5
-
에러
2019.12.17 19:53
-
문c(문영일)
2019.12.17 20:24
네. 디버깅할 몇 개 함수 앞에 위와 같은 지시어를 추가하고 컴파일한 후
디버깅을 하면 되지만 매우 불편합니다.
예전과 다르게 커널 v4.x 이후부터 Makefile의 -O2 또는 -Os 옵션을 -O0로 바꿔서
빌드하지 못하는 상황이므로 많은 리눅서들이 불편을 느끼고 있는 상황입니다.
혹시 좋은 방법이 있으면 공유하여 주시면 좋겠습니다.
(어느 누군가는 Makefile들을 모두 고치면 된다고도 합니다. 난 못해!!!)
감사합니다.
-
이파란
2021.04.12 19:21
최상단 root Makefile 을 sed 를 사용하여 고치는 방법이 있습니다.
cd linux sed -i 's/-O2/-O0/g' Makefile or xargs -0 sed -i 's/-O2/-Og/g' Makefile
참고:
https://unix.stackexchange.com/questions/606630/build-the-linux-kernel-without-gcc-optimization
-
이파란
2021.04.17 11:50
GCC '-Og' optimization level which is supported since GCC 4.8.
따라서 GCC 에서 권장하는 방법대로 -Og 기준으로 수정하고,루트 Makefile 과 빌드하면서 에러가 발생하는 2가지 파일을 수정했습니다.
v5.x 첫번째 SLTS 최신 릴리즈 v5.10.31 빌드 잘되고, p 값 잘 찍힙니다.
수정한 파일은 아래와 같습니다.
modified: Makefilemodified: include/asm-generic/fixmap.h
modified: include/linux/thread_info.h
과정:
sed -i 's/-O2/-Og/g' Makefile
실행하고
나머지 2가지 파일 수정사항은 아래를 참고하시면 됩니다.
사실 주석 처리한게 전부입니다.
~/linux$ git diff
diff --git a/Makefile b/Makefile
index c4c0b47e6ede..5d3717e60b3e 100644
--- a/Makefile
+++ b/Makefile
@@ -416,11 +416,11 @@ HOSTCXX = g++
endif
export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
- -O2 -fomit-frame-pointer -std=gnu89
+ -Og -fomit-frame-pointer -std=gnu89
export KBUILD_USERLDFLAGS :=
KBUILD_HOSTCFLAGS := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
-KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
+KBUILD_HOSTCXXFLAGS := -Wall -Og $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
@@ -734,7 +734,7 @@ KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
-KBUILD_CFLAGS += -O2
+KBUILD_CFLAGS += -Og
else ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3
KBUILD_CFLAGS += -O3
else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
diff --git a/include/asm-generic/fixmap.h b/include/asm-generic/fixmap.h
index 8cc7b09c1bc7..c1a8b527cd6e 100644
--- a/include/asm-generic/fixmap.h
+++ b/include/asm-generic/fixmap.h
@@ -29,7 +29,7 @@
*/
static __always_inline unsigned long fix_to_virt(const unsigned int idx)
{
- BUILD_BUG_ON(idx >= __end_of_fixed_addresses);
+ // BUILD_BUG_ON(idx >= __end_of_fixed_addresses);
return __fix_to_virt(idx);
}
diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index f3040b0b4b23..0caf20fe01b7 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -150,7 +150,7 @@ static inline void copy_overflow(int size, unsigned long count)
static __always_inline __must_check bool
check_copy_size(const void *addr, size_t bytes, bool is_source)
{
- int sz = __compiletime_object_size(addr);
+ /*int sz = __compiletime_object_size(addr);
if (unlikely(sz >= 0 && sz < bytes)) {
if (!__builtin_constant_p(bytes))
copy_overflow(sz, bytes);
@@ -159,7 +159,7 @@ check_copy_size(const void *addr, size_t bytes, bool is_source)
else
__bad_copy_to();
return false;
- }
+ }*/
if (WARN_ON_ONCE(bytes > INT_MAX))
return false;
check_object_size(addr, bytes, is_source);
해당 이슈 관련하여 인텔 엔지니어가 패치 제출 후에 리누스 토발즈가 거부 했었는데,
결국 2018년 패치를 수용하고 적용했지만,
이후 릴리즈에서 아키텍쳐 별로 업데이트가 잘 안되는 것 같습니다.
참고:
- https://lwn.net/ml/linux-kernel/1525566016-30172-5-git-send-email-changbin.du@intel.com/
- https://stackoverflow.com/questions/60666327/how-to-change-kernel-optimization-level
-
이파란
2022.04.25 12:58
위에 arm64 head.S 분석에 많은 도움이 되었습니다.
리눅스 뿐만 아니라 다른 커널 분석할 때도 요긴합니다!
관련 링크 정리
- https://www.youtube.com/watch?v=RPgYinVQUgw
- http://www.iamroot.org/xe/index.php?mid=Kernel&document_srl=22312
.
문영일 님이 알려주신 방법인데 함수 앞에 __attribute__((optimize("-O0"))) 붙이면 보입니다.