OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [stacktrace.cc] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
// stacktrace.cc - Functions for unwinding & inspecting the call stack.
2
 
3
/* Copyright (C) 2005  Free Software Foundation
4
 
5
   This file is part of libgcj.
6
 
7
This software is copyrighted work licensed under the terms of the
8
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9
details.  */
10
 
11
#include <config.h>
12
 
13
#include <jvm.h>
14
#include <gcj/cni.h>
15
#include <java-interp.h>
16
#include <java-stack.h>
17
 
18
#ifdef HAVE_DLFCN_H
19
#include <dlfcn.h>
20
#endif
21
 
22
#include <stdio.h>
23
 
24
#include <java/lang/Class.h>
25
#include <java/lang/Long.h>
26
#include <java/util/ArrayList.h>
27
#include <java/util/IdentityHashMap.h>
28
#include <gnu/java/lang/MainThread.h>
29
#include <gnu/gcj/runtime/NameFinder.h>
30
 
31
#include <sysdep/backtrace.h>
32
#include <sysdep/descriptor.h>
33
 
34
using namespace java::lang;
35
using namespace java::lang::reflect;
36
using namespace java::util;
37
using namespace gnu::gcj::runtime;
38
 
39
// Maps ncode values to their containing native class.
40
// NOTE: Currently this Map contradicts class GC for native classes. This map
41
// (and the "new class stack") will need to use WeakReferences in order to 
42
// enable native class GC.
43
static java::util::IdentityHashMap *ncodeMap;
44
 
45
// Check the "class stack" for any classes initialized since we were last 
46
// called, and add them to ncodeMap.
47
void
48
_Jv_StackTrace::UpdateNCodeMap ()
49
{
50
  // The Map should be large enough so that a typical Java app doesn't cause 
51
  // it to rehash, without using too much memory. ~5000 entries should be 
52
  // enough.
53
  if (ncodeMap == NULL)
54
    ncodeMap = new java::util::IdentityHashMap (5087);
55
 
56
  jclass klass;
57
  while ((klass = _Jv_PopClass ()))
58
    {
59
      //printf ("got %s\n", klass->name->data);
60
#ifdef INTERPRETER
61
      JvAssert (! _Jv_IsInterpretedClass (klass));
62
#endif
63
      for (int i=0; i < klass->method_count; i++)
64
        {
65
          _Jv_Method *method = &klass->methods[i];
66
          void *ncode = method->ncode;
67
          // Add non-abstract methods to ncodeMap.
68
          if (ncode)
69
            {
70
              ncode = UNWRAP_FUNCTION_DESCRIPTOR (ncode);
71
              ncodeMap->put ((java::lang::Object *)ncode, klass);
72
            }
73
        }
74
    }
75
}
76
 
77
// Given a native frame, return the class which this code belongs 
78
// to. Returns NULL if this IP is not associated with a native Java class.
79
// If NCODE is supplied, it will be set with the ip for the entry point of the 
80
// enclosing method.
81
jclass
82
_Jv_StackTrace::ClassForFrame (_Jv_StackFrame *frame)
83
{
84
  JvAssert (frame->type == frame_native);
85
  jclass klass = NULL;
86
  // use _Unwind_FindEnclosingFunction to find start of method
87
  //void *entryPoint = _Unwind_FindEnclosingFunction (ip);
88
 
89
  // look it up in ncodeMap
90
  if (frame->start_ip)
91
    klass = (jclass) ncodeMap->get ((jobject) frame->start_ip);
92
 
93
  return klass;
94
}
95
 
96
_Unwind_Reason_Code
97
_Jv_StackTrace::UnwindTraceFn (struct _Unwind_Context *context, void *state_ptr)
98
{
99
  _Jv_UnwindState *state = (_Jv_UnwindState *) state_ptr;
100
  jint pos = state->pos;
101
 
102
  // Check if the trace buffer needs to be extended.
103
  if (pos == state->length)
104
    {
105
      int newLength = state->length * 2;
106
      void *newFrames = _Jv_AllocBytes (newLength * sizeof(_Jv_StackFrame));
107
      memcpy (newFrames, state->frames, state->length * sizeof(_Jv_StackFrame));
108
      state->frames = (_Jv_StackFrame *) newFrames;
109
      state->length = newLength;
110
    }
111
 
112
  void *func_addr = (void *) _Unwind_GetRegionStart (context);
113
 
114
  // If we see the interpreter's main function, "pop" an entry off the 
115
  // interpreter stack and use that instead, so that the trace goes through 
116
  // the java code and not the interpreter itself. This assumes a 1:1 
117
  // correspondance between call frames in the interpreted stack and occurances
118
  // of _Jv_InterpMethod::run() on the native stack.
119
#ifdef INTERPRETER
120
  void *interp_run = (void *) &_Jv_InterpMethod::run;
121
  if (func_addr == UNWRAP_FUNCTION_DESCRIPTOR (interp_run))
122
    {
123
      state->frames[pos].type = frame_interpreter;
124
      state->frames[pos].interp.meth = state->interp_frame->self;
125
      state->frames[pos].interp.pc = state->interp_frame->pc;
126
      state->interp_frame = state->interp_frame->next;
127
    }
128
  else
129
#endif
130
    {
131
      state->frames[pos].type = frame_native;
132
      state->frames[pos].ip = (void *) _Unwind_GetIP (context);
133
      state->frames[pos].start_ip = func_addr;
134
    }
135
 
136
  //printf ("unwind ip: %p\n", _Unwind_GetIP (context));
137
 
138
  _Unwind_Reason_Code result = _URC_NO_REASON;
139
  if (state->trace_function != NULL)
140
    result = (state->trace_function) (state);
141
  state->pos++;
142
  return result;
143
}
144
 
145
// Return a raw stack trace from the current point of execution. The raw 
146
// trace will include all functions that have unwind info.
147
_Jv_StackTrace *
148
_Jv_StackTrace::GetStackTrace(void)
149
{
150
  int trace_size = 100;
151
  _Jv_StackFrame frames[trace_size];
152
  _Jv_UnwindState state (trace_size);
153
  state.frames = (_Jv_StackFrame *) &frames;
154
 
155
#ifdef SJLJ_EXCEPTIONS
156
  // The Unwind interface doesn't work with the SJLJ exception model.
157
  // Fall back to a platform-specific unwinder.
158
  fallback_backtrace (&state);
159
#else /* SJLJ_EXCEPTIONS */  
160
  _Unwind_Backtrace (UnwindTraceFn, &state);
161
#endif /* SJLJ_EXCEPTIONS */
162
 
163
  // Copy the trace and return it.
164
  int traceSize = sizeof (_Jv_StackTrace) +
165
    (sizeof (_Jv_StackFrame) * state.pos);
166
  _Jv_StackTrace *trace = (_Jv_StackTrace *) _Jv_AllocBytes (traceSize);
167
  trace->length = state.pos;
168
  memcpy (trace->frames, state.frames, sizeof (_Jv_StackFrame) * state.pos);
169
  return trace;
170
}
171
 
172
void
173
_Jv_StackTrace::getLineNumberForFrame(_Jv_StackFrame *frame, NameFinder *finder,
174
                                      jstring *sourceFileName, jint *lineNum,
175
                                      jstring *methodName)
176
{
177
#ifdef INTERPRETER
178
  if (frame->type == frame_interpreter)
179
    {
180
      _Jv_InterpMethod *interp_meth = frame->interp.meth;
181
      _Jv_InterpClass *interp_class =
182
         (_Jv_InterpClass *) interp_meth->defining_class->aux_info;
183
      *sourceFileName = interp_class->source_file_name;
184
      *lineNum = interp_meth->get_source_line(frame->interp.pc);
185
      return;
186
    }
187
#endif
188
  // Use dladdr() to determine in which binary the address IP resides.
189
#if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
190
  Dl_info info;
191
  jstring binaryName = NULL;
192
  const char *argv0 = _Jv_GetSafeArg(0);
193
 
194
  void *ip = frame->ip;
195
  _Unwind_Ptr offset = 0;
196
 
197
  if (dladdr (ip, &info))
198
    {
199
      if (info.dli_fname)
200
        binaryName = JvNewStringUTF (info.dli_fname);
201
      else
202
        return;
203
 
204
      if (*methodName == NULL && info.dli_sname)
205
        *methodName = JvNewStringUTF (info.dli_sname);
206
 
207
      // addr2line expects relative addresses for shared libraries.
208
      if (strcmp (info.dli_fname, argv0) == 0)
209
        offset = (_Unwind_Ptr) ip;
210
      else
211
        offset = (_Unwind_Ptr) ip - (_Unwind_Ptr) info.dli_fbase;
212
 
213
      //printf ("linenum ip: %p\n", ip);
214
      //printf ("%s: 0x%x\n", info.dli_fname, offset);
215
      //offset -= sizeof(void *);
216
 
217
      // The unwinder gives us the return address. In order to get the right
218
      // line number for the stack trace, roll it back a little.
219
      offset -= 1;
220
 
221
      // printf ("%s: 0x%x\n", info.dli_fname, offset);
222
 
223
      finder->lookup (binaryName, (jlong) offset);
224
      *sourceFileName = finder->getSourceFile();
225
      *lineNum = finder->getLineNum();
226
    }
227
#endif
228
}
229
 
230
// Look up class and method info for the given stack frame, setting 
231
// frame->klass and frame->meth if they are known.
232
void
233
_Jv_StackTrace::FillInFrameInfo (_Jv_StackFrame *frame)
234
{
235
  jclass klass = NULL;
236
  _Jv_Method *meth = NULL;
237
 
238
  if (frame->type == frame_native)
239
    {
240
      klass = _Jv_StackTrace::ClassForFrame (frame);
241
 
242
      if (klass != NULL)
243
        // Find method in class
244
        for (int j = 0; j < klass->method_count; j++)
245
          {
246
            if (klass->methods[j].ncode == frame->start_ip)
247
              {
248
                meth = &klass->methods[j];
249
                break;
250
              }
251
          }
252
    }
253
#ifdef INTERPRETER
254
  else if (frame->type == frame_interpreter)
255
    {
256
      _Jv_InterpMethod *interp_meth = frame->interp.meth;
257
      klass = interp_meth->defining_class;
258
      meth = interp_meth->self;
259
    }
260
#endif
261
  else
262
    JvFail ("Unknown frame type");
263
 
264
  frame->klass = klass;
265
  frame->meth = meth;
266
}
267
 
268
// Convert raw stack frames to a Java array of StackTraceElement objects.
269
JArray< ::java::lang::StackTraceElement *>*
270
_Jv_StackTrace::GetStackTraceElements (_Jv_StackTrace *trace,
271
  Throwable *throwable __attribute__((unused)))
272
{
273
  ArrayList *list = new ArrayList ();
274
 
275
#ifdef SJLJ_EXCEPTIONS
276
  // We can't use the nCodeMap without unwinder support. Instead,
277
  // fake the method name by giving the IP in hex - better than nothing.  
278
  jstring hex = JvNewStringUTF ("0x");
279
 
280
  for (int i = 0; i < trace->length; i++)
281
    {
282
      jstring sourceFileName = NULL;
283
      jint lineNum = -1;
284
      _Jv_StackFrame *frame = &trace->frames[i];
285
 
286
      jstring className = NULL;
287
      jstring methodName = hex->concat (Long::toHexString ((jlong) frame->ip));
288
 
289
      StackTraceElement *element = new StackTraceElement (sourceFileName,
290
        lineNum, className, methodName, 0);
291
      list->add (element);
292
    }
293
 
294
#else /* SJLJ_EXCEPTIONS */
295
 
296
  //JvSynchronized (ncodeMap);
297
  UpdateNCodeMap ();
298
 
299
  NameFinder *finder = new NameFinder();
300
  int start_idx = 0;
301
  int end_idx = trace->length - 1;
302
 
303
  // First pass: strip superfluous frames from beginning and end of the trace.  
304
  for (int i = 0; i < trace->length; i++)
305
    {
306
      _Jv_StackFrame *frame = &trace->frames[i];
307
      FillInFrameInfo (frame);
308
 
309
      if (!frame->klass || !frame->meth)
310
        // Not a Java frame.
311
        continue;
312
 
313
      // Throw away the top of the stack till we see:
314
      //  - the constructor(s) of this Throwable, or
315
      //  - the Throwable.fillInStackTrace call.
316
      if (frame->klass == throwable->getClass()
317
          && strcmp (frame->meth->name->chars(), "<init>") == 0)
318
        start_idx = i + 1;
319
 
320
      if (frame->klass == &Throwable::class$
321
          && strcmp (frame->meth->name->chars(), "fillInStackTrace") == 0)
322
        start_idx = i + 1;
323
 
324
      // End the trace at the application's main() method if we see call_main.
325
      if (frame->klass == &gnu::java::lang::MainThread::class$
326
          && strcmp (frame->meth->name->chars(), "call_main") == 0)
327
        end_idx = i - 1;
328
    }
329
 
330
  const jboolean remove_unknown
331
    = gnu::gcj::runtime::NameFinder::removeUnknown();
332
 
333
  // Second pass: Look up line-number info for remaining frames.
334
  for (int i = start_idx; i <= end_idx; i++)
335
    {
336
      _Jv_StackFrame *frame = &trace->frames[i];
337
 
338
      if (frame->klass == NULL && remove_unknown)
339
        // Not a Java frame.
340
        continue;
341
 
342
      jstring className = NULL;
343
      if (frame->klass != NULL)
344
        className = frame->klass->getName ();
345
 
346
      jstring methodName = NULL;
347
      if (frame->meth)
348
        methodName = JvNewStringUTF (frame->meth->name->chars());
349
 
350
      jstring sourceFileName = NULL;
351
      jint lineNum = -1;
352
 
353
      getLineNumberForFrame(frame, finder, &sourceFileName, &lineNum,
354
                            &methodName);
355
 
356
      StackTraceElement *element = new StackTraceElement (sourceFileName, lineNum,
357
        className, methodName, 0);
358
      list->add (element);
359
    }
360
 
361
  finder->close();
362
#endif /* SJLJ_EXCEPTIONS */
363
 
364
  JArray<Object *> *array = JvNewObjectArray (list->size (),
365
    &StackTraceElement::class$, NULL);
366
 
367
  return (JArray<StackTraceElement *>*) list->toArray (array);
368
}
369
 
370
struct CallingClassTraceData
371
{
372
  jclass checkClass;
373
  jclass foundClass;
374
  _Jv_Method *foundMeth;
375
  bool seen_checkClass;
376
};
377
 
378
_Unwind_Reason_Code
379
_Jv_StackTrace::calling_class_trace_fn (_Jv_UnwindState *state)
380
{
381
  CallingClassTraceData *trace_data = (CallingClassTraceData *)
382
    state->trace_data;
383
  _Jv_StackFrame *frame = &state->frames[state->pos];
384
  FillInFrameInfo (frame);
385
 
386
  if (trace_data->seen_checkClass
387
      && frame->klass
388
      && frame->klass != trace_data->checkClass)
389
    {
390
      trace_data->foundClass = frame->klass;
391
      trace_data->foundMeth = frame->meth;
392
      return _URC_NORMAL_STOP;
393
    }
394
 
395
  if (frame->klass == trace_data->checkClass)
396
    trace_data->seen_checkClass = true;
397
 
398
  return _URC_NO_REASON;
399
}
400
 
401
// Find the class immediately above the given class on the call stack. Any 
402
// intermediate non-Java 
403
// frames are ignored. If the calling class could not be determined (eg because 
404
// the unwinder is not supported on this platform), NULL is returned.
405
// This function is used to implement calling-classloader checks and reflection
406
// accessibility checks.
407
// CHECKCLASS is typically the class calling GetCallingClass. The first class
408
// above CHECKCLASS on the call stack will be returned.
409
jclass
410
_Jv_StackTrace::GetCallingClass (jclass checkClass)
411
{
412
  jclass result = NULL;
413
  GetCallerInfo (checkClass, &result, NULL);
414
  return result;
415
}
416
 
417
void
418
_Jv_StackTrace::GetCallerInfo (jclass checkClass, jclass *caller_class,
419
  _Jv_Method **caller_meth)
420
{
421
#ifndef SJLJ_EXCEPTIONS
422
  int trace_size = 20;
423
  _Jv_StackFrame frames[trace_size];
424
  _Jv_UnwindState state (trace_size);
425
  state.frames = (_Jv_StackFrame *) &frames;
426
 
427
  CallingClassTraceData trace_data;
428
  trace_data.checkClass = checkClass;
429
  trace_data.seen_checkClass = false;
430
  trace_data.foundClass = NULL;
431
  trace_data.foundMeth = NULL;
432
 
433
  state.trace_function = calling_class_trace_fn;
434
  state.trace_data = (void *) &trace_data;
435
 
436
  //JvSynchronized (ncodeMap);
437
  UpdateNCodeMap ();
438
 
439
  _Unwind_Backtrace (UnwindTraceFn, &state);
440
 
441
  if (caller_class)
442
    *caller_class = trace_data.foundClass;
443
  if (caller_meth)
444
    *caller_meth = trace_data.foundMeth;
445
#else
446
  return;
447
#endif
448
}
449
 
450
// Return a java array containing the Java classes on the stack above CHECKCLASS.
451
JArray<jclass> *
452
_Jv_StackTrace::GetClassContext (jclass checkClass)
453
{
454
  JArray<jclass> *result = NULL;
455
 
456
  int trace_size = 100;
457
  _Jv_StackFrame frames[trace_size];
458
  _Jv_UnwindState state (trace_size);
459
  state.frames = (_Jv_StackFrame *) &frames;
460
 
461
  //JvSynchronized (ncodeMap);
462
  UpdateNCodeMap ();
463
 
464
  _Unwind_Backtrace (UnwindTraceFn, &state);
465
 
466
  // Count the number of Java frames on the stack.
467
  int jframe_count = 0;
468
  bool seen_checkClass = false;
469
  int start_pos = -1;
470
  for (int i = 0; i < state.pos; i++)
471
    {
472
      _Jv_StackFrame *frame = &state.frames[i];
473
      FillInFrameInfo (frame);
474
 
475
      if (seen_checkClass)
476
        {
477
          if (frame->klass)
478
            {
479
              jframe_count++;
480
              if (start_pos == -1)
481
                start_pos = i;
482
            }
483
        }
484
      else
485
        seen_checkClass = frame->klass == checkClass;
486
    }
487
  result = (JArray<jclass> *) _Jv_NewObjectArray (jframe_count, &Class::class$, NULL);
488
  int pos = 0;
489
 
490
  for (int i = start_pos; i < state.pos; i++)
491
    {
492
      _Jv_StackFrame *frame = &state.frames[i];
493
      if (frame->klass)
494
        elements(result)[pos++] = frame->klass;
495
    }
496
  return result;
497
}
498
 
499
_Unwind_Reason_Code
500
_Jv_StackTrace::non_system_trace_fn (_Jv_UnwindState *state)
501
{
502
  _Jv_StackFrame *frame = &state->frames[state->pos];
503
  FillInFrameInfo (frame);
504
 
505
  ClassLoader *classLoader = NULL;
506
 
507
  if (frame->klass)
508
    {
509
      classLoader = frame->klass->getClassLoaderInternal();
510
#ifdef INTERPRETER
511
      if (classLoader != NULL)
512
        {
513
          state->trace_data = (void *) classLoader;
514
          return _URC_NORMAL_STOP;
515
        }
516
#endif
517
    }
518
 
519
  return _URC_NO_REASON;
520
}
521
 
522
ClassLoader *
523
_Jv_StackTrace::GetFirstNonSystemClassLoader ()
524
{
525
  int trace_size = 32;
526
  _Jv_StackFrame frames[trace_size];
527
  _Jv_UnwindState state (trace_size);
528
  state.frames = (_Jv_StackFrame *) &frames;
529
  state.trace_function = non_system_trace_fn;
530
  state.trace_data = NULL;
531
 
532
  //JvSynchronized (ncodeMap);
533
  UpdateNCodeMap ();
534
 
535
  _Unwind_Backtrace (UnwindTraceFn, &state);
536
 
537
  if (state.trace_data)
538
    return (ClassLoader *) state.trace_data;
539
 
540
  return NULL;
541
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.