3

I am having problems getting valgrind to print the stack trace. I am trying to learn to program in 'C' see http://learncodethehardway.org/ section 5.3 p23, running valgrind ./ex4

My system:

Linux version3.6.11+     
gcc4.7.2 
Raspbian Wheezy2013-02-09
valgrind 3.7.0

I used sudo apt-get install valgrind rather than compiling valgrind from source.

Running valgrind ./ex4 on this program

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 }

is supposed to produce the following output ...

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 $

but It doesn't when run on the Raspberry Pi - what it does is this ...

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 I'm a noob) - I gather that it ought to report that I have called a function from main() and that the function contains a mistake (eg an uninitialized variable). Then it should trace the error to the place that function was called in main(). Compilation with the -g option is supposed to give line numbers. However, doing this on Raspberry Pi produces unexpected results, in that the output from valgrind will only print the gibberish at the location of the uninitialised age value. That is it! Indeed the output does not give line numbers, it doesn't report an uninitialised value and there is no stack trace!

There was some speculation about architecture differences on stackoverflow.com in this related post Valgrind not showing line numbers in spite of -g flag (on Ubuntu 11.10/VirtualBox) but I'm not allowed to comment there and I don't know enough to answer the question. Various commenters offered suggestions to try but sadly, running it as

valgrind --track-origins=yes ./ex4

made no difference and using static linking

cc -Bstatic -g -o ex4 ex4.c

does not help either.

So, I contacted the author of, 'learn c the hard way,' Zed Shaw who replied:

"Hmm, I'm not sure but why would you want to learn C on the raspberrypi when you can just learn it on a regular linux box/VM and then after you learn C go use it on raspberrypi. Seems like adding an extra layer of difficulty that isn't necessary."

which is a perfectly reasonable and pragmatic reply but as I said to him, I'm contrary & I want it to work on the Pi because I want to develop on the Pi! So, I also requested help on raspberrypi.org, see http://www.raspberrypi.org/phpBB3/viewtopic.php?f=66&t=7689&p=322853#p322853 Can anyone tell me how to sort this out please? Anyone know anyone with nous++ that I could ask? Thx in advance. Rgds

4

0 回答 0