0

I'm doing the chapter in Learn C the Hard Way about Valgrind, but my line numbers aren't showing up despite using -g. I'm running Yosemite 10.10.3 and Valgrind 3.10.1.

My Makefile:

CC = gcc
CFLAGS=-Wall -g -static

FILES = ex1 ex3 ex4

all: $(FILES)

ex1: ex1.c
    $(CC) $(CFlAGS) -o ex1 ex1.c

ex3: ex3.c
    $(CC) $(CFlAGS) -o ex3 ex3.c

ex4: ex4.c
    $(CC) $(CFlAGS) -o ex4 ex4.c

clean:
    $(RM) $(FILES)

and my ex4.c file: (Intentionally broken)

#include <stdio.h>

int main(int argc, char *argv[]){
    int age  = 18;
    int height;

    printf("I am %d years old.\n");
    printf("I am %d inches tall.\n", height);

    return 0;
}

I've looked at this, which is my exact issue through on Ubuntu but the solution isn't working for me. I've also created my .dSYM file. My valgrind output is:

MacBook-Pro:c jaredramirez$ valgrind --dsymutil=yes --track-origins=yes 

./ex4
==649== Memcheck, a memory error detector
==649== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==649== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==649== Command: ./ex4
==649== 
--649-- run: /usr/bin/dsymutil "./ex4"
warning: no debug symbols in executable (-arch x86_64)
==649== Conditional jump or move depends on uninitialised value(s)
==649==    at 0x1003FAC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==649==    by 0x1001EEB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F8FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F2D: main (in ./ex4)
==649==  Uninitialised value was created by a stack allocation
==649==    at 0x1001F4BF8: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649== 
I am 75508496 years old.
==649== Conditional jump or move depends on uninitialised value(s)
==649==    at 0x1001F69F1: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F41: main (in ./ex4)
==649==  Uninitialised value was created by a stack allocation
==649==    at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==649== 
==649== Conditional jump or move depends on uninitialised value(s)
==649==    at 0x1001F6B1B: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F41: main (in ./ex4)
==649==  Uninitialised value was created by a stack allocation
==649==    at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==649== 
==649== Conditional jump or move depends on uninitialised value(s)
==649==    at 0x1001F9555: __ultoa (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F6B5E: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F41: main (in ./ex4)
==649==  Uninitialised value was created by a stack allocation
==649==    at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==649== 
==649== Syscall param write(buf) points to uninitialised byte(s)
==649==    at 0x1002F997A: write$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib)
==649==    by 0x1001F39EC: _swrite (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001EC4A6: __sflush (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001EEC50: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F906C: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F41: main (in ./ex4)
==649==  Address 0x100820385 is 5 bytes inside a block of size 4,096 alloc'd
==649==    at 0x1000084BB: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==649==    by 0x1001EF836: __smakebuf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100204387: __swsetup (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021E75D: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x10021EC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F4B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x1001F29D7: printf (in /usr/lib/system/libsystem_c.dylib)
==649==    by 0x100000F2D: main (in ./ex4)
==649==  Uninitialised value was created by a stack allocation
==649==    at 0x7FFF5FC01036: _dyld_start (in /usr/lib/dyld)
==649== 
I am 0 inches tall.
==649== 
==649== HEAP SUMMARY:
==649==     in use at exit: 38,782 bytes in 425 blocks
==649==   total heap usage: 504 allocs, 79 frees, 44,886 bytes allocated
==649== 
==649== LEAK SUMMARY:
==649==    definitely lost: 0 bytes in 0 blocks
==649==    indirectly lost: 0 bytes in 0 blocks
==649==      possibly lost: 0 bytes in 0 blocks
==649==    still reachable: 0 bytes in 0 blocks
==649==         suppressed: 38,782 bytes in 425 blocks
==649== 
==649== For counts of detected and suppressed errors, rerun with: -v
==649== ERROR SUMMARY: 6 errors from 5 contexts (suppressed: 0 from 0)
Community
  • 1
  • 1
jaredramirez
  • 645
  • 2
  • 11
  • 17
  • 2
    The output from `valgrind` says _`warning: no debug symbols in executable (-arch x86_64)`_ which strongly indicates that there is a problem with the build — of the "did not run the compiler/linker with `-g` option" kind. The alternative is that the debug symbols aren't the ones `valgrind` recognizes. Which version of `valgrind` are you using? Did you build it or did someone else? Which C compiler are you using? – Jonathan Leffler Jun 14 '15 at 22:44
  • And, FWIW, my default compilation options won't let the erroneous code compile. I use `-Werror`, and get 5 warnings/errors about the format error, the unused variable, two unused arguments, and using an uninitialized variable. Granted, this is only a trial program for verification, but … – Jonathan Leffler Jun 14 '15 at 22:49
  • After trying about everything else, I gave up `gcc` and compile now with `clang`. As far as I am concerned, there are only minor differences, but lots more *advantages* (such as a more clear way to indicate errors, and indeed, valgrind complains much less). – Jongware Jun 14 '15 at 22:49
  • 4
    Your makefile defines `CFLAGS` but the compilations use `$(CFlAGS)` (lower-case ell) so the `-g` option isn't used. Is that a glitch in the question or the source of the trouble? – Jonathan Leffler Jun 14 '15 at 22:51
  • 2
    With your code and my version of `valgrind` (3.11.0-SVN, collected from SVN in November 2014), running on Yosemite (10.10.3), using my home-built GCC 5.1.0 and not using `-static` (I don't have the relevant start-up file for the `-lcrt0.o` option), I get line numbers in the error output. I also get them with the current version of XCode (`/usr/bin/gcc --version` says, in part, _`Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.3.0`_.) – Jonathan Leffler Jun 14 '15 at 22:55

1 Answers1

1

Thanks to @JonathanLeffler for helping me solve the issue. After his comment, I changed my Makefile to:

CC = gcc
CFLAGS=-Wall -g -static

FILES = ex1 ex3 ex4

all: $(FILES)

ex1: ex1.c
    $(CC) $(CFLAGS) -o ex1 ex1.c

ex3: ex3.c
    $(CC) $(CFLAGS) -o ex3 ex3.c

ex4: ex4.c
    $(CC) $(CFLAGS) -o ex4 ex4.c

clean:
    $(RM) $(FILES)

and the line numbers are showing up and the warning: no debug symbols in executable (-arch x86_64 error is gone.

jaredramirez
  • 645
  • 2
  • 11
  • 17
  • 3
    Setting CFLAGS as a macro was a good idea; using `$(CFlAGS)` instead of `$(CFLAGS)` was a typo mistake. You'd do better fixing the typo than copying the value of the macro value around the file. Indeed, with the macro set, you wouldn't even need the explicit rules for building each one-file executable and yet `make` would pick up your choice of compiler flags. – Jonathan Leffler Jun 14 '15 at 23:06
  • 1
    @JonathanLeffler thanks! I altered my answer using CFLAGS – jaredramirez Jun 14 '15 at 23:22