STRCAT in C code crashing JVM, debug/logging how-to help needed

Hi Guys

I'm very inexperienced in debugging JNI/native lib, helps are very much appreicated. I've written C code to use a native library (.so) and also generate Java exception using the code below when making native method call via JNI, everything was running ok.

However, I sometimes get problem at STRCAT method in throwExceptionWithConvDump ( see below) and I don't how to debug such thing, and also I can not reproduce this exception as most of the time everything works perfectly normal. Please help me out if you know C code and tell me what have I done wrong or what should I do to make my useage of STRCAT more error proof. Or if you know how should I debug this issue. Thank you verymuch for helping.

Env:

Java 1.5

Tomcat 5

Log4J

Note: The XXX is the package that I don't have source code.

*

* Throwing a Java Exception

*

*/

void throwJavaException(JNIEnv *env,char *errorMsg,char *errorClass)

{

jclass newExcCls;

(*env)->ExceptionDescribe(env);

(*env)->ExceptionClear(env);

newExcCls = (*env)->FindClass(env, errorClass);

if (newExcCls == NULL)

newExcCls = (*env)->FindClass(env,"com/XXX/CAPIException");

if (newExcCls == NULL){

printf("in throwJavaException() : can't find com/XXX/CAPIException");

return;

}else{

printf("found com/XXX/CAPIException!! ");

}

(*env)->ThrowNew(env, newExcCls, errorMsg);

}

/*

* Throwing a Java Exception with error code and conversation dump

*

*/

void throwExceptionWithConvDump(JNIEnv *env, XXXErr XXX_err,char *convDump,char *errorClass)

{

char errNum[2];

sprintf(errNum,"%i", (int)cai_err);

char *errorMsg;

STRCPY(errorMsg,"\n XXX APIV Error: ErrorCode = $");

STRCAT(errorMsg,errNum);

STRCAT(errorMsg,"$ ");

STRCAT(errorMsg,XXX_getErrorStr(cai_err) );

STRCAT(errorMsg,"\n Conversation Dump : \n\n" );

STRCAT(errorMsg, convDump );

throwJavaException(env, errorMsg, errorClass);

}

Error :

# An unexpected error has been detected by HotSpot Virtual Machine:

#

# SIGSEGV (0xb) at pc=0xb75118b3, pid=31052, tid=2617514928

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_09-b01 mixed mode)

# Problematic frame:

# C [libc.so.6+0x768b3] strcat+0x53

#

T H R E A D

...

...

Stack: [0x9bfc1000,0x9c042000), sp=0x9c041220, free space=512k

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)

C [libc.so.6+0x768b3] strcat+0x53

C [libAmadeusProxy.so+0x904cf4] Java_com_XXX_sendXmlQuery+0xda

...

...

..

..

v ~OSRAdapter

v ~StubRoutines::call_stub

V [libjvm.so+0x17a75c]

V [libjvm.so+0x28afd8]

V [libjvm.so+0x179fb5]

V [libjvm.so+0x17a04e]

V [libjvm.so+0x1f1785]

V [libjvm.so+0x2f43d3]

V [libjvm.so+0x28bbe8]

C [libpthread.so.0+0x4dac]

[3961 byte] By [JerryTramadaa] at [2007-11-26 22:52:15]
# 1

> Hi Guys

>

> I'm very inexperienced in debugging JNI/native lib,

> helps are very much appreicated. I've written C code

> to use a native library (.so) and also generate Java

> exception using the code below when making native

> method call via JNI, everything was running ok.

>

> However, I sometimes get problem at STRCAT method in

> throwExceptionWithConvDump ( see below) and I don't

> how to debug such thing, and also I can not reproduce

> this exception as most of the time everything works

> perfectly normal. Please help me out if you know C

> code and tell me what have I done wrong or what

> should I do to make my useage of STRCAT more error

> proof. Or if you know how should I debug this issue.

> Thank you verymuch for helping.

For starters, check that 'convDump' and 'errorClass' aren't null.

Jim S.

Niceguy1a at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Another likely source of problem: Area being strcat'd into is too short.
bschauwejavaa at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Well, you should always think about memory while using C/C++!

For example this code is very bad (even you sure cai_err always 0 to 9):

char errNum[2];

sprintf(errNum, "%i", (int)cai_err);

BTW what is STRCPY and STRCAT? Your own functions or you use special library? Or you just wrote them uppercase while use standard strcpy and strcat in your code?

Michael.Nazarov@sun.coma at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Hi

your Problem is that you copy to non existent memory.

char *errorMsg;

STRCPY(errorMsg,"\n XXX APIV Error: ErrorCode = $");

errorMsg is just a pointer on the stack and it is pointing to the certain death of your application. You are lucky that you get a crash on the STRCAT.

Use something like

char *errorMsg = new char[MAX_MSG_SIZE];

STRCPY(...)

...

delete[] errorMsg;

do get some memory on the heap, but dont't forget the delete after you are done with the errorMsg.

Michael

mic1177a at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5
>your Problem is that you copy to non existent memory.Who said? Possibly STRCPY is custom function to allocate memory then strcpy. But OP should answer and I think he will not...
Michael.Nazarov@sun.coma at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

sorry for the late reply, the project was off and on again so the problem still exist and have to be resolved.

as for the STRCPY and STRCAT below are how it is defined.

#define STRCPY(d,s) d=(char *)malloc(strlen(s)+1);strcpy(d,s)

#define STRCAT(d,s) d=(char *)realloc(d,strlen(d)+strlen(s)+1);strcat(d,s)

JerryTramadaa at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7
Reread my reply #3 and think about code I named "bad".
Michael.Nazarov@sun.coma at 2007-7-10 12:14:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...