segfault in libc.so.6

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



More than likely I'm posting in the wrong place so for that I apologize,
but if so if anybody could point me to any forums/mailing lists that might
be helpfule I would greatly appreciate it.  I'm not a programmer or
developer at all, just trying to help somebody out that is having
problems. He has code that I will post below that is causing a segfault
when run. Ill post the command he is using to compile (which doesnt give
any errors) and the gdb output. If anybody can see anything obviously
wrong with the code we would be grateful. Simple code works just fine.
This has been tried on redhat enterprise 4 both 32 and 64bit with same
results..using gcc-c++-3.4.6-8, not sure what he is using on his 64bit
system. He says the sample code using legal constructs of C++ and pthreads
which I cannot confirm or deny (I really have no clue)

***** CODE *****
#include <netdb.h>
#include <pthread.h>
#include <cassert>
#include <iostream>

void* getHostbyName(void*)
{
std::cout << "From thread" << std::endl;

struct hostent HostBuf;
struct hostent* pHostBuf = 0;
char cTmpBuf[2048] = { 0 };
int nHerr = 0;

std::cout << "B4 calling gethostbyname_r" << std::endl;

int nResult = gethostbyname_r("localhost", &HostBuf, cTmpBuf, 2048,
&pHostBuf,
&nHerr);
assert(0 == nResult);

std::cout << "After calling gethostbyname_r" << std::endl;

return 0;
}

int main()
{
pthread_t Id;

const char * failure;
pthread_attr_t attr;
sched_param sched;
int policy;
int priority_min;
int priority_max;
int min_stack_size = 16384;
int prio = 70;
int stack_size = 16000;

for(;;)
{
if( (policy = sched_getscheduler( 0 )) == -1 )
{
failure = "sched_getscheduler";
break;
}

if( (priority_min = sched_get_priority_min( policy )) == -1 )
{
failure = "sched_get_priority_min";
break;
}

if( prio < priority_min )
prio = priority_min;

if( (priority_max = sched_get_priority_max( policy )) == -1 )
{
failure = "sched_get_priority_max";
break;
}

if( prio > priority_max )
prio = priority_max;

if( sched_getparam( 0, &sched ) != 0 )
{
failure = "sched_getparam";
break;
}

if( pthread_attr_init( &attr ) != 0 )
{
failure = "pthread_attr_init";
break;
}

if( pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ) != 0 ) {
failure = "pthread_attr_setdetachstate";
break;
}

if( pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ) != 0 )
{
failure = "pthread_attr_setscope";
break;
}

if( stack_size < min_stack_size )
stack_size = min_stack_size;

if( pthread_attr_setstacksize( &attr, stack_size ) != 0 )
{
failure = "pthread_attr_setstacksize";
break;
}

if( pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ) != 0 ) {
failure = "pthread_attr_setinheritsched";
break;
}

if( pthread_attr_setschedpolicy( &attr, policy ) != 0 )
{
failure = "pthread_attr_setschedpolicy";
break;
}

sched.sched_priority = prio;

if( pthread_attr_setschedparam( &attr, &sched ) != 0 )
{
failure = "pthread_attr_setschedparam";
break;
}

if( pthread_create( &Id, &attr, getHostbyName, 0 ) != 0 ) //This line
causes crash
//if( pthread_create( &Id, 0, getHostbyName, 0 ) != 0 ) //This line does
not cause crash
{
failure = "pthread_create";
break;
}

pthread_join(Id, 0);
return 0;
}
}


***** COMMAND to COMPILE *****
g++ -v -g -pthread gethostbyname.cc -lpthread

no errors on compile... ./a.out will segfault..


***** GDB ERROR *****
GNU gdb Red Hat Linux (6.3.0.0-1.143.el4rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are welcome to change it and/or distribute copies of it under certain
conditions. Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...Using host
libthread_db library "/lib64/tls/libthread_db.so.1".

Core was generated by `a.out'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /lib64/tls/libpthread.so.0...done.
Loaded symbols for /lib64/tls/libpthread.so.0
Reading symbols from /usr/lib64/libstdc++.so.6...done.
Loaded symbols for /usr/lib64/libstdc++.so.6
Reading symbols from /lib64/tls/libm.so.6...done.
Loaded symbols for /lib64/tls/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/tls/libc.so.6...done.
Loaded symbols for /lib64/tls/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
#0 0x00000034a98d3fed in __res_vinit () from /lib64/tls/libc.so.6
(gdb) where
#0 0x00000034a98d3fed in __res_vinit () from /lib64/tls/libc.so.6
#1 0x00000034a98d772e in __nss_hostname_digits_dots ()
from /lib64/tls/libc.so.6
#2 0x00000034a98dae5b in gethostbyname_r@@GLIBC_2.2.5 ()
from /lib64/tls/libc.so.6
#3 0x0000000000401038 in getHostbyName () at gethostbyname.cc:18
#4 0x00000034aa506137 in start_thread () from /lib64/tls/libpthread.so.0
#5 0x00000034a98c7113 in clone () from /lib64/tls/libc.so.6
(gdb) q

Again, sorry if this is in the wrong place..don't shoot the
messenger...please :)



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux