Recent

Author Topic: [SOLVED] Valgrind, Lazarus and Lion  (Read 12162 times)

caleb9

  • Jr. Member
  • **
  • Posts: 51
[SOLVED] Valgrind, Lazarus and Lion
« on: September 28, 2011, 12:10:42 pm »
Hello.
I've found myself in need to use Valgrind on Lion for my Lazarus project. Unfortunately it doesn't work:
Code: [Select]
$ valgrind --tool=memcheck --leak-check=summary ./project1
valgrind: ./project1: cannot execute binary file
where project1 is just an empty, dummy, default project created by Lazarus. The only things I've changed were Project Options -> Compiler Options / Linking where I toggled 'Generate Debugging Info For GDB' and 'Generate code for valgrind (-gv)', and in Code generation  I've set optimizations to Level 0.

I also tried running it with 'open' which is the way to run programs from the terminal on Mac
Code: [Select]
$ valgrind --tool=memcheck --leak-check=summary --log-file=out.txt open project1.appand it actually executed the program but valgrind died before closing the form (I attached an output file).

I'm on 10.7 Lion, using FPC 2.7.1 from SVN and Lazarus from SVN as well. I installed valgrind from macports (valgrind-devel @3.7.0-r12043 since 3.6.1 doesn't want to install, saying it's not supporting Lion). Here's bunch of info. The project1 executable is
Code: [Select]
$ file project1
project1: Mach-O executable i386

My Mac is
Code: [Select]
$ uname -a
Darwin Mac-mini.local 11.1.0 Darwin Kernel Version 11.1.0: Tue Jul 26 16:07:11 PDT 2011; root:xnu-1699.22.81~1/RELEASE_X86_64 x86_64

FPC
Code: [Select]
$ fpc -version
[0.117] Free Pascal Compiler version 2.7.1 [2011/09/28] for i386
[0.117] Copyright (c) 1993-2011 by Florian Klaempfl and others
[0.117] No source file name in command line
[0.117] Compilation aborted
Error: /usr/local/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)

Lazarus is 0.9.31 r32530

When compiling the empty project, fpc outputs
Code: [Select]
$ fpc  -MObjFPC -Scghi -g -gl -gv  -k-framework -kCarbon -k-framework -kOpenGL -k-dylib_file -k/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib -vewnhi -l -Filib/i386-darwin -Fu/Developer/lazarus/lcl/units/i386-darwin/carbon -Fu/Developer/lazarus/lcl/units/i386-darwin -Fu/Developer/lazarus/packager/units/i386-darwin -Fu. -FUlib/i386-darwin/ -dLCL -dLCLcarbon project1.lpr
Hint: Start of reading config file /etc/fpc.cfg
Hint: End of reading config file /etc/fpc.cfg
Free Pascal Compiler version 2.7.1 [2011/09/28] for i386
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Darwin for i386
Compiling project1.lpr
Assembling (pipe) lib/i386-darwin/project1.s
Compiling resource lib/i386-darwin/project1.or
Linking project1
ld: warning: -macosx_version_min not specificed, assuming 10.7
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in __start from /usr/lib/crt1.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
22 lines compiled, 4.5 sec
2 hint(s) issued

I tried valgrind with simple program in C and it seems to work. If anyone knows what can I do to use it with FPC, I'd be grateful for some hints :].
« Last Edit: September 29, 2011, 10:55:22 am by caleb9 »

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #1 on: September 28, 2011, 01:02:54 pm »
PS. I just installed valgrind-3.6.1 on Snow Leopard 10.6.8 and with FPC-2.4.4 there seems to be the same problem (although I tested it just for empty console program, not for Lazarus project)...

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #2 on: September 28, 2011, 01:10:13 pm »
PS 2
The C executable I successfully run through valgrind is different than the FPC one.
Code: [Select]
$ file a.out
a.out: Mach-O 64-bit executable x86_64
while as I noted earlier, the fpc executables are
Code: [Select]
$ file project1
project1: Mach-O executable i386
if it can be of any relevance.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 670
Re: Valgrind, Lazarus and Lion
« Reply #3 on: September 28, 2011, 01:15:25 pm »
First of all, you must use the -gv command line option when compiling a program for use with Valgrind. When using an FPC 2.4.4-compiled binary you will still get an error (mentioning that Valgrind cannot deal with a custom PAGE_ZERO segment size), but with FPC 2.5.x/2.7.x it should work fine.

I regularly use Valgrind on Mac OS X 10.6 with FPC-compiled programs and I've never seen the error message you are getting. I don't have 10.7.

The "file" command simply tells you that the C binary is a 64 bit one and the FPC binary is a 32 bit one. Valgrind supports both.

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #4 on: September 28, 2011, 01:19:29 pm »
Yup I know, if you look at the compiler options I used for the project (my first post) the -gv is there...

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 670
Re: Valgrind, Lazarus and Lion
« Reply #5 on: September 28, 2011, 01:29:06 pm »
Apparently the people from MacPorts have decided for some reason that their build of Valgrind should only support 64 bit applications (see e.g. http://stackoverflow.com/questions/6082808/run-valgrind-in-32-bit-mode-on-mac-10-6/6303692#6303692)

I use the one from Fink and it works fine. You can also compile Valgrind yourself.

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #6 on: September 28, 2011, 01:36:29 pm »
Yes, compiling the C program with -m32 (32bit executable) causes the same error. Thanks! :] I'll try to compile valgrind myself now and report back.

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 670
Re: Valgrind, Lazarus and Lion
« Reply #7 on: September 28, 2011, 01:40:51 pm »
You can also compile the FPC binary as 64 bit using "fpc -Px86_64". That won't work yet for Lazarus programs though due to its use of Carbon GUI functionality in the default Mac OS X widgetset.

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #8 on: September 28, 2011, 01:45:00 pm »
Great, it works. Installed valgrind straight from SVN. At first I also got the PAGE_ZERO error for unknown reason but after recompiling the executable again it all seems to work fine. I'll skip compiling with -Px86_64 since my main goal is to debug a Lazarus app, so I have to stick to 32 bits for now.
Anyway, solved - thanks for help!

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: [SOLVED] Valgrind, Lazarus and Lion
« Reply #9 on: September 28, 2011, 02:17:53 pm »
Bleh, seems I was happy too soon, though it's a different problem now... Even the simplest Lazarus project crashes valgrind. The output tells me, among others that
Code: [Select]
--17978-- dSYM directory is missing; consider using --dsymutil=yes
but when I used this option I get
Code: [Select]
dyld: could not load inserted library: /usr/local/lib/valgrind/vgpreload_core-x86-darwin.so:(
Do you know if I need to rebuild Lazarus with some specific settings?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 670
Re: Valgrind, Lazarus and Lion
« Reply #10 on: September 28, 2011, 02:33:11 pm »
a) neither of those messages shows an actual crash
b) the dSYM message is unlikely to be related to any crashing. Additionally, if you use that --dsymutil=yes option, you must make sure that you compiled the program with DWARF debug information (-gw). You can also avoid it by telling the compiler to generate a dSYM bundle (-gw -Xg)
c) since you are using the latest svn version of Valgrind, consider that it may simply be buggy and try using a release version instead (and if that crashes, maybe it's simply a Valgrind bug related to Mac OS X 10.7; try the same under 10.6 and see whether that works and if so, file a bug against Valgrind).

One thing you can try is to recompile the LCL with DWARF debug information. Since it's probably compiled using Stabs debug information without -gv, it contains some debug information that Valgrind cannot handle. I don't know whether it would cause it to crash though.

caleb9

  • Jr. Member
  • **
  • Posts: 51
Re: Valgrind, Lazarus and Lion
« Reply #11 on: September 28, 2011, 02:53:11 pm »
Recompiling LCL with these settings doesn't fix it, but I managed to eliminate the dsym- warnings with your suggestions.
The problem is I'm working on OS X 10.7 and the valgrind release (3.6.x) doesn't want to compile on it. But I'll try to see what does it say on 10.6.

Update:
valgrind 3.6.1 works fine on OS X 10.6 indeed, so maybe it is actually a bug in svn version. Anyway the case's closed for now I guess. Thanks again for help jmaebe.

Yet another update:
Seems valgrind-3.7.0 got fixed recently for OS X 10.7 (on SVN) - now I can run it with Lazarus applications seemingly without trouble.
« Last Edit: October 05, 2011, 09:49:21 am by caleb9 »