java-stack.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // java-stack.h - Definitions for unwinding & inspecting the call stack.
  2. /* Copyright (C) 2005, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __JV_STACKTRACE_H__
  8. #define __JV_STACKTRACE_H__
  9. #include <stdlib.h>
  10. #include <unwind.h>
  11. #include <gcj/cni.h>
  12. #include <gcj/javaprims.h>
  13. #include <java-interp.h>
  14. #include <java/lang/Class.h>
  15. #include <java/lang/StackTraceElement.h>
  16. #include <java/lang/Throwable.h>
  17. #include <java/lang/Thread.h>
  18. #include <java/util/IdentityHashMap.h>
  19. #include <gnu/gcj/runtime/NameFinder.h>
  20. using namespace gnu::gcj::runtime;
  21. using namespace java::lang;
  22. extern "Java"
  23. {
  24. namespace gnu
  25. {
  26. namespace classpath
  27. {
  28. class VMStackWalker;
  29. }
  30. }
  31. }
  32. #ifdef INTERPRETER
  33. struct _Jv_InterpFrameInfo
  34. {
  35. _Jv_InterpMethod *meth;
  36. pc_t pc;
  37. };
  38. #endif
  39. struct _Jv_StackFrame
  40. {
  41. _Jv_FrameType type; /* Native or interpreted. */
  42. union {
  43. #ifdef INTERPRETER
  44. _Jv_InterpFrameInfo interp;
  45. #endif
  46. struct {
  47. jclass proxyClass;
  48. _Jv_Method *proxyMethod;
  49. };
  50. struct {
  51. void *ip;
  52. void *start_ip;
  53. };
  54. };
  55. jclass klass;
  56. _Jv_Method *meth;
  57. };
  58. struct _Jv_UnwindState;
  59. typedef _Unwind_Reason_Code (*_Jv_TraceFn) (_Jv_UnwindState *);
  60. struct _Jv_UnwindState
  61. {
  62. jint length; // length of FRAMES
  63. jint pos; // current position in FRAMES
  64. _Jv_StackFrame *frames; // array of stack frame data to be filled.
  65. #ifdef INTERPRETER
  66. _Jv_InterpFrame *interp_frame; // current frame in the interpreter stack.
  67. #endif
  68. _Jv_TraceFn trace_function; // function to call back after each frame
  69. // is enumerated. May be NULL.
  70. void *trace_data; // additional state data for trace_function.
  71. _Jv_UnwindState (jint ln)
  72. {
  73. length = ln;
  74. pos = 0;
  75. frames = NULL;
  76. #ifdef INTERPRETER
  77. Thread *thread = Thread::currentThread();
  78. // Check for NULL currentThread(), in case an exception is created
  79. // very early during the runtime startup.
  80. if (thread)
  81. interp_frame = (_Jv_InterpFrame *) thread->interp_frame;
  82. else
  83. interp_frame = NULL;
  84. #endif
  85. trace_function = NULL;
  86. trace_data = NULL;
  87. }
  88. };
  89. class _Jv_StackTrace
  90. {
  91. private:
  92. int length;
  93. _Jv_StackFrame frames[];
  94. static java::util::IdentityHashMap *ncodeMap;
  95. static void UpdateNCodeMap ();
  96. static jclass ClassForFrame (_Jv_StackFrame *frame);
  97. static void FillInFrameInfo (_Jv_StackFrame *frame);
  98. static void getLineNumberForFrame(_Jv_StackFrame *frame, NameFinder *finder,
  99. jstring *sourceFileName, jint *lineNum,
  100. jstring *methodName);
  101. static _Unwind_Reason_Code UnwindTraceFn (struct _Unwind_Context *context,
  102. void *state_ptr);
  103. static _Unwind_Reason_Code calling_class_trace_fn (_Jv_UnwindState *state);
  104. static _Unwind_Reason_Code non_system_trace_fn (_Jv_UnwindState *state);
  105. static _Unwind_Reason_Code accesscontrol_trace_fn (_Jv_UnwindState *state);
  106. static _Unwind_Reason_Code stackwalker_trace_fn (_Jv_UnwindState *state);
  107. static _Unwind_Reason_Code stackwalker_nnl_trace_fn (_Jv_UnwindState *state);
  108. public:
  109. static _Jv_StackTrace *GetStackTrace (void);
  110. static JArray< ::java::lang::StackTraceElement *>*
  111. GetStackTraceElements (_Jv_StackTrace *trace,
  112. java::lang::Throwable *throwable);
  113. static jclass GetCallingClass (jclass);
  114. static void GetCallerInfo (jclass checkClass, jclass *, _Jv_Method **);
  115. static ClassLoader *GetFirstNonSystemClassLoader (void);
  116. static jobjectArray GetAccessControlStack ();
  117. static JArray<jclass> *GetStackWalkerStack ();
  118. static jclass GetStackWalkerCallingClass ();
  119. static ClassLoader *GetStackWalkerFirstNonNullLoader ();
  120. friend jclass _Jv_GetMethodDeclaringClass (jmethodID);
  121. friend class gnu::classpath::VMStackWalker;
  122. };
  123. // Information about a given address.
  124. struct _Jv_AddrInfo
  125. {
  126. // File name of the defining module.
  127. const char *file_name;
  128. // Base address of the loaded module.
  129. void *base;
  130. // Name of the nearest symbol.
  131. const char *sym_name;
  132. // Address of the nearest symbol.
  133. void *sym_addr;
  134. ~_Jv_AddrInfo (void)
  135. {
  136. // On systems with a real dladdr(), the file and symbol names given by
  137. // _Jv_platform_dladdr() are not dynamically allocated. On Windows,
  138. // they are.
  139. #ifdef WIN32
  140. if (file_name)
  141. free ((void *)file_name);
  142. if (sym_name)
  143. free ((void *)sym_name);
  144. #endif /* WIN32 */
  145. }
  146. };
  147. #endif /* __JV_STACKTRACE_H__ */