#!/usr/bin/perl

# Replace stack frame for a thread created by pthread_create():
#     by 0x........: _thrp_setup (in /lib/libc.so.1)
# with a stack frame expected on Linux:
#     by 0x........: start_thread (pthread_create.c:123)
# It will be further reduced and mangled by the main filter_stderr script.
# This needs to be done first.
s/_thrp_setup \(in .*libc.*\)/start_thread \(pthread_create.c:123\)/g;

# We need to filter out the Solaris libc's stack frame which looks like:
#     by 0x........: pthread_mutex_init (in /...libc...)
# to be consistent with the expected output of test cases.
#
# Stack frames for synchronization functions are missing on Linux because
# of optimalization. 
# 
s/.*\(in \/...libc...\)\R//m;

# We need to replace Solaris threading and sychronization function
# names with POSIX ones for drd_pthread_intercepts.c stack frame:
#     by 0x........: cond_init (drd_pthread_intercepts.c:123)
# See also comments in drd_pthread_intercepts.c.
my %regex = (
    'thr_create'     => 'pthread_create',
    'thr_join'       => 'pthread_join',
    'mutex_init'     => 'pthread_mutex_init',
    'mutex_destroy'  => 'pthread_mutex_destroy',
    'mutex_lock'     => 'pthread_mutex_lock',
    'mutex_trylock'  => 'pthread_mutex_trylock',
    'mutex_unlock'   => 'pthread_mutex_unlock',
    'cond_init'      => 'pthread_cond_init',
    'cond_destroy'   => 'pthread_cond_destroy',
    'cond_wait'      => 'pthread_cond_wait',
    'cond_timedwait' => 'pthread_cond_timedwait',
    'cond_signal'    => 'pthread_cond_signal',
    'cond_broadcast' => 'pthread_cond_broadcast',
    'sema_init'      => 'sem_init',
    'sema_destroy'   => 'sem_destroy',
    'sema_wait'      => 'sem_wait',
    'sema_trywait'   => 'sem_trywait',
    'sema_timedwait' => 'sem_timedwait',
    'sema_post'      => 'sem_post',
    'rwlock_init'    => 'pthread_rwlock_init',
    'rwlock_destroy' => 'pthread_rwlock_destroy',
    'rw_rdlock'      => 'pthread_rwlock_rdlock',
    'rw_wrlock'      => 'pthread_rwlock_wrlock',
    'rw_tryrdlock'   => 'pthread_rwlock_tryrdlock',
    'rw_trywrlock'   => 'pthread_rwlock_trywrlock',
    'rw_unlock'      => 'pthread_rwlock_unlock'
);
my $check = join "|", keys %regex;
if (! /: pthread_/) {
    s/($check)(.*drd_pthread_intercepts.c)/$regex{$1}$2/g;
}
