Segment-fault handling for pthreads


 
Thread Tools Search this Thread
Top Forums Programming Segment-fault handling for pthreads
# 1  
Old 09-13-2012
Segment-fault handling for pthreads

Hi
I have struggling a week to fix a program , in the begining i got SIGBUS , but after many attempts still the program gets SIGSEGV segment fault , In bellow i post the seg fault log + source codes. would really appreciate if experts help me to fix this segment fault error. any advice is highly appreciated.
Thanks in advance


Code:
LL_NODE *ll_prepend(LLIST *l, void *obj)
{
    if (l && obj) {
#line bellow is module-datastruct-llist.c:167 , mentioned in segment-fault log as frame 3
 
      if (!ll_lock(l)) return NULL;
        LL_NODE *new;
        if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
        new->obj = obj;
        new->nxt = l->initial;
        l->initial = new;
        if (!l->last)
            l->last = l->initial;
        l->count++;
        ll_unlock(l);
        return new;
    }
    return NULL;
}

Code:
int32_t ll_lock(LLIST *l)
{
    int32_t res = 1;
   res=cs_trylock(&l->lock);

  #line bellow is module-datastruct-llist.c:51 , mentioned in segment-fault log as frame 2

  while (l && !l->flag && res) {
        cs_debug_mask(D_TRACE, "trylock ll_lock wait");
        cs_sleepms(fast_rnd()%5 + 1);
    }
    return !res;
}

Code:
int32_t cs_trylock(pthread_mutex_t *mutex){

if(!mutex) return -1;
    int32_t result, oldtype;
    /* Make sure that we won't get interrupted while getting the lock */
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

#line bellow is oscam-simples.c:1233 , mentioned in segment-fault log as frame 1

    if((result=pthread_mutex_trylock(mutex)) == 0){

        struct s_client *cl = cs_preparelock(cur_client(), mutex);
        if(cl)
            cl->mutexstore_used++;
    }
    pthread_setcanceltype(oldtype, NULL);
    pthread_testcancel();
    return result;
}

Code:
// in this function &l->lock is initialized   
    LLIST *ll_create()
    {
        LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
        pthread_mutex_init(&l->lock, NULL);
        return l;
    }




Segment fault log :


Code:
Program received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1905]
0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
(gdb) bt
#0  0x2979b7ba in pthread_mutex_trylock () from /lib/libpthread.so.0
#1  0x00410d98 in cs_trylock (mutex=0x247373a4) at oscam-simples.c:1233
#2  0x0043d4aa in ll_lock (l=0x24737398) at module-datastruct-llist.c:51
#3  0x0043d956 in ll_prepend (l=0x24737398, obj=0x4a2410)
    at module-datastruct-llist.c:167
#4  0x0040a66e in get_cw (client=0x4daa80, er=0x5063a0) at oscam.c:2645
#5  0x00439754 in dvbapi_process_input (demux_id=0, filter_num=0, 
    buffer=0x2a98bb60 "\201q=", len=320) at module-dvbapi.c:1634
#6  0x0043c866 in stapi_read_thread (sparam=0x4d1558) at module-dvbapi.c:2441
#7  0x29799486 in ?? () from /lib/libpthread.so.0
Backtrace stopped: frame did not save the PC
(gdb) info args
No symbol table info available.


Last edited by pooyair; 09-13-2012 at 10:14 AM..
# 2  
Old 09-13-2012
The only possibility I see for a problem on that line is:
the
Code:
 pthread_mutex_t *mutex

pointer variable is probably NULL or has been subject to arithmetic change (++ or --), which usually called an off-by-one error, or the mutex variable was never initalized to NULL to start with.

The problem is the pointer.

FWIW:
I do not see where you release the mutex after you call pthread_mutex_trylock and it returns success...
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 09-13-2012
Edit : found it , yes as u said there was null pointer passing. Thanks

Last edited by pooyair; 09-14-2012 at 02:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

2. Programming

Segment fault for C++ program when return vector

I am trying to reverse complement DNA sequence (string) with a short c++ code using boost library. Code was compiled without any warning/error, but ran into Segmentation fault. My guess is the function to return a vector, but not sure. #include <iostream> #include <fstream> #include <string>... (14 Replies)
Discussion started by: yifangt
14 Replies

3. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

4. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Programming

a strange segment fault about ltp-posix test

Hi all In the ltp-posix test,there is a case in open_posix_testsuite\conformance\interfaces\timer_gettime\speculative/6-1.c I run the above code,it will has a segment fault, if I modify it to below,it works well Anybody can tell me why? (1 Reply)
Discussion started by: yanglei_fage
1 Replies

7. HP-UX

pthreads

Hi! I'm linking my hpux code using -lpthread (gcc), yet it references libpthread_tr.1, the debug version of the pthread lib. How do I force it to use pthreads? Thanks, :) (3 Replies)
Discussion started by: zackz
3 Replies

8. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies

9. Programming

pthreads

Does any one no of some good web site which will explain about how to program using pthreads in a UNIX enviroment? (6 Replies)
Discussion started by: fishman2001
6 Replies

10. UNIX for Advanced & Expert Users

PThreads

Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads. Kindly state by an example. (3 Replies)
Discussion started by: S.P.Prasad
3 Replies
Login or Register to Ask a Question