我在获取valgrind打印堆栈跟踪时遇到问题。我正在尝试学习用'C'编程,参见http://learncodethehardway.org/第5.3节第23页,运行valgrind ./ex4
我的系统:
Linux version3.6.11+
gcc4.7.2
Raspbian Wheezy2013-02-09
valgrind 3.7.0
我使用sudo apt-get install valgrind
而不是从源代码编译valgrind。
正在运行valgrind ./ex4
关于这个计划
1 #include <stdio.h>
2
3 /* Warning: ex4.c - this program is wrong on purpose. */
4
5 int main()
6 {
7 int age = 10;
8 int height;
10 printf("I am %d years old.\n");
11 printf("I am %d inches tall.\n", height);
12
13 return 0;
14 }
应该产生以下输出......
1 $ make ex4
2 cc -Wall -g ex4.c -o ex4
3 ex4.c: In function 'main':
4 ex4.c:10: warning: too few arguments for format
5 ex4.c:7: warning: unused variable 'age'
6 ex4.c:11: warning: 'height' is used uninitialized in this function
7 $ valgrind ./ex4
8 ==3082== Memcheck, a memory error detector
9 ==3082== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
10 ==3082== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
11 ==3082== Command: ./ex4
12 ==3082==
13 I am -16775432 years old.
14 ==3082== Use of uninitialised value of size 8
15 ==3082== at 0x4E730EB: _itoa_word (_itoa.c:195)
16 ==3082== by 0x4E743D8: vfprintf (vfprintf.c:1613)
17 ==3082== by 0x4E7E6F9: printf (printf.c:35)
18 ==3082== by 0x40052B: main (ex4.c:11)
19 ==3082==
20 ==3082== Conditional jump or move depends on uninitialised value(s)
21 ==3082== at 0x4E730F5: _itoa_word (_itoa.c:195)
22 ==3082== by 0x4E743D8: vfprintf (vfprintf.c:1613)
23 ==3082== by 0x4E7E6F9: printf (printf.c:35)
24 ==3082== by 0x40052B: main (ex4.c:11)
25 ==3082==
26 ==3082== Conditional jump or move depends on uninitialised value(s)
27 ==3082== at 0x4E7633B: vfprintf (vfprintf.c:1613)
28 ==3082== by 0x4E7E6F9: printf (printf.c:35)
29 ==3082== by 0x40052B: main (ex4.c:11)
30 ==3082==
31 ==3082== Conditional jump or move depends on uninitialised value(s)
32 ==3082== at 0x4E744C6: vfprintf (vfprintf.c:1613)
33 ==3082== by 0x4E7E6F9: printf (printf.c:35)
34 ==3082== by 0x40052B: main (ex4.c:11)
35 ==3082==
36 I am 0 inches tall.
37 ==3082==
38 ==3082== HEAP SUMMARY:
39 ==3082== in use at exit: 0 bytes in 0 blocks
40 ==3082== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
41 ==3082==
42 ==3082== All heap blocks were freed -- no leaks are possible
43 ==3082==
44 ==3082== For counts of detected and suppressed errors, rerun with: -v
45 ==3082== Use --track-origins=yes to see where uninitialised values come from
46 ==3082== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 4 from 4)
47 $
但是当它在Raspberry Pi上运行时它不会 - 它的作用是......
pmy@pisoft1 ~/cprog $ valgrind --track-origins=yes ./ex4
==14237== Memcheck, a memory error detector
==14237== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==14237== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==14237== Command: ./ex4
==14237==
I am -1111374604 years old.
I am 72 inches tall.
==14237==
==14237== HEAP SUMMARY:
==14237== in use at exit: 0 bytes in 0 blocks
==14237== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==14237==
==14237== All heap blocks were freed -- no leaks are possible
==14237==
==14237== For counts of detected and suppressed errors, rerun with: -v
==14237== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 6)
(nb我是菜鸟) - 我认为它应该报告我从main()调用了一个函数,并且该函数包含一个错误(例如一个未初始化的变量)。然后它应该将错误跟踪到main()中调用函数的位置。使用-g选项编译应该给出行号。但是,在Raspberry Pi上执行此操作会产生意想不到的结果,因为valgrind的输出只会在未初始化的年龄值的位置打印出乱码。这就对了!实际上输出没有给出行号,它没有报告未初始化的值,也没有堆栈跟踪!
在这篇相关文章Valgrind not showing line numbers in spite of -g flag (on Ubuntu 11.10/VirtualBox)中有一些关于stackoverflow.com架构差异的猜测,但我不允许在那里发表评论,我不知道回答这个问题。各种评论者提出了尝试的建议,但遗憾的是,将其作为
运行valgrind --track-origins=yes ./ex4
没有区别并使用静态链接
cc -Bstatic -g -o ex4 ex4.c
也没有帮助。
所以,我联系了作者,'艰难地学习',Zed Shaw回答说:
“嗯,我不确定,但是为什么你要在Rasberrypi上学习C ,当你可以在常规的Linux机器/ VM上学习它然后在你学习C之后使用它在raspberrypi上。似乎增加了额外的难度层,这是不必要的。“
这是一个非常合理和务实的回复,但正如我对他说的那样,我是相反的。我希望它能在Pi上工作,因为我想在Pi上发展!所以,我也请求raspberrypi.org上的帮助,请参阅http://www.raspberrypi.org/phpBB3/viewtopic.php?f=66&t=7689&p=322853#p322853 谁能告诉我如何解决这个问题呢?任何人都知道我可以问的是什么人吗? Thx提前。 RGDS