Languages and Scripting

Re: Segmentation fault while executing an executable file

 
SOLVED
Go to solution
Vishal2k5
Occasional Advisor

Segmentation fault while executing an executable file

I am working on HP_UX Itanium 11.31
There, I have created an executable file by compiling it with gcc 4.4.3.
When I am running that binary file error is coming :Segmentation fault (core dumped)

# file core
core:ELF-64 core file - IA64 from 'hp' - received SIGSEGV

I have tried the sample program but it is also not working at my end
I am working on HP_UX Itanium processor

Sample Code is
--------------------------------------------------------------------------------------------------------
#include
#include
#include

/* This is the program's "main" routine. */
int main (int argc, char *argv[]) {

JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args;
JavaVMOption options[1];

jint res;
jclass cls;
jmethodID mid;

/* IMPORTANT: need to specify vm_args version especially if you are not using JDK1.1.
* Otherwise, will the compiler will revert to using the 'JDK1_1InitArgs' struct.
*/
vm_args.version = JNI_VERSION_1_4;

/* This option doesn't do anything, just to illustrate how to pass args to JVM. */
options[0].optionString = "-verbose:none";
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_FALSE;

/* load and initialize a Java VM, return a JNI interface pointer in env */
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}

jclass ver;
jmethodID print;

ver = (*env)->FindClass(env, "sun/misc/Version");
if (ver == 0) {
fprintf(stderr, "Can't find Version");
}
print = (*env)->GetStaticMethodID(env, ver, "print", "()V");
(*env)->CallStaticVoidMethod(env, ver, print);

/* invoke the Main.test method using the JNI */
cls = (*env)->FindClass(env, "Main");
if (cls == 0) {
fprintf(stderr, "Can't find Main.class\n");
exit(1);
}

mid = (*env)->GetStaticMethodID(env, cls, "test", "(I)V");
if (mid==0) {
fprintf(stderr, "No such method!\n");
exit(1);
}
// otherwise execute this method
(*env)->CallStaticVoidMethod(env, cls, mid,100);

/* We are done. */
(*jvm)->DestroyJavaVM(jvm);

return 0;
}
--------------------------------------------------------------------------------------------------------

It is successfully compiling & generating an exe
To generate exe following script is used

JAVA_HOME=/opt/java6
LIB_PATH=$JAVA_HOME/jre/lib/IA64W
INCLUDE_PATH=$JAVA_HOME/include

$JAVA_HOME/bin/javac Main.java

#Compile the invoker .c file
~/gcc -g -mlp64 -I$INCLUDE_PATH -I$INCLUDE_PATH/hp-ux -L$LIB_PATH -L$LIB_PATH/server -ljava -ljvm -lverify testJNI.c -o testJNI
--------------------------------------------------------------------------------------------------------

public class Main
{
public static void main(String[] args)
{
System.out.println("Argument: " + args[0]);
}

public static void test(int arg)
{
System.out.println("Test Argument: " + arg);
}
}
--------------------------------------------------------------------------------------------------------
export JAVA_HOME=/opt/java6
export LIB_PATH=$JAVA_HOME/jre/lib/IA64W
export LD_LIBRARY_PATH=$LIB_PATH:$LIB_PATH/server
./testJNI
--------------------------------------------------------------------------------------------------------

following is the debugger output

Program received signal SIGSEGV, Segmentation fault
si_code: 2 - SEGV_ACCERR - Invalid Permissions for object.
0x9fffffffed949cd0:0 in ENTER_PTHREAD_LIBRARY_FUNC+0x50 ()
from /usr/lib/hpux64/libpthread.so.1
(gdb) where
#0 0x9fffffffed949cd0:0 in ENTER_PTHREAD_LIBRARY_FUNC+0x50 ()
from /usr/lib/hpux64/libpthread.so.1
#1 0x9fffffffed98e920:0 in pthread_num_processors_np+0xa0 ()
from /usr/lib/hpux64/libpthread.so.1
#2 0x9fffffffee9367e0:0 in os::active_processor_count ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/os/hp-ux/vm/os_hp-ux.cpp:842
#3 0x9fffffffeec1b3f0:0 in os::is_server_class_machine ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/share/vm/runtime/os.cpp:845
#4 0x9fffffffeec1ad60:0 in Arguments::set_ergonomics_flags ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/share/vm/runtime/arguments.cpp:1239
#5 0x9fffffffeec0db60:0 in Arguments::parse ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/share/vm/runtime/arguments.cpp:2673
#6 0x9fffffffeec06cc0:0 in Threads::create_vm ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/share/vm/runtime/thread.cpp:3451
#7 0x9fffffffeec048b0:0 in JNI_CreateJavaVM ()
at /CLO/Components/JAVA_HOTSPOT/Src/src/share/vm/prims/jni.cpp:2674
#8 0x4000000000000f90:0 in main (argc=1, argv=0x9ffffffffffff660)
at testJNI.c:29
(gdb) list
11 JavaVMOption options[1];
12
13 jint res;
14 jclass cls;
15 jmethodID mid;
16
17 /* IMPORTANT: need to specify vm_args version especially if you are not using JDK1.1.
18 * Otherwise, will the compiler will revert to using the 'JDK1_1InitArgs' struct.
19 */
20 vm_args.version = JNI_VERSION_1_4;
--------------------------------------------------------------------------------------------------------
ldd testJNI

libjava.so => /opt/java1.5/jre/lib/IA64W/libjava.so
libjvm.so => /opt/java1.5/jre/lib/IA64W/hotspot/libjvm.so
libverify.so => /opt/java1.5/jre/lib/IA64W/libverify.so
libunwind.so.1 => /lib/hpux64/libunwind.so.1
libc.so.1 => /lib/hpux64/libc.so.1
libverify.so => /opt/java1.5/jre/lib/IA64W/./libverify.so
libdl.so.1 => /usr/lib/hpux64/libdl.so.1
libc.so.1 => /usr/lib/hpux64/libc.so.1
libpthread.so.1 => /usr/lib/hpux64/libpthread.so.1
libdl.so.1 => /usr/lib/hpux64/libdl.so.1
libm.so.1 => /usr/lib/hpux64/libm.so.1
librt.so.1 => /usr/lib/hpux64/librt.so.1
libuca.so.1 => /usr/lib/hpux64/libuca.so.1
libunwind.so.1 => /usr/lib/hpux64/libunwind.so.1
libstd_v2.so.1 => /usr/lib/hpux64/libstd_v2.so.1
libcl.so.1 => /usr/lib/hpux64/libcl.so.1
libCsup.so.1 => /usr/lib/hpux64/libCsup.so.1
libuca.so.1 => /usr/lib/hpux64/libuca.so.1
libdl.so.1 => /usr/lib/hpux64/libdl.so.1
libIO77.so.1 => /usr/lib/hpux64/libIO77.so.1
libunwind.so.1 => /usr/lib/hpux64/libunwind.so.1
--------------------------------------------------------------------------------------------------------
Can u pleas help me out in finding the solution
8 REPLIES 8
Laurent Menase
Honored Contributor

Re: Segmentation fault while executing an executable file

What about -mt option on compilation line?
Laurent Menase
Honored Contributor

Re: Segmentation fault while executing an executable file

forget it, since we see libpthread.so in the ldd output
Vishal2k5
Occasional Advisor

Re: Segmentation fault while executing an executable file

Hi Laurent,
Can you please help me out in finding the solution
Dennis Handly
Acclaimed Contributor

Re: Segmentation fault while executing an executable file

#0 0x9fffffffed949cd0:0 in ENTER_PTHREAD_LIBRARY_FUNC+0x50
libpthread.so.1
#1 0x9fffffffed98e920:0 in pthread_num_processors_np+0xa0
#2 0x9fffffffee9367e0:0 in os::active_processor_count

This indicates the stack is so corrupted that libpthread aborts.

You probably need to contact the Response Center for help with your java JNI.

> Laurent: What about -mt option on compilation line?

Yes, that or the gcc equivalent may help.
Laurent Menase
Honored Contributor
Solution

Re: Segmentation fault while executing an executable file

Do you read Japanese ?
I found a similar quuestion :

http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=45298&forum=12


looks like it was solved by adding -lpthread

so add -mt ot -lpthread, it may help
Vishal2k5
Occasional Advisor

Re: Segmentation fault while executing an executable file

Denis,
Can u please tell me what does it mean "stack is so corrupted"
I am not from assembly background. I am new on HP-UX as well. This code is working on all other OS (32 as well as 64bit) including HP_UX PA_RISC2.0,AIX Solaris, Windows, linux

(gdb) info registers
pr0: 0x1
pr1: 0x1
pr2: 0
pr3: 0x1
pr4: 0
pr5: 0
pr6: 0
pr7: 0
pr8: 0x1
pr9: 0
pr10: 0
pr11: 0
pr12: 0x1
pr13: 0
pr14: 0
pr15: 0x1
pr16: 0
pr17: 0
pr18: 0
pr19: 0
pr20: 0
pr21: 0
pr22: 0
---Type to continue, or q to quit---
pr23: 0
pr24: 0
pr25: 0
pr26: 0
pr27: 0
pr28: 0
pr29: 0
pr30: 0
pr31: 0
pr32: 0
pr33: 0
pr34: 0
pr35: 0
pr36: 0
pr37: 0
pr38: 0
pr39: 0
pr40: 0
pr41: 0
pr42: 0
pr43: 0
pr44: 0
pr45: 0
---Type to continue, or q to quit---
pr46: 0
pr47: 0
pr48: 0
pr49: 0
pr50: 0
pr51: 0
pr52: 0
pr53: 0
pr54: 0
pr55: 0
pr56: 0
pr57: 0
pr58: 0
pr59: 0
pr60: 0
pr61: 0
pr62: 0
pr63: 0
gr0: 0
gr1: 0x9ffffffffeda2ef0
gr2: 0x9fffffff7f7e7c00
gr3: 0x9fffffff7f7e7c00
gr4: 0
---Type to continue, or q to quit---
gr5: 0xc000000000000408
gr6: 0x9ffffffffeedea20
gr7: 0x9ffffffffee9ae48
gr8: 0x9ffffffffeda29c0
gr9: 0x9ffffffffcb4ed48
gr10: 0
gr11: 0xc000000000000207
gr12: 0x9fffffffffffebe0
gr13: 0x9ffffffffcb4e080
gr14: 0x9ffffffffeda2ef0
gr15: 0x1a
gr16: 0xc000000000000008
gr17: 0x1f
gr18: 0
gr19: 0x9ffffffffef354c8
gr20: 0x9fffffffffffebe0
gr21: 0
gr22: 0x9fffffff7f7e8000
gr23: 0x60
gr24: 0
gr25: 0x9ffffffffed318d0
gr26: 0
gr27: 0
---Type to continue, or q to quit---
gr28: 0
gr29: 0
gr30: 0x9ffffffffebb3de0
gr31: 0x710
gr32: 0x9ffffffffeda2ef0
gr33: 0xc000000000000207
gr34: 0x9ffffffffd186920
gr35: 0x5a
gr36: 0x2c
gr37: 0x9ffffffffeda2c50
gr38: 0x58
gr39: 0
gr40: 0
gr41: 0
gr42: 0
gr43: 0
br0: 0x9ffffffffd186920
br1: 0x9ffffffffebb3de0
br2: 0
br3: 0
br4: 0
br5: 0
br6: 0x9ffffffffed318d0
---Type to continue, or q to quit---
br7: 0xe0000001084fc720
rsc: 0x1f
bsp: 0x9ffffffffef354c8
bspst: 0x9ffffffffef35528
rnat: 0
ccv: 0
unat: 0
fpsr: 0x9804c8274433f
pfs: 0xc000000000000207
(sor:0, sol:4, sof:7)
lc: 0
ec: 0
ip: 0x9ffffffffd141cd0:0
cfm: 0x48c
(sor:0, sol:9, sof:12)
psr: 0x10130862e01a
(gdb)
Vishal2k5
Occasional Advisor

Re: Segmentation fault while executing an executable file

Hi Laurent Menase,

Thanks a ton. It worked.

While compiling with gcc, setting -lpthread parameter solved the problem

Thanks again
Dennis Handly
Acclaimed Contributor

Re: Segmentation fault while executing an executable file

>(gdb) info registers

This is not so helpful unless you have the instructions around PC to go with it:
disas $pc-16*8 $pc+16*8