Brian Jones <[EMAIL PROTECTED]> writes:

> I've created a small patch to japhar with which I've tried to make
> Japhar support printStackTrace (PrintStream) and printStackTrace
> (PrintWriter) correctly.  Currently it always prints to stderr.

I tracked down the problem finally to an incorrect method signature in
my code.  I've updated the patch but I don't want to check it into
Japhar without review.  Could someone else take a look and let me know
if there are problems with this?  I'm mostly concerned about how I've
had to assume the stream class is either PrintStream or PrintWriter
rather than just being able to ask for a println method on whatever
object I'm passed.  Then there is the question of memory usage and
whether or not I've created any sort of leak.  Finally should I do
anything in particular to try to prevent throwing an exception while
printing an exception's backtrace?

I did remove the part of printbacktrace which redundantly prints the
name of the class and the exception message.  This makes Japhar's
output look much more similar to Sun's JDK when using the JDK
classes.zip.

Next to do... make classpath a little more debuggable thanks to
stacktraces... can't hardly wait.  After taking a look at Kaffe 1.0.5
I believe we should just make punting to a native routine implemented
by the VM the norm rather than trying to put the frame representation,
etc. into Classpath and diddling with the output ourselves.  That
should at least make that part of the Kaffe port similar to and as
easy as the Japhar port.

Brian
-- 
Brian Jones <[EMAIL PROTECTED]>

Index: include/runtime.h
===================================================================
RCS file: /cvsroot/hungry/java/japhar/include/runtime.h,v
retrieving revision 1.11
diff -r1.11 runtime.h
430a431,434
> HVM_ExceptionPrintBacktraceToStream(HungryEnv *henv, japhar_object* throwable_ref, 
>                             japhar_object* stream);
> 
> PR_EXTERN( void )
Index: lib/libnative/java.lang/throwable.c
===================================================================
RCS file: /cvsroot/hungry/java/japhar/lib/libnative/java.lang/throwable.c,v
retrieving revision 1.19
diff -r1.19 throwable.c
38c38
<   HVM_ExceptionPrintBacktrace(henv, throwable);
---
>   HVM_ExceptionPrintBacktraceToStream(henv, throwable, printwriter_stream);
Index: lib/libruntime/exceptions.c
===================================================================
RCS file: /cvsroot/hungry/java/japhar/lib/libruntime/exceptions.c,v
retrieving revision 1.57
diff -r1.57 exceptions.c
66,67c66,73
<   ClazzFile *throwable_class;
<   MethodStruct *getMessage;
---
>   HVM_ExceptionPrintBacktraceToStream(henv, throwable_ref, (japhar_object*)NULL);
> }
> 
> PR_IMPLEMENT(void)
> HVM_ExceptionPrintBacktraceToStream(HungryEnv *henv, japhar_object* throwable_ref, 
>japhar_object* stream_ref)
> {
>   ClazzFile *throwable_class, *stream_class;
>   MethodStruct *getMessage, *println = NULL;
74a81,82
>   char *msg_to_print;
>   japhar_object *msg_obj;
76,96c84
<   throwable_class = HVM_ClassFind(henv, java_lang_Throwable);
< 
<   getMessage = HVM_MethodFind(henv, throwable_class,
<                               "getMessage",
<                               "()Ljava/lang/String;");
< 
<   /*
<    * Cache exception and make sure the runtime don't think the call to
<    * getMessage failed
<    */
<   exception_cache = henv->_exception;
<   henv->_exception = NULL;
< 
<   msg_value = HVM_MethodCallA(henv, getMessage, throwable_ref, NULL);
<   msg = msg_value.l;
< 
<   /* Don't know what to do if the call fails.  Die a horrible death? */
<   PR_ASSERT(NULL == henv->_exception);
<   henv->_exception = exception_cache;
< 
<   if (msg)
---
>   if (stream_ref != NULL) 
98c86,91
<       const char *msg_bytes = HVM_StringToCString(henv, msg);
---
>       ClazzFile *stream_clazz = HVM_ClassFind(henv, "java/io/PrintWriter");
>       PRBool pwriter = HVM_ObjectIsInstanceOf(henv, stream_ref, stream_clazz);
>       if (pwriter) 
>         stream_class = HVM_ClassFind(henv, "java/io/PrintWriter");
>       else
>         stream_class = HVM_ClassFind(henv, "java/io/PrintStream");
100c93,104
<       fprintf (stderr, "%s (%s)\n", exceptionname, msg_bytes);
---
>       if (stream_class == NULL)
>         {
>           if (pwriter)
>             abort_with_message("ExceptionPrintBacktraceToStream could not find 
>java/io/PrintWriter");
>           else
>             abort_with_message("ExceptionPrintBacktraceToStream could not find 
>java/io/PrintStream");
>         }
>       
>       println = HVM_MethodFind(henv, stream_class, "println", 
>                              "(Ljava/lang/String;)V");
>       if (println == NULL)
>         abort_with_message("ExceptionPrintBacktraceToStream could not find method 
>println");
102,103d105
<   else
<     fprintf (stderr, "%s\n", exceptionname);
112,116c114,132
<         fprintf (stderr, "        in %s.%s(%s%snative method)\n",
<                  level->classname,
<                  level->method->name,
<                  level->filename ? level->filename : "",
<                  level->filename ? ", " : "");
---
>         {
>           msg_to_print = PR_smprintf("        in %s.%s(%s%snative method)", 
>                                    level->classname, 
>                                    level->method->name, 
>                                      level->filename ? level->filename : "", 
>                                      level->filename ? ", " : "");
>           msg_obj = HVM_StringFromCString(henv, msg_to_print);
>           if (msg_obj == NULL)
>             abort_with_message("ExceptionPrintBacktraceToStream unable to allocate 
>message");
> 
>           if (println != NULL)
>             {
>               HVM_MethodCall(henv, println, stream_ref, msg_obj);
>             }
>           else
>             fprintf (stderr, "%s\n", msg_to_print);
> 
>           PR_smprintf_free(msg_to_print);
>         }
118,123c134,153
<         fprintf (stderr, "        in %s.%s(%s%spc = %d)\n",
<                  level->classname,
<                  level->method->name,
<                  level->filename ? level->filename : "",
<                  level->filename ? ", " : "",
<                  level->pc);
---
>         {
>           msg_to_print = PR_smprintf("        in %s.%s(%s%spc = %d)",
>                                      level->classname,
>                                      level->method->name,
>                                      level->filename ? level->filename : "",
>                                      level->filename ? ", " : "",
>                                      level->pc);
>           msg_obj = HVM_StringFromCString(henv, msg_to_print);
>           if (msg_obj == NULL)
>             abort_with_message("ExceptionPrintBacktraceToStream unable to allocate 
>message");
> 
>           if (println != NULL)
>             {
>               HVM_MethodCall(henv, println, stream_ref, msg_obj);
>             }
>           else
>             fprintf (stderr, "%s\n", msg_to_print);
> 
>           PR_smprintf_free(msg_to_print);
>         }
125,131c155,175
<         fprintf (stderr, "        at %s.%s(%s%s%d, pc = %d)\n",
<                  level->classname,
<                  level->method->name,
<                  level->filename ? level->filename : "",
<                  level->filename ? ":" : "line ",
<                  line_number,
<                  level->pc);
---
>         {
>           msg_to_print = PR_smprintf("        at %s.%s(%s%s%d, pc = %d)",
>                                      level->classname,
>                                      level->method->name,
>                                      level->filename ? level->filename : "",
>                                      level->filename ? ":" : "line ",
>                                      line_number,
>                                      level->pc);
>           msg_obj = HVM_StringFromCString(henv, msg_to_print);
>           if (msg_obj == NULL)
>             abort_with_message("ExceptionPrintBacktraceToStream unable to allocate 
>message");
> 
>           if (println != NULL)
>             {
>               HVM_MethodCall(henv, println, stream_ref, msg_obj);
>             }
>           else
>             fprintf (stderr, "%s\n", msg_to_print);
> 
>           PR_smprintf_free(msg_to_print);
>         }

Reply via email to