如何寻找 "to the invalid address stated on the next line" 错误
How to hunt a "to the invalid address stated on the next line" bug
我正在制作一个代码太多的库,无法在此处提供。
我的问题是分段错误,Valgrind 分析为:
Jump to the invalid address stated on the next line
at 0x72612F656D6F682F: ???
at [...] (stack call)
感谢this question,我猜是因为我的某个地方有堆栈损坏。
我的问题是:如何找到它?
我尝试使用 GDB,但分段错误似乎不在同一个地方。 GDB 告诉我它在函数的第一行,而 Valgrind 告诉它是这个函数的调用造成了分段错误。
如果问题是可重复的,您可以使用类似于 this answer 的技术在 return 地址的位置设置一个观察点,并让 GDB 停止在紧跟在损坏指令之后的指令上它。
由于这是多年前的事,您可能已经找到了您的错误。但是对于可能偶然发现此问题的任何人,我强烈建议您查看 "sanitizers".
如果您运行正在使用 Memcheck,您可能 运行 AddressSanitizer, which exists in both clang and gcc。 AddressSanitizer 通常可以比 Memcheck 更好地检测堆栈损坏问题。 (除了堆栈损坏,AddressSanitizer 还可以检测许多不同类型的寻址错误)。
但是,如果您在 Memcheck 日志中向后滚动,您可能会看到 Conditional jump or move depends on uninitialised value(s)
,在这种情况下,您使用的是未初始化的变量,这通常更难调试。为此,您可以尝试 MemorySanitizer (currently clang and Linux only, https://clang.llvm.org/docs/MemorySanitizer.html). In particular, look at the origin tracking 选项。对于未初始化变量的使用,这提供了比 Memcheck 更好的来源跟踪。但是请注意,MemorySanitizer 的设置并非易事,因为它通常需要使用 (MemorySanitizer) 工具构建所有外部库。
我正在制作一个代码太多的库,无法在此处提供。
我的问题是分段错误,Valgrind 分析为:
Jump to the invalid address stated on the next line
at 0x72612F656D6F682F: ???
at [...] (stack call)
感谢this question,我猜是因为我的某个地方有堆栈损坏。
我的问题是:如何找到它?
我尝试使用 GDB,但分段错误似乎不在同一个地方。 GDB 告诉我它在函数的第一行,而 Valgrind 告诉它是这个函数的调用造成了分段错误。
如果问题是可重复的,您可以使用类似于 this answer 的技术在 return 地址的位置设置一个观察点,并让 GDB 停止在紧跟在损坏指令之后的指令上它。
由于这是多年前的事,您可能已经找到了您的错误。但是对于可能偶然发现此问题的任何人,我强烈建议您查看 "sanitizers".
如果您运行正在使用 Memcheck,您可能 运行 AddressSanitizer, which exists in both clang and gcc。 AddressSanitizer 通常可以比 Memcheck 更好地检测堆栈损坏问题。 (除了堆栈损坏,AddressSanitizer 还可以检测许多不同类型的寻址错误)。
但是,如果您在 Memcheck 日志中向后滚动,您可能会看到 Conditional jump or move depends on uninitialised value(s)
,在这种情况下,您使用的是未初始化的变量,这通常更难调试。为此,您可以尝试 MemorySanitizer (currently clang and Linux only, https://clang.llvm.org/docs/MemorySanitizer.html). In particular, look at the origin tracking 选项。对于未初始化变量的使用,这提供了比 Memcheck 更好的来源跟踪。但是请注意,MemorySanitizer 的设置并非易事,因为它通常需要使用 (MemorySanitizer) 工具构建所有外部库。