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 2195 2005-04-03 16:53:16Z edwin $
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"
45 #include "vm/tables.h"
46 #include "vm/jit/codegen.inc.h"
50 /*JoWenn: simplify collectors (trace doesn't contain internal methods)*/
52 /* the line number is only u2, but to avoid alignment problems it is made the same size as a native
53 pointer. In the structures where this is used, values of -1 or -2 have a special meainging, so
54 if java bytecode is ever extended to support more than 65535 lines/file, this could will have to
63 typedef struct lineNumberTableEntry {
64 /* The special value of -1 means that a inlined function starts, a value of -2 means that an inlined function ends*/
67 } lineNumberTableEntry;
69 typedef struct lineNumberTableEntryInlineBegin {
70 /*this should have the same layout and size as the lineNumberTableEntry*/
71 LineNumber lineNrOuter;
73 } lineNumberTableEntryInlineBegin;
76 typedef void(*CacaoStackTraceCollector)(void **,stackTraceBuffer*);
78 #define BLOCK_INITIALSIZE 40
79 #define BLOCK_SIZEINCREMENT 40
81 static void addEntry(stackTraceBuffer* buffer,methodinfo*method ,LineNumber line) {
82 if (buffer->size>buffer->full) {
83 stacktraceelement *tmp=&(buffer->start[buffer->full]);
86 buffer->full = buffer->full + 1;
88 log_text("addEntry (stacktrace):");
89 if (method) utf_display(method->name); else printf("Native");
90 if (method) {printf("\n");utf_display(method->class->name);}
91 printf("\nLine:%ld\n",line);
94 stacktraceelement *newBuffer=(stacktraceelement*)
95 malloc((buffer->size+BLOCK_SIZEINCREMENT)*sizeof(stacktraceelement));
96 if (newBuffer==0) panic("OOM during stacktrace creation");
97 memcpy(newBuffer,buffer->start,buffer->size*sizeof(stacktraceelement));
98 if (buffer->needsFree) free(buffer->start);
99 buffer->start=newBuffer;
100 buffer->size=buffer->size+BLOCK_SIZEINCREMENT;
102 addEntry(buffer,method,line);
106 static int fillInStackTrace_methodRecursive(stackTraceBuffer *buffer,methodinfo
107 *method,lineNumberTableEntry *startEntry, lineNumberTableEntry **entry, size_t *entriesAhead,void *adress) {
109 size_t ahead=*entriesAhead;
110 lineNumberTableEntry *ent=*entry;
111 lineNumberTableEntryInlineBegin *ilStart;
113 for (;ahead>0;ahead--,ent++) {
114 if (adress>=ent->pc) {
115 switch (ent->lineNr) {
116 case -1: /*begin of inlined method */
117 ilStart=(lineNumberTableEntryInlineBegin*)(++ent);
120 if (fillInStackTrace_methodRecursive(buffer,ilStart->method,ent,&ent,&ahead,adress)) {
121 addEntry(buffer,method,ilStart->lineNrOuter);
125 case -2: /*end of inlined method*/
131 if (adress==ent->pc) {
132 addEntry(buffer,method,ent->lineNr);
138 if (adress>startEntry->pc) {
140 addEntry(buffer,method,ent->lineNr);
142 } else panic("trace point before method");
146 addEntry(buffer,method,ent->lineNr);
151 static void fillInStackTrace_method(stackTraceBuffer *buffer,methodinfo *method,char *dataSeg, void* adress) {
152 size_t lineNumberTableSize=(*((size_t*)(dataSeg+LineNumberTableSize)));
155 if ( lineNumberTableSize == 0) {
156 /*right now this happens only on
157 i386,if the native stub causes an exception in a <clinit> invocation (jowenn)*/
158 addEntry(buffer,method,0);
161 lineNumberTableEntry *ent; /*=(lineNumberTableEntry*) ((*((char**)(dataSeg+LineNumberTableStart))) - (sizeof(lineNumberTableEntry)-sizeof(void*)));*/
163 lineNumberTableEntry *startEntry;
165 /* printf("dataSeg: %p\n",dataSeg);*/
166 calc=dataSeg+LineNumberTableStart;
167 /* printf("position of line number table start reference in data segment: %p\n",calc);
168 printf("line number table start as found in table: %p\n",*calc);*/
169 ent=(lineNumberTableEntry *) (((char*)(*calc) - (sizeof(lineNumberTableEntry)-sizeof(void*))));
170 /* printf("line number table start as calculated: %p\n",ent);*/
171 ent-=(lineNumberTableSize-1);
173 /* printf("line number table real start (bottom end) as calculated(2): %p\n",startEntry);*/
175 if (!fillInStackTrace_methodRecursive(buffer,method,startEntry,&ent,&lineNumberTableSize,adress)) {
176 panic("Trace point not found in suspected method");
182 void cacao_stacktrace_fillInStackTrace(void **target,CacaoStackTraceCollector coll)
185 stacktraceelement primaryBlock[BLOCK_INITIALSIZE*sizeof(stacktraceelement)];
186 /*In most cases this should be enough -> one malloc less. I don't think temporary data should be
187 allocated with the GC, only the result*/
188 stackTraceBuffer buffer;
190 buffer.start=primaryBlock;
191 buffer.size=BLOCK_INITIALSIZE*sizeof(stacktraceelement);
196 struct native_stackframeinfo *info=(*(((void**)(builtin_asm_get_stackframeinfo()))));
198 log_text("info ==0");
202 char *dataseg; /*make it byte addressable*/
203 methodinfo *currentMethod=0;
207 /* utf_display(info->method->class->name);
208 utf_display(info->method->name);*/
210 while ((currentMethod!=0) || (info!=0)) {
211 if (currentMethod==0) { /*some builtin native */
212 currentMethod=info->method;
213 returnAdress=info->returnToFromNative;
214 /*log_text("native");*/
216 /*utf_display(currentMethod->class->name);
217 utf_display(currentMethod->name);*/
218 addEntry(&buffer,currentMethod,0);
220 #if defined(__ALPHA__)
221 if (info->savedpv!=0)
222 dataseg=info->savedpv;
224 dataseg=codegen_findmethod(returnAdress);
225 #elif defined(__I386__)
226 dataseg=codegen_findmethod(returnAdress);
228 currentMethod=(*((methodinfo**)(dataseg+MethodPointer)));
229 if (info->beginOfJavaStackframe==0)
230 stackPtr=((char*)info)+sizeof(native_stackframeinfo);
232 #if defined(__ALPHA__)
233 stackPtr=(char*)(info->beginOfJavaStackframe);
234 #elif defined(__I386__)
235 stackPtr=(char*)(info->beginOfJavaStackframe)+sizeof(void*);
237 info=info->oldThreadspecificHeadValue;
238 } else { /*method created by jit*/
241 #if defined (__ALPHA__)
242 if (currentMethod->isleafmethod) {
244 printf("class.method:%s.%s\n",currentMethod->class->name->text,currentMethod->name->text);
246 panic("How could that happen ??? A leaf method in the middle of a stacktrace ??");
249 /*utf_display(currentMethod->class->name);
250 utf_display(currentMethod->name);*/
251 fillInStackTrace_method(&buffer,currentMethod,dataseg,returnAdress);
252 frameSize=*((u4*)(dataseg+FrameSize));
253 #if defined(__ALPHA__)
254 /* cacao saves the return adress as the first element of the stack frame on alphas*/
255 dataseg=codegen_findmethod(*((void**)(stackPtr+frameSize-sizeof(void*))));
256 returnAdress=(*((void**)(stackPtr+frameSize-sizeof(void*))));
257 #elif defined(__I386__)
258 /* on i386 the return adress is the first element before the stack frme*/
259 returnAdress=(*((void**)(stackPtr+frameSize)));
260 dataseg=codegen_findmethod(*((void**)(stackPtr+frameSize)));
262 /* printf ("threadrootmethod %p\n",builtin_asm_get_threadrootmethod());
263 if (currentMethod==builtin_asm_get_threadrootmethod()) break;*/
264 currentMethod=(*((methodinfo**)(dataseg+MethodPointer)));
265 #if defined(__ALPHA__)
267 #elif defined(__I386__)
268 stackPtr+=frameSize+sizeof(void*);
273 if (coll) coll(target,&buffer);
274 if (buffer.needsFree) free(buffer.start);
277 /*log_text("\n=========================================================");*/
285 void stackTraceCollector(void **target, stackTraceBuffer *buffer) {
286 stackTraceBuffer *dest=*target=heap_allocate(sizeof(stackTraceBuffer)+buffer->full*sizeof(stacktraceelement),true,0);
287 memcpy(*target,buffer,sizeof(stackTraceBuffer));
288 memcpy(dest+1,buffer->start,buffer->full*sizeof(stacktraceelement));
291 dest->size=dest->full;
295 if (buffer->full>0) {
296 printf("SOURCE BUFFER:%s\n",buffer->start[0].method->name->text);
297 printf("DEST BUFFER:%s\n",dest->start[0].method->name->text);
298 } else printf("Buffer is empty\n");
303 void cacao_stacktrace_NormalTrace(void **target) {
304 cacao_stacktrace_fillInStackTrace(target,&stackTraceCollector);
310 void classContextCollector(void **target, stackTraceBuffer *buffer) {
311 java_objectarray *tmpArray;
313 stacktraceelement *current;
314 stacktraceelement *start;
322 if (buffer->start[i].method!=0) targetSize++;
327 if (targetSize > 0) {
328 if ((start->method) && (start->method->class== class_java_lang_SecurityManager)) {
334 c = class_array_of(class_java_lang_Class,true);
336 panic("Could not create array class in classContextCollector");
338 builtin_newarray(targetSize, c->vftbl);
340 for(i = 0, current = start; i < targetSize; i++, current++) {
341 if (current->method==0) { i--; continue;}
342 /*printf("adding item to class context array:%s\n",current->method->class->name->text);
343 printf("method for class: :%s\n",current->method->name->text);*/
344 use_class_as_object(current->method->class);
345 tmpArray->data[i] = (java_objectheader *) current->method->class;
353 java_objectarray *cacao_createClassContextArray() {
354 java_objectarray *array=0;
355 cacao_stacktrace_fillInStackTrace(&array,&classContextCollector);
362 void classLoaderCollector(void **target, stackTraceBuffer *buffer) {
364 stacktraceelement *current;
365 stacktraceelement *start;
367 classinfo *privilegedAction;
374 start=&(buffer->start[1]);
375 if (start == class_java_lang_SecurityManager) {
384 if (!load_class_bootstrap(utf_new_char("java/security/PrivilegedAction"),&privilegedAction))
385 /* XXX handle exception */;
387 for(i=0, current = start; i < size; i++, current++) {
391 if (m->class == privilegedAction) {
396 if (m->class->classloader) {
397 *target= (java_lang_ClassLoader *) m->class->classloader;
405 java_objectheader *cacao_currentClassLoader() {
406 java_objectheader *header=0;
407 cacao_stacktrace_fillInStackTrace(&header,&classLoaderCollector);
413 void callingMethodCollector(void **target, stackTraceBuffer *buffer) {
414 if (buffer->full >2) (*target)=buffer->start[2].method;
418 methodinfo *cacao_callingMethod() {
420 cacao_stacktrace_fillInStackTrace(&method,&callingMethodCollector);
425 * These are local overrides for various environment variables in Emacs.
426 * Please do not remove this and leave it at the end of the file, where
427 * Emacs will automagically detect them.
428 * ---------------------------------------------------------------------
431 * indent-tabs-mode: t