localtime_r() SEGV

localtime_r() SEGV

Post by Hyeonkuk Jeon » Fri, 05 Nov 1999 04:00:00

In Solaris 7 with gcc 2.95.1, I wrote a function to log error messages with
timestamp. I use time() and localtime_t() just like this.

void ErrorLog(char *s)
    time_t        t;
    struct tm    tm;

    localtime_r(&t, &tm);
    fprintf(stderr, "%04d%02d%02d%02d%02d%02d %s",
        tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, tm.tm_hour, tm.tm_min,
        tm.tm_sec, s);


And when I call this function from the start of thread routine created by
pthread_create(), localtime_r() generates SEGV and the thread stops. This
happens once in a while, not always.

signal fault in critical section
signal number: 11, signal code: 1,              fault address: 0xfe7d1e20,
pc: 0xff10c548, sp: 0xff0113e0
libthread panic: fault in libthread critical section (PID: 6182 LWP 4)
    ff10c52c    // <_mutex_adaptive_lock+132>:  call  0xff11ba7c
    ff10c378    // <_cmutex_lock+80>:   call  0xff10c4a8
    ff1d32ec    //  <localtime_r+24>:    call  0xff234858
    80784        // <ErrorLog+60>:        call  0xde188 <localtime_r>
    27e8c        // Calling ErrorLog()

I ran gdb to see what caused SEGV and found these.

(gdb) where
#0  0xff2145fc in _libc_sigtimedwait () from /usr/lib/libc.so.1
#1  0xff10db54 in _panic () from /usr/lib/libthread.so.1
#2  <signal handler called>
#3  0xff10a320 in _co_timerset () from /usr/lib/libthread.so.1
#4  0xff10c380 in _cmutex_lock () from /usr/lib/libthread.so.1
#5  0xff1d32f4 in localtime_r () from /usr/lib/libc.so.1
#6  0x8078c in ErrorLog (s=0xc6110 "Watchdog thread start\n") at

(gdb) disassemble 0xff10c548
0xff10c52c <_mutex_adaptive_lock+132>:  call  0xff11ba7c <_lock_owner>
0xff10c530 <_mutex_adaptive_lock+136>:  mov  %i0, %o0
0xff10c534 <_mutex_adaptive_lock+140>:  cmp  %l0, 0x64
0xff10c538 <_mutex_adaptive_lock+144>:
    bge  0xff10c590 <_mutex_adaptive_lock+232>
0xff10c53c <_mutex_adaptive_lock+148>:  cmp  %o0, 0
0xff10c540 <_mutex_adaptive_lock+152>:
    be,a   0xff10c594 <_mutex_adaptive_lock+236>
0xff10c544 <_mutex_adaptive_lock+156>:  ldub  [ %i0 + 0xf ], %l0
0xff10c548 <_mutex_adaptive_lock+160>:  ldsb  [ %o0 + 0x58 ], %o1    <-- pc
points here
0xff10c54c <_mutex_adaptive_lock+164>:  cmp  %o1, 4
0xff10c550 <_mutex_adaptive_lock+168>:
    bne,a   0xff10c594 <_mutex_adaptive_lock+236>

and %o0 is 0xb(11) at "#3  0xff10a320 in _co_timerset () from

I think localtime_r() uses mutex object to make it as multithread-safe and
first time a process calls localtime_r(), it creates a mutex object and
locks/unlocks it everytime localtime_r() is called. This problem occurs only
by first localtime_r() call in a process. Maybe when localtime_r() creates a
mutex object, it doesn't initialize the mutex object I think.

If anyone had this kind of problem before or have any solution for this,

Thanks in advance.


1. localtime(), localtime_r()

Hello, I seem to be having problems converting a time_t to a tm structure in
local time when i'm in a thread.  When i call localtime() or localtime_r()
in my main(), it converts it correctly, but when i'm inside a thread, it
doesn't convert the time to local, it remains in GMT time.  Does anybody
know anything about this, i'm using Tru64 Unix 5, using the cxx compiler..

2. Solaris x86 v2.4 - $75

3. localtime_r, gmtime_r, strftime bugs

4. Userdata in a Linux network?

5. memory leak in solaris2.3 localtime_r(3)

6. Redirect...How?

7. localtime_r() problem with thread.

8. CD-ROM bootable SCSI

9. localtime_r

10. localtime_r(), formatting wrong hour

11. localtime_r seg.faults

12. Why is localtime_r() not ASYNC-SIGNAL-safe ?

13. 2.3 memory leak in localtime_r?