I have been hacking with cprof on debian stable (god
knows what version) and came accross an issue
whereby the cprof MENTER (i.e. __menter_internal)
function was crashing the first time it was called, when
linked against a particular program I am trying to profile
:-).
In debugging the problem I discovered that the cprof
code was calling pthread_getspecific(prof_key) before
calling pthread_key_create(&prof_key, NULL), which
(according to the pthread manpages I have) results in
undefined behaviour. It seems under some circumstances
this early call to pthread_getspecific(prof_key) will return
NULL, and at other times (for reasons I do not know) it
will return some random value that is not NULL.
For my purposes I solved the problem by changing the
function get_thread_prof in lib/profile.c as follows -
static INLINE1 struct thread_prof *get_thread_prof(void)
{
struct thread_prof *thread = NULL;
prof_init(); // must call pthread_create_key()
// before pthread_getspecific()
#ifdef WINE_SUPPORT
thread = get_teb()->prof_thread;
#else
thread = pthread_getspecific(prof_key);
#endif
if (!thread) thread = prof_thread_init();
return thread;
}