Valgrind Support for Mono

by Miguel de Icaza

During hack week, I took an afternoon to add Valgrind support for Mono symbols. It was kind of a hackternoon thing.

Mono works great with Valgrind, but when there is an error in unmanaged code stack traces only contain symbols from the native libraries and do not contain information from the JITed code. During the Moonlight hacking sprint we used Valgrind extensively for finding errors in our code and it was becoming annoying to manually lookup addresses from stack traces and match them up with Mono's -v output. Today the output looks like this:


==22441== Mismatched free() / delete / delete []
==22441==    at 0x4020E26: operator delete(void*) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==22441==    by 0x5EB49FA: Value::FreeValue() (value.cpp:261)
==22441==    by 0x5EB4AAC: value_free_value (value.cpp:275)
==22441==    by 0x66E5E60: ???
==22441==    by 0x66E4587: ???
==22441==    by 0x66E3FF1: ???
==22441==    by 0x66E3DE5: ???
==22441==    by 0x66E3D35: ???
==22441==    by 0x809D294: mono_runtime_class_init (object.c:329)
==22441==    by 0x815920C: mono_jit_compile_method (mini.c:10905)
==22441==    by 0x81595C4: mono_jit_runtime_invoke (mini.c:11081)
==22441==    by 0x809FD34: mono_runtime_invoke_array (object.c:2616)

	

This app is the culrpit for the above stacktrace. A developer first reaction to the "???" text is to panic.
Today we lower the panic alert level.

At the beginning of the hackternoon, I did not know it, but Nat had written a script that achieved similar results:

I added a new client API to Valgrind that JIT compilers can use to register generated code with Valgrind so that the actual method name is displayed on the stack traces, the new output looks like this for the same error:

==22478== Mismatched free() / delete / delete []
==22478==    at 0x4020E26: operator delete(void*) (vg_replace_malloc.c:244)
==22478==    by 0x5EB49FA: Value::FreeValue() (value.cpp:261)
==22478==    by 0x5EB4AAC: value_free_value (value.cpp:275)
==22478==    by 0x66E5E60: (wrapper managed-to-native) Mono.NativeMethods:value_free_value (Mono.Value&)
==22478==    by 0x66E4587: System.Windows.DependencyObject:SetValue (System.Windows.DependencyProperty,System.Windows.Media.Color)
==22478==    by 0x66E3FF1: System.Windows.Media.SolidColorBrush:.ctor (System.Windows.Media.Color)
==22478==    by 0x66E3DE5: Desklets.Monitor:.cctor ()
==22478==    by 0x66E3D35: (wrapper runtime-invoke) Desklets.Monitor:runtime_invoke_void (object,intptr,intptr,intptr)
==22478==    by 0x809D294: mono_runtime_class_init (object.c:329)
==22478==    by 0x815920C: mono_jit_compile_method (mini.c:10905)
==22478==    by 0x81595C4: mono_jit_runtime_invoke (mini.c:11081)
==22478==    by 0x809FD34: mono_runtime_invoke_array (object.c:2616)
	

The patch is here.

Support your Valgrind/Mono addiction by voting my idea up.

Posted on 29 Jun 2007