3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Joseph Wenninger
29 $Id: stacktrace.c 1771 2004-12-17 23:05:27Z jowenn $
37 #include "asmoffsets.h"
39 #include "native/native.h"
40 #include "vm/global.h" /* required here for native includes */
41 #include "native/include/java_lang_ClassLoader.h"
42 #include "toolbox/logging.h"
43 #include "vm/builtin.h"
44 #include "vm/tables.h"
45 #include "vm/jit/codegen.inc.h"
50 extern classinfo *class_java_lang_Class;
51 extern classinfo *class_java_lang_SecurityManager;
53 /* the line number is only u2, but to avoid alignment problems it is made the same size as a native
54 pointer. In the structures where this is used, values of -1 or -2 have a special meainging, so
55 if java bytecode is ever extended to support more than 65535 lines/file, this could will have to
64 typedef struct lineNumberTableEntry {
65 /* The special value of -1 means that a inlined function starts, a value of -2 means that an inlined function ends*/
68 } lineNumberTableEntry;
70 typedef struct lineNumberTableEntryInlineBegin {
71 /*this should have the same layout and size as the lineNumberTableEntry*/
72 LineNumber lineNrOuter;
74 } lineNumberTableEntryInlineBegin;
77 typedef void(*CacaoStackTraceCollector)(void **,stackTraceBuffer*);
79 #define BLOCK_INITIALSIZE 40
80 #define BLOCK_SIZEINCREMENT 40
82 static void addEntry(stackTraceBuffer* buffer,methodinfo*method ,LineNumber line) {
83 if (buffer->size>buffer->full) {
84 stacktraceelement *tmp=&(buffer->start[buffer->full]);
87 buffer->full = buffer->full + 1;
89 log_text("addEntry (stacktrace):");
90 if (method) utf_display(method->name); else printf("Native");
91 if (method) {printf("\n");utf_display(method->class->name);}
92 printf("\nLine:%ld\n",line);
95 stacktraceelement *newBuffer=(stacktraceelement*)
96 malloc((buffer->size+BLOCK_SIZEINCREMENT)*sizeof(stacktraceelement));
97 if (newBuffer==0) panic("OOM during stacktrace creation");
98 memcpy(newBuffer,buffer->start,buffer->size*sizeof(stacktraceelement));
99 if (buffer->needsFree) free(buffer->start);
100 buffer->start=newBuffer;
101 buffer->size=buffer->size+BLOCK_SIZEINCREMENT;
103 addEntry(buffer,method,line);
107 static int fillInStackTrace_methodRecursive(stackTraceBuffer *buffer,methodinfo
108 *method,lineNumberTableEntry *startEntry, lineNumberTableEntry **entry, size_t *entriesAhead,void *adress) {
110 size_t ahead=*entriesAhead;
111 lineNumberTableEntry *ent=*entry;
112 lineNumberTableEntryInlineBegin *ilStart;
114 for (;ahead>0;ahead--,ent++) {
115 if (adress>=ent->pc) {
116 switch (ent->lineNr) {
117 case -1: /*begin of inlined method */
118 ilStart=(lineNumberTableEntryInlineBegin*)(++ent);
121 if (fillInStackTrace_methodRecursive(buffer,ilStart->method,ent,&ent,&ahead,adress)) {
122 addEntry(buffer,method,ilStart->lineNrOuter);
126 case -2: /*end of inlined method*/
132 if (adress==ent->pc) {
133 addEntry(buffer,method,ent->lineNr);
139 if (adress>startEntry->pc) {
141 addEntry(buffer,method,ent->lineNr);
143 } else panic("trace point before method");
147 addEntry(buffer,method,ent->lineNr);
152 static void fillInStackTrace_method(stackTraceBuffer *buffer,methodinfo *method,char *dataSeg, void* adress) {
153 size_t lineNumberTableSize=(*((size_t*)(dataSeg+LineNumberTableSize)));
156 if ( lineNumberTableSize == 0) {
157 /*right now this happens only on
158 i386,if the native stub causes an exception in a <clinit> invocation (jowenn)*/
159 addEntry(buffer,method,0);
162 lineNumberTableEntry *ent; /*=(lineNumberTableEntry*) ((*((char**)(dataSeg+LineNumberTableStart))) - (sizeof(lineNumberTableEntry)-sizeof(void*)));*/
164 lineNumberTableEntry *startEntry;
166 /* printf("dataSeg: %p\n",dataSeg);*/
167 calc=dataSeg+LineNumberTableStart;
168 /* printf("position of line number table start reference in data segment: %p\n",calc);
169 printf("line number table start as found in table: %p\n",*calc);*/
170 ent=(lineNumberTableEntry *) (((char*)(*calc) - (sizeof(lineNumberTableEntry)-sizeof(void*))));
171 /* printf("line number table start as calculated: %p\n",ent);*/
172 ent-=(lineNumberTableSize-1);
174 /* printf("line number table real start (bottom end) as calculated(2): %p\n",startEntry);*/
176 if (!fillInStackTrace_methodRecursive(buffer,method,startEntry,&ent,&lineNumberTableSize,adress)) {
177 panic("Trace point not found in suspected method");
183 void cacao_stacktrace_fillInStackTrace(void **target,CacaoStackTraceCollector coll)
186 stacktraceelement primaryBlock[BLOCK_INITIALSIZE*sizeof(stacktraceelement)];
187 /*In most cases this should be enough -> one malloc less. I don't think temporary data should be
188 allocated with the GC, only the result*/
189 stackTraceBuffer buffer;
191 buffer.start=primaryBlock;
192 buffer.size=BLOCK_INITIALSIZE*sizeof(stacktraceelement);
197 struct native_stackframeinfo *info=(*(((void**)(builtin_asm_get_stackframeinfo()))));
199 log_text("info ==0");
203 char *dataseg; /*make it byte addressable*/
204 methodinfo *currentMethod=0;
208 /* utf_display(info->method->class->name);
209 utf_display(info->method->name);*/
211 while ((currentMethod!=0) || (info!=0)) {
212 if (currentMethod==0) { /*some builtin native */
213 currentMethod=info->method;
214 returnAdress=info->returnToFromNative;
215 /*log_text("native");*/
217 /*utf_display(currentMethod->class->name);
218 utf_display(currentMethod->name);*/
219 addEntry(&buffer,currentMethod,0);
221 #if defined(__ALPHA__)
222 if (info->savedpv!=0)
223 dataseg=info->savedpv;
225 dataseg=codegen_findmethod(returnAdress);
226 #elif defined(__I386__)
227 dataseg=codegen_findmethod(returnAdress);
229 currentMethod=(*((methodinfo**)(dataseg+MethodPointer)));
230 if (info->beginOfJavaStackframe==0)
231 stackPtr=((char*)info)+sizeof(native_stackframeinfo);
233 #if defined(__ALPHA__)
234 stackPtr=(char*)(info->beginOfJavaStackframe);
235 #elif defined(__I386__)
236 stackPtr=(char*)(info->beginOfJavaStackframe)+sizeof(void*);
238 info=info->oldThreadspecificHeadValue;
239 } else { /*method created by jit*/
242 #if defined (__ALPHA__)
243 if (currentMethod->isleafmethod) {
245 printf("class.method:%s.%s\n",currentMethod->class->name->text,currentMethod->name->text);
247 panic("How could that happen ??? A leaf method in the middle of a stacktrace ??");
250 /*utf_display(currentMethod->class->name);
251 utf_display(currentMethod->name);*/
252 fillInStackTrace_method(&buffer,currentMethod,dataseg,returnAdress);
253 frameSize=*((u4*)(dataseg+FrameSize));
254 #if defined(__ALPHA__)
255 /* cacao saves the return adress as the first element of the stack frame on alphas*/
256 dataseg=codegen_findmethod(*((void**)(stackPtr+frameSize-sizeof(void*))));
257 returnAdress=(*((void**)(stackPtr+frameSize-sizeof(void*))));
258 #elif defined(__I386__)
259 /* on i386 the return adress is the first element before the stack frme*/
260 returnAdress=(*((void**)(stackPtr+frameSize)));
261 dataseg=codegen_findmethod(*((void**)(stackPtr+frameSize)));
263 /* printf ("threadrootmethod %p\n",builtin_asm_get_threadrootmethod());
264 if (currentMethod==builtin_asm_get_threadrootmethod()) break;*/
265 currentMethod=(*((methodinfo**)(dataseg+MethodPointer)));
266 #if defined(__ALPHA__)
268 #elif defined(__I386__)
269 stackPtr+=frameSize+sizeof(void*);
274 if (coll) coll(target,&buffer);
275 if (buffer.needsFree) free(buffer.start);
278 /*log_text("\n=========================================================");*/
286 void stackTraceCollector(void **target, stackTraceBuffer *buffer) {
287 stackTraceBuffer *dest=*target=heap_allocate(sizeof(stackTraceBuffer)+buffer->full*sizeof(stacktraceelement),true,0);
288 memcpy(*target,buffer,sizeof(stackTraceBuffer));
289 memcpy(dest+1,buffer->start,buffer->full*sizeof(stacktraceelement));
292 dest->size=dest->full;
296 if (buffer->full>0) {
297 printf("SOURCE BUFFER:%s\n",buffer->start[0].method->name->text);
298 printf("DEST BUFFER:%s\n",dest->start[0].method->name->text);
299 } else printf("Buffer is empty\n");
304 void cacao_stacktrace_NormalTrace(void **target) {
305 cacao_stacktrace_fillInStackTrace(target,&stackTraceCollector);
311 void classContextCollector(void **target, stackTraceBuffer *buffer) {
312 java_objectarray *tmpArray;
314 stacktraceelement *current;
315 stacktraceelement *start;
323 if (buffer->start[i].method!=0) targetSize++;
327 if (!class_java_lang_Class)
328 class_java_lang_Class = class_new(utf_new_char("java/lang/Class"));
330 if (!class_java_lang_SecurityManager)
331 class_java_lang_SecurityManager =
332 class_new(utf_new_char("java/lang/SecurityManager"));
334 if (targetSize > 0) {
335 if ((start->method) && (start->method->class== class_java_lang_SecurityManager)) {
342 builtin_newarray(targetSize, class_array_of(class_java_lang_Class)->vftbl);
344 for(i = 0, current = start; i < targetSize; i++, current++) {
345 if (current->method==0) { i--; continue;}
346 /*printf("adding item to class context array:%s\n",current->method->class->name->text);
347 printf("method for class: :%s\n",current->method->name->text);*/
348 use_class_as_object(current->method->class);
349 tmpArray->data[i] = (java_objectheader *) current->method->class;
357 java_objectarray *cacao_createClassContextArray() {
358 java_objectarray *array=0;
359 cacao_stacktrace_fillInStackTrace(&array,&classContextCollector);
366 void classLoaderCollector(void **target, stackTraceBuffer *buffer) {
368 stacktraceelement *current;
369 stacktraceelement *start;
371 classinfo *privilegedAction;
377 if (!class_java_lang_SecurityManager)
378 class_java_lang_SecurityManager =
379 class_new(utf_new_char("java/lang/SecurityManager"));
383 start=&(buffer->start[1]);
384 if (start == class_java_lang_SecurityManager) {
392 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
394 for(i=0, current = start; i < size; i++, current++) {
398 if (m->class == privilegedAction) {
403 if (m->class->classloader) {
404 *target= (java_lang_ClassLoader *) m->class->classloader;
412 java_objectheader *cacao_currentClassLoader() {
413 java_objectheader *header=0;
414 cacao_stacktrace_fillInStackTrace(&header,&classLoaderCollector);
420 * These are local overrides for various environment variables in Emacs.
421 * Please do not remove this and leave it at the end of the file, where
422 * Emacs will automagically detect them.
423 * ---------------------------------------------------------------------
426 * indent-tabs-mode: t