jvmti.cc 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627
  1. // jvmti.cc - JVMTI implementation
  2. /* Copyright (C) 2006, 2007, 2010 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. #include <config.h>
  8. #include <platform.h>
  9. #include <jvm.h>
  10. #include <java-threads.h>
  11. #include <java-gc.h>
  12. #include <java-interp.h>
  13. #include <jvmti.h>
  14. #include "jvmti-int.h"
  15. #include <gcj/method.h>
  16. #include <gnu/classpath/SystemProperties.h>
  17. #include <gnu/gcj/runtime/BootClassLoader.h>
  18. #include <gnu/gcj/jvmti/Breakpoint.h>
  19. #include <gnu/gcj/jvmti/BreakpointManager.h>
  20. #include <java/lang/Class.h>
  21. #include <java/lang/ClassLoader.h>
  22. #include <java/lang/OutOfMemoryError.h>
  23. #include <java/lang/Thread.h>
  24. #include <java/lang/ThreadGroup.h>
  25. #include <java/lang/Thread$State.h>
  26. #include <java/lang/Throwable.h>
  27. #include <java/lang/VMClassLoader.h>
  28. #include <java/lang/reflect/Field.h>
  29. #include <java/lang/reflect/Modifier.h>
  30. #include <java/util/Collection.h>
  31. #include <java/util/HashMap.h>
  32. #include <java/util/concurrent/locks/Lock.h>
  33. #include <java/util/concurrent/locks/ReentrantReadWriteLock.h>
  34. #include <java/net/URL.h>
  35. static void check_enabled_events (void);
  36. static void check_enabled_event (jvmtiEvent);
  37. namespace JVMTI
  38. {
  39. // Is JVMTI enabled? (i.e., any jvmtiEnv created?)
  40. bool enabled;
  41. // Event notifications
  42. bool VMInit = false;
  43. bool VMDeath = false;
  44. bool ThreadStart = false;
  45. bool ThreadEnd = false;
  46. bool ClassFileLoadHook = false;
  47. bool ClassLoad = false;
  48. bool ClassPrepare = false;
  49. bool VMStart = false;
  50. bool Exception = false;
  51. bool ExceptionCatch = false;
  52. bool SingleStep = false;
  53. bool FramePop = false;
  54. bool Breakpoint = false;
  55. bool FieldAccess = false;
  56. bool FieldModification = false;
  57. bool MethodEntry = false;
  58. bool MethodExit = false;
  59. bool NativeMethodBind = false;
  60. bool CompiledMethodLoad = false;
  61. bool CompiledMethodUnload = false;
  62. bool DynamicCodeGenerated = false;
  63. bool DataDumpRequest = false;
  64. bool reserved72 = false;
  65. bool MonitorWait = false;
  66. bool MonitorWaited = false;
  67. bool MonitorContendedEnter = false;
  68. bool MonitorContendedEntered = false;
  69. bool reserved77 = false;
  70. bool reserved78 = false;
  71. bool reserved79 = false;
  72. bool reserved80 = false;
  73. bool GarbageCollectionStart = false;
  74. bool GarbageCollectionFinish = false;
  75. bool ObjectFree = false;
  76. bool VMObjectAlloc = false;
  77. };
  78. extern struct JNINativeInterface _Jv_JNIFunctions;
  79. struct _Jv_rawMonitorID
  80. {
  81. _Jv_Mutex_t mutex;
  82. _Jv_ConditionVariable_t condition;
  83. };
  84. /* A simple linked list of all JVMTI environments. Since
  85. events must be delivered to environments in the order
  86. in which the environments were created, new environments
  87. are added to the end of the list. */
  88. struct jvmti_env_list
  89. {
  90. jvmtiEnv *env;
  91. struct jvmti_env_list *next;
  92. };
  93. static struct jvmti_env_list *_jvmtiEnvironments = NULL;
  94. static java::util::concurrent::locks::
  95. ReentrantReadWriteLock *_envListLock = NULL;
  96. #define FOREACH_ENVIRONMENT(Ele) \
  97. for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)
  98. // Some commonly-used checks
  99. #define THREAD_DEFAULT_TO_CURRENT(Ajthread) \
  100. do \
  101. { \
  102. if (Ajthread == NULL) \
  103. Ajthread = java::lang::Thread::currentThread (); \
  104. } \
  105. while (0)
  106. #define THREAD_CHECK_VALID(Athread) \
  107. do \
  108. { \
  109. if (!java::lang::Thread::class$.isAssignableFrom (&(Athread->class$))) \
  110. return JVMTI_ERROR_INVALID_THREAD; \
  111. } \
  112. while (0)
  113. #define THREAD_CHECK_IS_ALIVE(Athread) \
  114. do \
  115. { \
  116. if (!Athread->isAlive ()) \
  117. return JVMTI_ERROR_THREAD_NOT_ALIVE; \
  118. } \
  119. while (0)
  120. // FIXME: if current phase is not set in Phases,
  121. // return JVMTI_ERROR_WRONG_PHASE
  122. #define REQUIRE_PHASE(Env, Phases)
  123. #define NULL_CHECK(Ptr) \
  124. do \
  125. { \
  126. if (Ptr == NULL) \
  127. return JVMTI_ERROR_NULL_POINTER; \
  128. } \
  129. while (0)
  130. #define ILLEGAL_ARGUMENT(Cond) \
  131. do \
  132. { \
  133. if ((Cond)) \
  134. return JVMTI_ERROR_ILLEGAL_ARGUMENT; \
  135. } \
  136. while (0)
  137. #define CHECK_FOR_NATIVE_METHOD(AjmethodID) \
  138. do \
  139. { \
  140. jboolean is_native; \
  141. jvmtiError jerr = env->IsMethodNative (AjmethodID, &is_native); \
  142. if (jerr != JVMTI_ERROR_NONE) \
  143. return jerr; \
  144. if (is_native) \
  145. return JVMTI_ERROR_NATIVE_METHOD; \
  146. } \
  147. while (0)
  148. static jvmtiError JNICALL
  149. _Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
  150. {
  151. using namespace java::lang;
  152. THREAD_DEFAULT_TO_CURRENT (thread);
  153. THREAD_CHECK_VALID (thread);
  154. THREAD_CHECK_IS_ALIVE (thread);
  155. _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
  156. _Jv_SuspendThread (data);
  157. return JVMTI_ERROR_NONE;
  158. }
  159. static jvmtiError JNICALL
  160. _Jv_JVMTI_ResumeThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
  161. {
  162. using namespace java::lang;
  163. THREAD_DEFAULT_TO_CURRENT (thread);
  164. THREAD_CHECK_VALID (thread);
  165. THREAD_CHECK_IS_ALIVE (thread);
  166. _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
  167. _Jv_ResumeThread (data);
  168. return JVMTI_ERROR_NONE;
  169. }
  170. static jvmtiError JNICALL
  171. _Jv_JVMTI_InterruptThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
  172. {
  173. using namespace java::lang;
  174. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  175. // FIXME: capability handling? 'can_signal_thread'
  176. if (thread == NULL)
  177. return JVMTI_ERROR_INVALID_THREAD;
  178. THREAD_CHECK_VALID (thread);
  179. THREAD_CHECK_IS_ALIVE (thread);
  180. thread->interrupt();
  181. return JVMTI_ERROR_NONE;
  182. }
  183. // This method performs the common tasks to get and set variables of all types.
  184. // It is called by the _Jv_JVMTI_Get/SetLocalInt/Object/.... methods.
  185. static jvmtiError
  186. getLocalFrame (jvmtiEnv *env, jthread thread, jint depth, jint slot, char type,
  187. _Jv_InterpFrame **iframe)
  188. {
  189. using namespace java::lang;
  190. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  191. ILLEGAL_ARGUMENT (depth < 0);
  192. THREAD_DEFAULT_TO_CURRENT (thread);
  193. THREAD_CHECK_VALID (thread);
  194. THREAD_CHECK_IS_ALIVE (thread);
  195. _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  196. for (int i = 0; i < depth; i++)
  197. {
  198. frame = frame->next;
  199. if (frame == NULL)
  200. return JVMTI_ERROR_NO_MORE_FRAMES;
  201. }
  202. if (frame->frame_type == frame_native)
  203. return JVMTI_ERROR_OPAQUE_FRAME;
  204. jint max_locals;
  205. jvmtiError jerr = env->GetMaxLocals (reinterpret_cast<jmethodID>
  206. (frame->self->get_method ()),
  207. &max_locals);
  208. if (jerr != JVMTI_ERROR_NONE)
  209. return jerr;
  210. _Jv_InterpFrame *tmp_iframe = reinterpret_cast<_Jv_InterpFrame *> (frame);
  211. // The second slot taken up by a long type is marked as type 'x' meaning it
  212. // is not valid for access since it holds only the 4 low bytes of the value.
  213. if (tmp_iframe->locals_type[slot] == 'x')
  214. return JVMTI_ERROR_INVALID_SLOT;
  215. if (tmp_iframe->locals_type[slot] != type)
  216. return JVMTI_ERROR_TYPE_MISMATCH;
  217. // Check for invalid slots, if the type is a long type, we must check that
  218. // the next slot is valid as well.
  219. if (slot < 0 || slot >= max_locals
  220. || ((type == 'l' || type == 'd') && slot + 1 >= max_locals))
  221. return JVMTI_ERROR_INVALID_SLOT;
  222. *iframe = tmp_iframe;
  223. return JVMTI_ERROR_NONE;
  224. }
  225. static jvmtiError JNICALL
  226. _Jv_JVMTI_GetLocalObject (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  227. jobject *value)
  228. {
  229. NULL_CHECK (value);
  230. _Jv_InterpFrame *frame;
  231. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'o', &frame);
  232. if (jerr != JVMTI_ERROR_NONE)
  233. return jerr;
  234. *value = frame->locals[slot].o;
  235. return JVMTI_ERROR_NONE;
  236. }
  237. static jvmtiError JNICALL
  238. _Jv_JVMTI_SetLocalObject (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  239. jobject value)
  240. {
  241. _Jv_InterpFrame *frame;
  242. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'o', &frame);
  243. if (jerr != JVMTI_ERROR_NONE)
  244. return jerr;
  245. frame->locals[slot].o = value;
  246. return JVMTI_ERROR_NONE;
  247. }
  248. static jvmtiError JNICALL
  249. _Jv_JVMTI_GetLocalInt (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  250. jint *value)
  251. {
  252. NULL_CHECK (value);
  253. _Jv_InterpFrame *frame;
  254. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'i', &frame);
  255. if (jerr != JVMTI_ERROR_NONE)
  256. return jerr;
  257. *value = frame->locals[slot].i;
  258. return JVMTI_ERROR_NONE;
  259. }
  260. static jvmtiError JNICALL
  261. _Jv_JVMTI_SetLocalInt (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  262. jint value)
  263. {
  264. _Jv_InterpFrame *frame;
  265. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'i', &frame);
  266. if (jerr != JVMTI_ERROR_NONE)
  267. return jerr;
  268. frame->locals[slot].i = value;
  269. return JVMTI_ERROR_NONE;
  270. }
  271. static jvmtiError JNICALL
  272. _Jv_JVMTI_GetLocalLong (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  273. jlong *value)
  274. {
  275. NULL_CHECK (value);
  276. _Jv_InterpFrame *frame;
  277. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'l', &frame);
  278. if (jerr != JVMTI_ERROR_NONE)
  279. return jerr;
  280. #if SIZEOF_VOID_P==8
  281. *value = frame->locals[slot].l;
  282. #else
  283. _Jv_word2 val;
  284. val.ia[0] = frame->locals[slot].ia[0];
  285. val.ia[1] = frame->locals[slot + 1].ia[0];
  286. *value = val.l;
  287. #endif
  288. return JVMTI_ERROR_NONE;
  289. }
  290. static jvmtiError JNICALL
  291. _Jv_JVMTI_SetLocalLong (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  292. jlong value)
  293. {
  294. _Jv_InterpFrame *frame;
  295. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'l', &frame);
  296. if (jerr != JVMTI_ERROR_NONE)
  297. return jerr;
  298. #if SIZEOF_VOID_P==8
  299. frame->locals[slot].l = value;
  300. #else
  301. _Jv_word2 val;
  302. val.l = value;
  303. frame->locals[slot].ia[0] = val.ia[0];
  304. frame->locals[slot + 1].ia[0] = val.ia[1];
  305. #endif
  306. return JVMTI_ERROR_NONE;
  307. }
  308. static jvmtiError JNICALL
  309. _Jv_JVMTI_GetLocalFloat (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  310. jfloat *value)
  311. {
  312. NULL_CHECK (value);
  313. _Jv_InterpFrame *frame;
  314. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'f', &frame);
  315. if (jerr != JVMTI_ERROR_NONE)
  316. return jerr;
  317. *value = frame->locals[slot].f;
  318. return JVMTI_ERROR_NONE;
  319. }
  320. static jvmtiError JNICALL
  321. _Jv_JVMTI_SetLocalFloat (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  322. jfloat value)
  323. {
  324. _Jv_InterpFrame *frame;
  325. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'f', &frame);
  326. if (jerr != JVMTI_ERROR_NONE)
  327. return jerr;
  328. frame->locals[slot].f = value;
  329. return JVMTI_ERROR_NONE;
  330. }
  331. static jvmtiError JNICALL
  332. _Jv_JVMTI_GetLocalDouble (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  333. jdouble *value)
  334. {
  335. NULL_CHECK (value);
  336. _Jv_InterpFrame *frame;
  337. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'd', &frame);
  338. if (jerr != JVMTI_ERROR_NONE)
  339. return jerr;
  340. #if SIZEOF_VOID_P==8
  341. *value = frame->locals[slot].d;
  342. #else
  343. _Jv_word2 val;
  344. val.ia[0] = frame->locals[slot].ia[0];
  345. val.ia[1] = frame->locals[slot + 1].ia[0];
  346. *value = val.d;
  347. #endif
  348. return JVMTI_ERROR_NONE;
  349. }
  350. static jvmtiError JNICALL
  351. _Jv_JVMTI_SetLocalDouble (jvmtiEnv *env, jthread thread, jint depth, jint slot,
  352. jdouble value)
  353. {
  354. _Jv_InterpFrame *frame;
  355. jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'd', &frame);
  356. if (jerr != JVMTI_ERROR_NONE)
  357. return jerr;
  358. #if SIZEOF_VOID_P==8
  359. frame->locals[slot].d = value;
  360. #else
  361. _Jv_word2 val;
  362. val.d = value;
  363. frame->locals[slot].ia[0] = val.ia[0];
  364. frame->locals[slot + 1].ia[0] = val.ia[1];
  365. #endif
  366. return JVMTI_ERROR_NONE;
  367. }
  368. static jvmtiError JNICALL
  369. _Jv_JVMTI_GetAllThreads(MAYBE_UNUSED jvmtiEnv *env, jint *thread_cnt,
  370. jthread **threads)
  371. {
  372. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  373. NULL_CHECK (thread_cnt);
  374. NULL_CHECK (threads);
  375. using namespace java::lang;
  376. ThreadGroup *root_grp = ThreadGroup::root;
  377. jint estimate = root_grp->activeCount ();
  378. JArray<Thread *> *thr_arr;
  379. // Allocate some extra space since threads can be created between calls
  380. try
  381. {
  382. thr_arr = reinterpret_cast<JArray<Thread *> *> (JvNewObjectArray
  383. ((estimate * 2),
  384. &Thread::class$, NULL));
  385. }
  386. catch (java::lang::OutOfMemoryError *err)
  387. {
  388. return JVMTI_ERROR_OUT_OF_MEMORY;
  389. }
  390. *thread_cnt = root_grp->enumerate (thr_arr);
  391. jvmtiError jerr = env->Allocate ((jlong) ((*thread_cnt) * sizeof (jthread)),
  392. (unsigned char **) threads);
  393. if (jerr != JVMTI_ERROR_NONE)
  394. return jerr;
  395. // Transfer the threads to the result array
  396. jthread *tmp_arr = reinterpret_cast<jthread *> (elements (thr_arr));
  397. memcpy ((*threads), tmp_arr, (*thread_cnt));
  398. return JVMTI_ERROR_NONE;
  399. }
  400. static jvmtiError JNICALL
  401. _Jv_JVMTI_GetFrameCount (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
  402. jint *frame_count)
  403. {
  404. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  405. NULL_CHECK (frame_count);
  406. using namespace java::lang;
  407. THREAD_DEFAULT_TO_CURRENT (thread);
  408. THREAD_CHECK_VALID (thread);
  409. THREAD_CHECK_IS_ALIVE (thread);
  410. _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  411. (*frame_count) = frame->depth ();
  412. return JVMTI_ERROR_NONE;
  413. }
  414. static jvmtiError JNICALL
  415. _Jv_JVMTI_GetThreadState (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
  416. jint *thread_state_ptr)
  417. {
  418. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  419. THREAD_DEFAULT_TO_CURRENT (thread);
  420. THREAD_CHECK_VALID (thread);
  421. NULL_CHECK (thread_state_ptr);
  422. jint state = 0;
  423. if (thread->isAlive ())
  424. {
  425. state |= JVMTI_THREAD_STATE_ALIVE;
  426. _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
  427. if (_Jv_IsThreadSuspended (data))
  428. state |= JVMTI_THREAD_STATE_SUSPENDED;
  429. if (thread->isInterrupted ())
  430. state |= JVMTI_THREAD_STATE_INTERRUPTED;
  431. _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  432. if (frame != NULL && frame->frame_type == frame_native)
  433. state |= JVMTI_THREAD_STATE_IN_NATIVE;
  434. using namespace java::lang;
  435. Thread$State *ts = thread->getState ();
  436. if (ts == Thread$State::RUNNABLE)
  437. state |= JVMTI_THREAD_STATE_RUNNABLE;
  438. else if (ts == Thread$State::BLOCKED)
  439. state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
  440. else if (ts == Thread$State::TIMED_WAITING
  441. || ts == Thread$State::WAITING)
  442. {
  443. state |= JVMTI_THREAD_STATE_WAITING;
  444. state |= ((ts == Thread$State::WAITING)
  445. ? JVMTI_THREAD_STATE_WAITING_INDEFINITELY
  446. : JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT);
  447. /* FIXME: We don't have a way to tell
  448. the caller why the thread is suspended,
  449. i.e., JVMTI_THREAD_STATE_SLEEPING,
  450. JVMTI_THREAD_STATE_PARKED, and
  451. JVMTI_THREAD_STATE_IN_OBJECT_WAIT
  452. are never set. */
  453. }
  454. }
  455. else
  456. {
  457. using namespace java::lang;
  458. Thread$State *ts = thread->getState ();
  459. if (ts == Thread$State::TERMINATED)
  460. state |= JVMTI_THREAD_STATE_TERMINATED;
  461. }
  462. *thread_state_ptr = state;
  463. return JVMTI_ERROR_NONE;
  464. }
  465. static jvmtiError JNICALL
  466. _Jv_JVMTI_CreateRawMonitor (MAYBE_UNUSED jvmtiEnv *env, const char *name,
  467. jrawMonitorID *result)
  468. {
  469. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  470. NULL_CHECK (name);
  471. NULL_CHECK (result);
  472. *result = (jrawMonitorID) _Jv_MallocUnchecked (sizeof (_Jv_rawMonitorID));
  473. if (*result == NULL)
  474. return JVMTI_ERROR_OUT_OF_MEMORY;
  475. _Jv_MutexInit (&(*result)->mutex);
  476. _Jv_CondInit (&(*result)->condition);
  477. return JVMTI_ERROR_NONE;
  478. }
  479. static jvmtiError JNICALL
  480. _Jv_JVMTI_DestroyRawMonitor (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
  481. {
  482. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  483. // Note we have no better way of knowing whether this object is
  484. // really a raw monitor.
  485. if (monitor == NULL)
  486. return JVMTI_ERROR_INVALID_MONITOR;
  487. // FIXME: perform checks on monitor, release it if this thread owns
  488. // it.
  489. #ifdef _Jv_HaveMutexDestroy
  490. _Jv_MutexDestroy (&monitor->mutex);
  491. #endif
  492. _Jv_Free (monitor);
  493. return JVMTI_ERROR_NONE;
  494. }
  495. static jvmtiError JNICALL
  496. _Jv_JVMTI_RawMonitorEnter (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
  497. {
  498. if (monitor == NULL)
  499. return JVMTI_ERROR_INVALID_MONITOR;
  500. _Jv_MutexLock (&monitor->mutex);
  501. return JVMTI_ERROR_NONE;
  502. }
  503. static jvmtiError JNICALL
  504. _Jv_JVMTI_RawMonitorExit (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
  505. {
  506. if (monitor == NULL)
  507. return JVMTI_ERROR_INVALID_MONITOR;
  508. if (_Jv_MutexUnlock (&monitor->mutex))
  509. return JVMTI_ERROR_NOT_MONITOR_OWNER;
  510. return JVMTI_ERROR_NONE;
  511. }
  512. static jvmtiError JNICALL
  513. _Jv_JVMTI_RawMonitorWait (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor,
  514. jlong millis)
  515. {
  516. if (monitor == NULL)
  517. return JVMTI_ERROR_INVALID_MONITOR;
  518. int r = _Jv_CondWait (&monitor->condition, &monitor->mutex, millis, 0);
  519. if (r == _JV_NOT_OWNER)
  520. return JVMTI_ERROR_NOT_MONITOR_OWNER;
  521. if (r == _JV_INTERRUPTED)
  522. return JVMTI_ERROR_INTERRUPT;
  523. return JVMTI_ERROR_NONE;
  524. }
  525. static jvmtiError JNICALL
  526. _Jv_JVMTI_RawMonitorNotify (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
  527. {
  528. if (monitor == NULL)
  529. return JVMTI_ERROR_INVALID_MONITOR;
  530. if (_Jv_CondNotify (&monitor->condition, &monitor->mutex) == _JV_NOT_OWNER)
  531. return JVMTI_ERROR_NOT_MONITOR_OWNER;
  532. return JVMTI_ERROR_NONE;
  533. }
  534. static jvmtiError JNICALL
  535. _Jv_JVMTI_RawMonitorNotifyAll (MAYBE_UNUSED jvmtiEnv *env,
  536. jrawMonitorID monitor)
  537. {
  538. if (monitor == NULL)
  539. return JVMTI_ERROR_INVALID_MONITOR;
  540. if (_Jv_CondNotifyAll (&monitor->condition, &monitor->mutex)
  541. == _JV_NOT_OWNER)
  542. return JVMTI_ERROR_NOT_MONITOR_OWNER;
  543. return JVMTI_ERROR_NONE;
  544. }
  545. static jvmtiError JNICALL
  546. _Jv_JVMTI_SetBreakpoint (jvmtiEnv *env, jmethodID method, jlocation location)
  547. {
  548. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  549. using namespace gnu::gcj::jvmti;
  550. Breakpoint *bp
  551. = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
  552. location);
  553. if (bp == NULL)
  554. {
  555. jclass klass;
  556. jvmtiError err = env->GetMethodDeclaringClass (method, &klass);
  557. if (err != JVMTI_ERROR_NONE)
  558. return err;
  559. if (!_Jv_IsInterpretedClass (klass))
  560. return JVMTI_ERROR_INVALID_CLASS;
  561. _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
  562. if (base == NULL)
  563. return JVMTI_ERROR_INVALID_METHODID;
  564. jint flags;
  565. err = env->GetMethodModifiers (method, &flags);
  566. if (err != JVMTI_ERROR_NONE)
  567. return err;
  568. if (flags & java::lang::reflect::Modifier::NATIVE)
  569. return JVMTI_ERROR_NATIVE_METHOD;
  570. _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
  571. if (imeth->get_insn (location) == NULL)
  572. return JVMTI_ERROR_INVALID_LOCATION;
  573. // Now the breakpoint can be safely installed
  574. bp = BreakpointManager::newBreakpoint (reinterpret_cast<jlong> (method),
  575. location);
  576. }
  577. else
  578. {
  579. // Duplicate breakpoints are not permitted by JVMTI
  580. return JVMTI_ERROR_DUPLICATE;
  581. }
  582. return JVMTI_ERROR_NONE;
  583. }
  584. static jvmtiError JNICALL
  585. _Jv_JVMTI_ClearBreakpoint (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  586. jlocation location)
  587. {
  588. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  589. using namespace gnu::gcj::jvmti;
  590. Breakpoint *bp
  591. = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
  592. location);
  593. if (bp == NULL)
  594. return JVMTI_ERROR_NOT_FOUND;
  595. BreakpointManager::deleteBreakpoint (reinterpret_cast<jlong> (method), location);
  596. return JVMTI_ERROR_NONE;
  597. }
  598. static jvmtiError JNICALL
  599. _Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
  600. unsigned char **result)
  601. {
  602. ILLEGAL_ARGUMENT (size < 0);
  603. NULL_CHECK (result);
  604. if (size == 0)
  605. *result = NULL;
  606. else
  607. {
  608. *result = (unsigned char *) _Jv_MallocUnchecked (size);
  609. if (*result == NULL)
  610. return JVMTI_ERROR_OUT_OF_MEMORY;
  611. }
  612. return JVMTI_ERROR_NONE;
  613. }
  614. static jvmtiError JNICALL
  615. _Jv_JVMTI_Deallocate (MAYBE_UNUSED jvmtiEnv *env, unsigned char *mem)
  616. {
  617. if (mem != NULL)
  618. _Jv_Free (mem);
  619. return JVMTI_ERROR_NONE;
  620. }
  621. static jvmtiError JNICALL
  622. _Jv_JVMTI_GetClassStatus (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  623. jint *status_ptr)
  624. {
  625. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  626. NULL_CHECK (status_ptr);
  627. if (klass == NULL)
  628. return JVMTI_ERROR_INVALID_CLASS;
  629. if (klass->isArray ())
  630. *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
  631. else if (klass->isPrimitive ())
  632. *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
  633. else
  634. {
  635. jbyte state = _Jv_GetClassState (klass);
  636. *status_ptr = 0;
  637. if (state >= JV_STATE_LINKED)
  638. (*status_ptr) |= JVMTI_CLASS_STATUS_VERIFIED;
  639. if (state >= JV_STATE_PREPARED)
  640. (*status_ptr) |= JVMTI_CLASS_STATUS_PREPARED;
  641. if (state == JV_STATE_ERROR || state == JV_STATE_PHANTOM)
  642. (*status_ptr) |= JVMTI_CLASS_STATUS_ERROR;
  643. else if (state == JV_STATE_DONE)
  644. (*status_ptr) |= JVMTI_CLASS_STATUS_INITIALIZED;
  645. }
  646. return JVMTI_ERROR_NONE;
  647. }
  648. static jvmtiError JNICALL
  649. _Jv_JVMTI_GetClassModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  650. jint *mods)
  651. {
  652. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  653. // Don't bother checking KLASS' type.
  654. if (klass == NULL)
  655. return JVMTI_ERROR_INVALID_CLASS;
  656. NULL_CHECK (mods);
  657. *mods = klass->getModifiers();
  658. return JVMTI_ERROR_NONE;
  659. }
  660. static jvmtiError JNICALL
  661. _Jv_JVMTI_GetClassMethods (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  662. jint *count_ptr, jmethodID **methods_ptr)
  663. {
  664. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  665. // FIXME: capability can_maintain_original_method_order
  666. // Don't bother checking KLASS' type.
  667. if (klass == NULL)
  668. return JVMTI_ERROR_INVALID_CLASS;
  669. NULL_CHECK (count_ptr);
  670. NULL_CHECK (methods_ptr);
  671. *count_ptr = JvNumMethods(klass);
  672. *methods_ptr
  673. = (jmethodID *) _Jv_MallocUnchecked (*count_ptr * sizeof (jmethodID));
  674. if (*methods_ptr == NULL)
  675. return JVMTI_ERROR_OUT_OF_MEMORY;
  676. jmethodID start = JvGetFirstMethod (klass);
  677. for (jint i = 0; i < *count_ptr; ++i)
  678. // FIXME: correct?
  679. (*methods_ptr)[i] = start + i;
  680. return JVMTI_ERROR_NONE;
  681. }
  682. static jvmtiError JNICALL
  683. _Jv_JVMTI_IsInterface (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  684. jboolean *result)
  685. {
  686. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  687. if (klass == NULL)
  688. return JVMTI_ERROR_INVALID_CLASS;
  689. NULL_CHECK (result);
  690. *result = klass->isInterface();
  691. return JVMTI_ERROR_NONE;
  692. }
  693. static jvmtiError JNICALL
  694. _Jv_JVMTI_IsArrayClass (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  695. jboolean *result)
  696. {
  697. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  698. if (klass == NULL)
  699. return JVMTI_ERROR_INVALID_CLASS;
  700. NULL_CHECK (result);
  701. *result = klass->isArray();
  702. return JVMTI_ERROR_NONE;
  703. }
  704. static jvmtiError JNICALL
  705. _Jv_JVMTI_GetClassLoader (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  706. jobject *result)
  707. {
  708. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  709. if (klass == NULL)
  710. return JVMTI_ERROR_INVALID_CLASS;
  711. NULL_CHECK (result);
  712. *result = klass->getClassLoaderInternal();
  713. return JVMTI_ERROR_NONE;
  714. }
  715. static jvmtiError JNICALL
  716. _Jv_JVMTI_GetObjectHashCode (MAYBE_UNUSED jvmtiEnv *env, jobject obj,
  717. jint *result)
  718. {
  719. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  720. if (obj == NULL)
  721. return JVMTI_ERROR_INVALID_OBJECT;
  722. NULL_CHECK (result);
  723. *result = _Jv_HashCode (obj);
  724. return JVMTI_ERROR_NONE;
  725. }
  726. static jvmtiError JNICALL
  727. _Jv_JVMTI_GetFieldModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  728. jfieldID field, jint *result)
  729. {
  730. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  731. if (klass == NULL)
  732. return JVMTI_ERROR_INVALID_CLASS;
  733. if (field == NULL)
  734. return JVMTI_ERROR_INVALID_FIELDID;
  735. NULL_CHECK (result);
  736. *result = field->getModifiers();
  737. return JVMTI_ERROR_NONE;
  738. }
  739. static jvmtiError JNICALL
  740. _Jv_JVMTI_IsFieldSynthetic (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
  741. jfieldID field, jboolean *result)
  742. {
  743. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  744. if (klass == NULL)
  745. return JVMTI_ERROR_INVALID_CLASS;
  746. if (field == NULL)
  747. return JVMTI_ERROR_INVALID_FIELDID;
  748. NULL_CHECK (result);
  749. // FIXME: capability can_get_synthetic_attribute
  750. *result = ((field->getModifiers() & java::lang::reflect::Modifier::SYNTHETIC)
  751. != 0);
  752. return JVMTI_ERROR_NONE;
  753. }
  754. static jvmtiError JNICALL
  755. _Jv_JVMTI_GetMethodName (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  756. char **name_ptr, char **signature_ptr,
  757. char **generic_ptr)
  758. {
  759. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  760. if (method == NULL)
  761. return JVMTI_ERROR_INVALID_METHODID;
  762. if (name_ptr != NULL)
  763. {
  764. int len = static_cast<int> (method->name->len ());
  765. *name_ptr = (char *) _Jv_MallocUnchecked (len + 1);
  766. if (*name_ptr == NULL)
  767. return JVMTI_ERROR_OUT_OF_MEMORY;
  768. strncpy (*name_ptr, method->name->chars (), len);
  769. (*name_ptr)[len] = '\0';
  770. }
  771. if (signature_ptr != NULL)
  772. {
  773. int len = static_cast<int> (method->signature->len ());
  774. *signature_ptr = (char *) _Jv_MallocUnchecked (len + 1);
  775. if (*signature_ptr == NULL)
  776. {
  777. if (name_ptr != NULL)
  778. _Jv_Free (*name_ptr);
  779. return JVMTI_ERROR_OUT_OF_MEMORY;
  780. }
  781. strncpy (*signature_ptr, method->signature->chars (), len);
  782. (*signature_ptr)[len] = '\0';
  783. }
  784. if (generic_ptr != NULL)
  785. {
  786. *generic_ptr = NULL;
  787. }
  788. return JVMTI_ERROR_NONE;
  789. }
  790. static jvmtiError JNICALL
  791. _Jv_JVMTI_GetMethodModifiers (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  792. jint *result)
  793. {
  794. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  795. if (method == NULL)
  796. return JVMTI_ERROR_INVALID_METHODID;
  797. NULL_CHECK (result);
  798. // FIXME: mask off some internal bits...
  799. *result = method->accflags;
  800. return JVMTI_ERROR_NONE;
  801. }
  802. static jvmtiError JNICALL
  803. _Jv_JVMTI_GetLineNumberTable (jvmtiEnv *env, jmethodID method,
  804. jint *entry_count_ptr,
  805. jvmtiLineNumberEntry **table_ptr)
  806. {
  807. NULL_CHECK (entry_count_ptr);
  808. NULL_CHECK (table_ptr);
  809. jclass klass;
  810. jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  811. if (jerr != JVMTI_ERROR_NONE)
  812. return jerr;
  813. _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
  814. if (base == NULL)
  815. return JVMTI_ERROR_INVALID_METHODID;
  816. if (java::lang::reflect::Modifier::isNative (method->accflags)
  817. || !_Jv_IsInterpretedClass (klass))
  818. return JVMTI_ERROR_NATIVE_METHOD;
  819. _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
  820. jlong start, end;
  821. jintArray lines = NULL;
  822. jlongArray indices = NULL;
  823. imeth->get_line_table (start, end, lines, indices);
  824. if (lines == NULL)
  825. return JVMTI_ERROR_ABSENT_INFORMATION;
  826. jvmtiLineNumberEntry *table;
  827. jsize len = lines->length * sizeof (jvmtiLineNumberEntry);
  828. table = (jvmtiLineNumberEntry *) _Jv_MallocUnchecked (len);
  829. if (table == NULL)
  830. return JVMTI_ERROR_OUT_OF_MEMORY;
  831. jint *line = elements (lines);
  832. jlong *index = elements (indices);
  833. for (int i = 0; i < lines->length; ++i)
  834. {
  835. table[i].start_location = index[i];
  836. table[i].line_number = line[i];
  837. }
  838. *table_ptr = table;
  839. *entry_count_ptr = lines->length;
  840. return JVMTI_ERROR_NONE;
  841. }
  842. static jvmtiError JNICALL
  843. _Jv_JVMTI_GetLocalVariableTable (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  844. jint *num_locals,
  845. jvmtiLocalVariableEntry **locals)
  846. {
  847. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  848. NULL_CHECK (num_locals);
  849. NULL_CHECK (locals);
  850. CHECK_FOR_NATIVE_METHOD(method);
  851. jclass klass;
  852. jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  853. if (jerr != JVMTI_ERROR_NONE)
  854. return jerr;
  855. _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *>
  856. (_Jv_FindInterpreterMethod (klass, method));
  857. if (imeth == NULL)
  858. return JVMTI_ERROR_INVALID_METHODID;
  859. jerr = env->GetMaxLocals (method, num_locals);
  860. if (jerr != JVMTI_ERROR_NONE)
  861. return jerr;
  862. jerr = env->Allocate (static_cast<jlong>
  863. ((*num_locals) * sizeof (jvmtiLocalVariableEntry)),
  864. reinterpret_cast<unsigned char **> (locals));
  865. if (jerr != JVMTI_ERROR_NONE)
  866. return jerr;
  867. //the slot in the methods local_var_table to get
  868. int table_slot = 0;
  869. char *name;
  870. char *sig;
  871. char *generic_sig;
  872. while (table_slot < *num_locals
  873. && imeth->get_local_var_table (&name, &sig, &generic_sig,
  874. &((((*locals)[table_slot].start_location))),
  875. &((*locals)[table_slot].length),
  876. &((*locals)[table_slot].slot),
  877. table_slot)
  878. >= 0)
  879. {
  880. char **str_ptr = &(*locals)[table_slot].name;
  881. jerr = env->Allocate (static_cast<jlong> (strlen (name) + 1),
  882. reinterpret_cast<unsigned char **> (str_ptr));
  883. if (jerr != JVMTI_ERROR_NONE)
  884. return jerr;
  885. strcpy ((*locals)[table_slot].name, name);
  886. str_ptr = &(*locals)[table_slot].signature;
  887. jerr = env->Allocate (static_cast<jlong> (strlen (sig) + 1),
  888. reinterpret_cast<unsigned char **> (str_ptr));
  889. if (jerr != JVMTI_ERROR_NONE)
  890. return jerr;
  891. strcpy ((*locals)[table_slot].signature, sig);
  892. str_ptr = &(*locals)[table_slot].generic_signature;
  893. jerr = env->Allocate (static_cast<jlong> (strlen (generic_sig) + 1),
  894. reinterpret_cast<unsigned char **> (str_ptr));
  895. if (jerr != JVMTI_ERROR_NONE)
  896. return jerr;
  897. strcpy ((*locals)[table_slot].generic_signature, generic_sig);
  898. table_slot++;
  899. }
  900. if (table_slot == 0)
  901. return JVMTI_ERROR_ABSENT_INFORMATION;
  902. // If there are double or long variables in the table, the the table will be
  903. // smaller than the max number of slots, so correct for this here.
  904. if ((table_slot) < *num_locals)
  905. *num_locals = table_slot;
  906. return JVMTI_ERROR_NONE;
  907. }
  908. static jvmtiError JNICALL
  909. _Jv_JVMTI_IsMethodNative (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  910. jboolean *result)
  911. {
  912. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  913. if (method == NULL)
  914. return JVMTI_ERROR_INVALID_METHODID;
  915. NULL_CHECK (result);
  916. *result = ((method->accflags & java::lang::reflect::Modifier::NATIVE) != 0);
  917. return JVMTI_ERROR_NONE;
  918. }
  919. static jvmtiError JNICALL
  920. _Jv_JVMTI_IsMethodSynthetic (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
  921. jboolean *result)
  922. {
  923. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  924. if (method == NULL)
  925. return JVMTI_ERROR_INVALID_METHODID;
  926. NULL_CHECK (result);
  927. // FIXME capability can_get_synthetic_attribute
  928. *result = ((method->accflags & java::lang::reflect::Modifier::SYNTHETIC)
  929. != 0);
  930. return JVMTI_ERROR_NONE;
  931. }
  932. static jvmtiError JNICALL
  933. _Jv_JVMTI_GetMaxLocals (jvmtiEnv *env, jmethodID method, jint *max_locals)
  934. {
  935. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  936. NULL_CHECK (max_locals);
  937. CHECK_FOR_NATIVE_METHOD (method);
  938. jclass klass;
  939. jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  940. if (jerr != JVMTI_ERROR_NONE)
  941. return jerr;
  942. _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *>
  943. (_Jv_FindInterpreterMethod (klass, method));
  944. if (imeth == NULL)
  945. return JVMTI_ERROR_INVALID_METHODID;
  946. *max_locals = imeth->get_max_locals ();
  947. return JVMTI_ERROR_NONE;
  948. }
  949. static jvmtiError JNICALL
  950. _Jv_JVMTI_GetArgumentsSize (jvmtiEnv *env, jmethodID method, jint *size)
  951. {
  952. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  953. NULL_CHECK (size);
  954. CHECK_FOR_NATIVE_METHOD (method);
  955. jvmtiError jerr;
  956. char *sig;
  957. jint num_slots = 0;
  958. jerr = env->GetMethodName (method, NULL, &sig, NULL);
  959. if (jerr != JVMTI_ERROR_NONE)
  960. return jerr;
  961. // If the method is non-static add a slot for the "this" pointer.
  962. if ((method->accflags & java::lang::reflect::Modifier::STATIC) == 0)
  963. num_slots++;
  964. for (int i = 0; sig[i] != ')'; i++)
  965. {
  966. if (sig[i] == 'Z' || sig[i] == 'B' || sig[i] == 'C' || sig[i] == 'S'
  967. || sig[i] == 'I' || sig[i] == 'F')
  968. num_slots++;
  969. else if (sig[i] == 'J' || sig[i] == 'D')
  970. {
  971. // If this is an array of wide types it uses a single slot
  972. if (i > 0 && sig[i - 1] == '[')
  973. num_slots++;
  974. else
  975. num_slots += 2;
  976. }
  977. else if (sig[i] == 'L')
  978. {
  979. num_slots++;
  980. while (sig[i] != ';')
  981. i++;
  982. }
  983. }
  984. *size = num_slots;
  985. return JVMTI_ERROR_NONE;
  986. }
  987. static jvmtiError JNICALL
  988. _Jv_JVMTI_GetMethodDeclaringClass (MAYBE_UNUSED jvmtiEnv *env,
  989. jmethodID method,
  990. jclass *declaring_class_ptr)
  991. {
  992. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  993. NULL_CHECK (declaring_class_ptr);
  994. jclass klass = _Jv_GetMethodDeclaringClass (method);
  995. if (klass != NULL)
  996. {
  997. *declaring_class_ptr = klass;
  998. return JVMTI_ERROR_NONE;
  999. }
  1000. return JVMTI_ERROR_INVALID_METHODID;
  1001. }
  1002. static jvmtiError JNICALL
  1003. _Jv_JVMTI_GetClassLoaderClasses (MAYBE_UNUSED jvmtiEnv *env,
  1004. jobject init_loader,
  1005. jint *count_ptr,
  1006. jclass **result_ptr)
  1007. {
  1008. using namespace java::lang;
  1009. using namespace java::util;
  1010. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  1011. NULL_CHECK (count_ptr);
  1012. NULL_CHECK (result_ptr);
  1013. ClassLoader *loader = (ClassLoader *) init_loader;
  1014. if (loader == NULL)
  1015. loader = VMClassLoader::bootLoader;
  1016. Collection *values = loader->loadedClasses->values();
  1017. jobjectArray array = values->toArray();
  1018. *count_ptr = array->length;
  1019. jobject *elts = elements (array);
  1020. jclass *result
  1021. = (jclass *) _Jv_MallocUnchecked (*count_ptr * sizeof (jclass));
  1022. if (result == NULL)
  1023. return JVMTI_ERROR_OUT_OF_MEMORY;
  1024. // FIXME: JNI references...
  1025. memcpy (result, elts, *count_ptr * sizeof (jclass));
  1026. *result_ptr = result;
  1027. return JVMTI_ERROR_NONE;
  1028. }
  1029. static jvmtiError JNICALL
  1030. _Jv_JVMTI_GetStackTrace (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
  1031. jint start_depth, jint max_frames,
  1032. jvmtiFrameInfo *frames, jint *frame_count)
  1033. {
  1034. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  1035. ILLEGAL_ARGUMENT (max_frames < 0);
  1036. NULL_CHECK (frames);
  1037. NULL_CHECK (frame_count);
  1038. using namespace java::lang;
  1039. THREAD_DEFAULT_TO_CURRENT (thread);
  1040. THREAD_CHECK_VALID (thread);
  1041. THREAD_CHECK_IS_ALIVE (thread);
  1042. jvmtiError jerr = env->GetFrameCount (thread, frame_count);
  1043. if (jerr != JVMTI_ERROR_NONE)
  1044. return jerr;
  1045. // start_depth can be either a positive number, indicating the depth of the
  1046. // stack at which to begin the trace, or a negative number indicating the
  1047. // number of frames at the bottom of the stack to exclude. These checks
  1048. // ensure that it is a valid value in either case
  1049. ILLEGAL_ARGUMENT (start_depth >= (*frame_count));
  1050. ILLEGAL_ARGUMENT (start_depth < (-(*frame_count)));
  1051. _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  1052. // If start_depth is negative use this to determine at what depth to start
  1053. // the trace by adding it to the length of the call stack. This allows the
  1054. // use of the same frame "discarding" mechanism as for a positive start_depth
  1055. if (start_depth < 0)
  1056. start_depth = *frame_count + start_depth;
  1057. // If start_depth > 0 "remove" start_depth frames from the beginning
  1058. // of the stack before beginning the trace by moving along the frame list.
  1059. while (start_depth > 0)
  1060. {
  1061. frame = frame->next;
  1062. start_depth--;
  1063. (*frame_count)--;
  1064. }
  1065. // Now check to see if the array supplied by the agent is large enough to
  1066. // hold frame_count frames, after adjustment for start_depth.
  1067. if ((*frame_count) > max_frames)
  1068. (*frame_count) = max_frames;
  1069. for (int i = 0; i < (*frame_count); i++)
  1070. {
  1071. frames[i].method = frame->self->get_method ();
  1072. // Set the location in the frame, native frames have location = -1
  1073. if (frame->frame_type == frame_interpreter)
  1074. {
  1075. _Jv_InterpMethod *imeth
  1076. = static_cast<_Jv_InterpMethod *> (frame->self);
  1077. _Jv_InterpFrame *interp_frame
  1078. = static_cast<_Jv_InterpFrame *> (frame);
  1079. frames[i].location = imeth->insn_index (interp_frame->get_pc ());
  1080. }
  1081. else
  1082. frames[i].location = -1;
  1083. frame = frame->next;
  1084. }
  1085. return JVMTI_ERROR_NONE;
  1086. }
  1087. static jvmtiError JNICALL
  1088. _Jv_JVMTI_ForceGarbageCollection (MAYBE_UNUSED jvmtiEnv *env)
  1089. {
  1090. REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  1091. _Jv_RunGC();
  1092. return JVMTI_ERROR_NONE;
  1093. }
  1094. static jvmtiError JNICALL
  1095. _Jv_JVMTI_SetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
  1096. const jniNativeInterface *function_table)
  1097. {
  1098. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  1099. NULL_CHECK (function_table);
  1100. memcpy (&_Jv_JNIFunctions, function_table, sizeof (jniNativeInterface));
  1101. return JVMTI_ERROR_NONE;
  1102. }
  1103. static jvmtiError JNICALL
  1104. _Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
  1105. jniNativeInterface **function_table)
  1106. {
  1107. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  1108. NULL_CHECK (function_table);
  1109. *function_table
  1110. = (jniNativeInterface *) _Jv_MallocUnchecked (sizeof (jniNativeInterface));
  1111. if (*function_table == NULL)
  1112. return JVMTI_ERROR_OUT_OF_MEMORY;
  1113. memcpy (*function_table, &_Jv_JNIFunctions, sizeof (jniNativeInterface));
  1114. return JVMTI_ERROR_NONE;
  1115. }
  1116. static jvmtiError JNICALL
  1117. _Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
  1118. {
  1119. NULL_CHECK (env);
  1120. if (_jvmtiEnvironments == NULL)
  1121. return JVMTI_ERROR_INVALID_ENVIRONMENT;
  1122. else
  1123. {
  1124. _envListLock->writeLock ()->lock ();
  1125. if (_jvmtiEnvironments->env == env)
  1126. {
  1127. struct jvmti_env_list *next = _jvmtiEnvironments->next;
  1128. _Jv_Free (_jvmtiEnvironments);
  1129. _jvmtiEnvironments = next;
  1130. }
  1131. else
  1132. {
  1133. struct jvmti_env_list *e = _jvmtiEnvironments;
  1134. while (e->next != NULL && e->next->env != env)
  1135. e = e->next;
  1136. if (e->next == NULL)
  1137. {
  1138. _envListLock->writeLock ()->unlock ();
  1139. return JVMTI_ERROR_INVALID_ENVIRONMENT;
  1140. }
  1141. struct jvmti_env_list *next = e->next->next;
  1142. _Jv_Free (e->next);
  1143. e->next = next;
  1144. }
  1145. _envListLock->writeLock ()->unlock ();
  1146. }
  1147. _Jv_Free (env);
  1148. check_enabled_events ();
  1149. return JVMTI_ERROR_NONE;
  1150. }
  1151. static jvmtiError JNICALL
  1152. _Jv_JVMTI_GetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
  1153. char **result)
  1154. {
  1155. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  1156. NULL_CHECK (property);
  1157. NULL_CHECK (result);
  1158. jstring name = JvNewStringUTF(property);
  1159. jstring result_str = gnu::classpath::SystemProperties::getProperty(name);
  1160. if (result_str == NULL)
  1161. return JVMTI_ERROR_NOT_AVAILABLE;
  1162. int len = JvGetStringUTFLength (result_str);
  1163. *result = (char *) _Jv_MallocUnchecked (len + 1);
  1164. if (*result == NULL)
  1165. return JVMTI_ERROR_OUT_OF_MEMORY;
  1166. JvGetStringUTFRegion (result_str, 0, result_str->length(), *result);
  1167. (*result)[len] = '\0';
  1168. return JVMTI_ERROR_NONE;
  1169. }
  1170. static jvmtiError JNICALL
  1171. _Jv_JVMTI_SetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
  1172. const char *value)
  1173. {
  1174. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
  1175. NULL_CHECK (property);
  1176. if (value == NULL)
  1177. {
  1178. // FIXME: When would a property not be writeable?
  1179. return JVMTI_ERROR_NONE;
  1180. }
  1181. jstring prop_str = JvNewStringUTF(property);
  1182. jstring value_str = JvNewStringUTF(value);
  1183. gnu::classpath::SystemProperties::setProperty(prop_str, value_str);
  1184. return JVMTI_ERROR_NONE;
  1185. }
  1186. static jvmtiError JNICALL
  1187. _Jv_JVMTI_GetTime (MAYBE_UNUSED jvmtiEnv *env, jlong *nanos_ptr)
  1188. {
  1189. NULL_CHECK (nanos_ptr);
  1190. *nanos_ptr = _Jv_platform_nanotime();
  1191. return JVMTI_ERROR_NONE;
  1192. }
  1193. static jvmtiError JNICALL
  1194. _Jv_JVMTI_GetAvailableProcessors (MAYBE_UNUSED jvmtiEnv *env,
  1195. jint *nprocessors_ptr)
  1196. {
  1197. NULL_CHECK (nprocessors_ptr);
  1198. #ifdef _SC_NPROCESSORS_ONLN
  1199. *nprocessors_ptr = sysconf(_SC_NPROCESSORS_ONLN);
  1200. #else
  1201. *nprocessors_ptr = 1;
  1202. #endif
  1203. return JVMTI_ERROR_NONE;
  1204. }
  1205. static jvmtiError JNICALL
  1206. _Jv_JVMTI_AddToBootstrapClassLoaderSearch (MAYBE_UNUSED jvmtiEnv *env,
  1207. const char *segment)
  1208. {
  1209. using namespace java::lang;
  1210. using namespace java::net;
  1211. using namespace gnu::gcj::runtime;
  1212. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
  1213. NULL_CHECK (segment);
  1214. jstring str_segment = JvNewStringUTF(segment);
  1215. URL *url;
  1216. try
  1217. {
  1218. url = new URL(JvNewStringUTF("file"), NULL, str_segment);
  1219. }
  1220. catch (jthrowable ignore)
  1221. {
  1222. return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  1223. }
  1224. BootClassLoader *loader = VMClassLoader::bootLoader;
  1225. // Don't call this too early.
  1226. // assert (loader != NULL);
  1227. loader->addURL(url);
  1228. return JVMTI_ERROR_NONE;
  1229. }
  1230. static jvmtiError JNICALL
  1231. _Jv_JVMTI_SetVerboseFlag (MAYBE_UNUSED jvmtiEnv *env, jvmtiVerboseFlag flag,
  1232. jboolean value)
  1233. {
  1234. switch (flag)
  1235. {
  1236. case JVMTI_VERBOSE_OTHER:
  1237. case JVMTI_VERBOSE_GC:
  1238. case JVMTI_VERBOSE_JNI:
  1239. // Ignore.
  1240. break;
  1241. case JVMTI_VERBOSE_CLASS:
  1242. gcj::verbose_class_flag = value;
  1243. break;
  1244. default:
  1245. return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  1246. }
  1247. return JVMTI_ERROR_NONE;
  1248. }
  1249. static jvmtiError JNICALL
  1250. _Jv_JVMTI_GetObjectSize (MAYBE_UNUSED jvmtiEnv *env, jobject object,
  1251. jlong *result)
  1252. {
  1253. REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  1254. if (object == NULL)
  1255. return JVMTI_ERROR_INVALID_OBJECT;
  1256. NULL_CHECK (result);
  1257. jclass klass = object->getClass();
  1258. if (klass->isArray())
  1259. {
  1260. jclass comp = klass->getComponentType();
  1261. jint base
  1262. = (jint) (_Jv_uintptr_t) _Jv_GetArrayElementFromElementType(NULL,
  1263. klass->getComponentType());
  1264. // FIXME: correct for primitive types?
  1265. jint compSize = comp->size();
  1266. __JArray *array = (__JArray *) object;
  1267. *result = base + array->length * compSize;
  1268. }
  1269. else
  1270. {
  1271. // Note that if OBJECT is a String then it may (if
  1272. // str->data==str) take more space. Do we care?
  1273. *result = klass->size();
  1274. }
  1275. return JVMTI_ERROR_NONE;
  1276. }
  1277. /* An event is enabled only if it has both an event handler
  1278. and it is enabled in the environment. */
  1279. static void
  1280. check_enabled_event (jvmtiEvent type)
  1281. {
  1282. bool *enabled;
  1283. int offset;
  1284. #define GET_OFFSET(Event) \
  1285. do \
  1286. { \
  1287. enabled = &JVMTI::Event; \
  1288. offset = offsetof (jvmtiEventCallbacks, Event); \
  1289. } \
  1290. while (0)
  1291. switch (type)
  1292. {
  1293. case JVMTI_EVENT_VM_INIT:
  1294. GET_OFFSET (VMInit);
  1295. break;
  1296. case JVMTI_EVENT_VM_DEATH:
  1297. GET_OFFSET (VMDeath);
  1298. break;
  1299. case JVMTI_EVENT_THREAD_START:
  1300. GET_OFFSET (ThreadStart);
  1301. break;
  1302. case JVMTI_EVENT_THREAD_END:
  1303. GET_OFFSET (ThreadEnd);
  1304. break;
  1305. case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
  1306. GET_OFFSET (ClassFileLoadHook);
  1307. break;
  1308. case JVMTI_EVENT_CLASS_LOAD:
  1309. GET_OFFSET (ClassLoad);
  1310. break;
  1311. case JVMTI_EVENT_CLASS_PREPARE:
  1312. GET_OFFSET (ClassPrepare);
  1313. break;
  1314. case JVMTI_EVENT_VM_START:
  1315. GET_OFFSET (VMStart);
  1316. break;
  1317. case JVMTI_EVENT_EXCEPTION:
  1318. GET_OFFSET (Exception);
  1319. break;
  1320. case JVMTI_EVENT_EXCEPTION_CATCH:
  1321. GET_OFFSET (ExceptionCatch);
  1322. break;
  1323. case JVMTI_EVENT_SINGLE_STEP:
  1324. GET_OFFSET (SingleStep);
  1325. break;
  1326. case JVMTI_EVENT_FRAME_POP:
  1327. GET_OFFSET (FramePop);
  1328. break;
  1329. case JVMTI_EVENT_BREAKPOINT:
  1330. GET_OFFSET (Breakpoint);
  1331. break;
  1332. case JVMTI_EVENT_FIELD_ACCESS:
  1333. GET_OFFSET (FieldAccess);
  1334. break;
  1335. case JVMTI_EVENT_FIELD_MODIFICATION:
  1336. GET_OFFSET (FieldModification);
  1337. break;
  1338. case JVMTI_EVENT_METHOD_ENTRY:
  1339. GET_OFFSET (MethodEntry);
  1340. break;
  1341. case JVMTI_EVENT_METHOD_EXIT:
  1342. GET_OFFSET (MethodExit);
  1343. break;
  1344. case JVMTI_EVENT_NATIVE_METHOD_BIND:
  1345. GET_OFFSET (NativeMethodBind);
  1346. break;
  1347. case JVMTI_EVENT_COMPILED_METHOD_LOAD:
  1348. GET_OFFSET (CompiledMethodLoad);
  1349. break;
  1350. case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
  1351. GET_OFFSET (CompiledMethodUnload);
  1352. break;
  1353. case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
  1354. GET_OFFSET (DynamicCodeGenerated);
  1355. break;
  1356. case JVMTI_EVENT_DATA_DUMP_REQUEST:
  1357. GET_OFFSET (DataDumpRequest);
  1358. break;
  1359. case JVMTI_EVENT_MONITOR_WAIT:
  1360. GET_OFFSET (MonitorWait);
  1361. break;
  1362. case JVMTI_EVENT_MONITOR_WAITED:
  1363. GET_OFFSET (MonitorWaited);
  1364. break;
  1365. case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
  1366. GET_OFFSET (MonitorContendedEnter);
  1367. break;
  1368. case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
  1369. GET_OFFSET (MonitorContendedEntered);
  1370. break;
  1371. case JVMTI_EVENT_GARBAGE_COLLECTION_START:
  1372. GET_OFFSET (GarbageCollectionStart);
  1373. break;
  1374. case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
  1375. GET_OFFSET (GarbageCollectionFinish);
  1376. break;
  1377. case JVMTI_EVENT_OBJECT_FREE:
  1378. GET_OFFSET (ObjectFree);
  1379. break;
  1380. case JVMTI_EVENT_VM_OBJECT_ALLOC:
  1381. GET_OFFSET (VMObjectAlloc);
  1382. break;
  1383. default:
  1384. fprintf (stderr,
  1385. "libgcj: check_enabled_event for unknown JVMTI event (%d)\n",
  1386. (int) type);
  1387. return;
  1388. }
  1389. #undef GET_OFFSET
  1390. int index = EVENT_INDEX (type); // safe since caller checks this
  1391. if (_jvmtiEnvironments != NULL)
  1392. {
  1393. _envListLock->readLock ()->lock ();
  1394. struct jvmti_env_list *e;
  1395. FOREACH_ENVIRONMENT (e)
  1396. {
  1397. char *addr
  1398. = reinterpret_cast<char *> (&e->env->callbacks) + offset;
  1399. void **callback = reinterpret_cast<void **> (addr);
  1400. if (e->env->enabled[index] && *callback != NULL)
  1401. {
  1402. *enabled = true;
  1403. _envListLock->readLock ()->unlock ();
  1404. return;
  1405. }
  1406. }
  1407. _envListLock->readLock ()->unlock ();
  1408. }
  1409. *enabled = false;
  1410. }
  1411. static void
  1412. check_enabled_events ()
  1413. {
  1414. check_enabled_event (JVMTI_EVENT_VM_INIT);
  1415. check_enabled_event (JVMTI_EVENT_VM_DEATH);
  1416. check_enabled_event (JVMTI_EVENT_THREAD_START);
  1417. check_enabled_event (JVMTI_EVENT_THREAD_END);
  1418. check_enabled_event (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK);
  1419. check_enabled_event (JVMTI_EVENT_CLASS_LOAD);
  1420. check_enabled_event (JVMTI_EVENT_CLASS_PREPARE);
  1421. check_enabled_event (JVMTI_EVENT_VM_START);
  1422. check_enabled_event (JVMTI_EVENT_EXCEPTION);
  1423. check_enabled_event (JVMTI_EVENT_EXCEPTION_CATCH);
  1424. check_enabled_event (JVMTI_EVENT_SINGLE_STEP);
  1425. check_enabled_event (JVMTI_EVENT_FRAME_POP);
  1426. check_enabled_event (JVMTI_EVENT_BREAKPOINT);
  1427. check_enabled_event (JVMTI_EVENT_FIELD_ACCESS);
  1428. check_enabled_event (JVMTI_EVENT_FIELD_MODIFICATION);
  1429. check_enabled_event (JVMTI_EVENT_METHOD_ENTRY);
  1430. check_enabled_event (JVMTI_EVENT_METHOD_EXIT);
  1431. check_enabled_event (JVMTI_EVENT_NATIVE_METHOD_BIND);
  1432. check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_LOAD);
  1433. check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_UNLOAD);
  1434. check_enabled_event (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
  1435. check_enabled_event (JVMTI_EVENT_DATA_DUMP_REQUEST);
  1436. check_enabled_event (JVMTI_EVENT_MONITOR_WAIT);
  1437. check_enabled_event (JVMTI_EVENT_MONITOR_WAITED);
  1438. check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTER);
  1439. check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED);
  1440. check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_START);
  1441. check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH);
  1442. check_enabled_event (JVMTI_EVENT_OBJECT_FREE);
  1443. check_enabled_event (JVMTI_EVENT_VM_OBJECT_ALLOC);
  1444. }
  1445. static jvmtiError JNICALL
  1446. _Jv_JVMTI_SetEventNotificationMode (jvmtiEnv *env, jvmtiEventMode mode,
  1447. jvmtiEvent type, jthread event_thread, ...)
  1448. {
  1449. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  1450. if (event_thread != NULL)
  1451. {
  1452. THREAD_CHECK_VALID (event_thread);
  1453. THREAD_CHECK_IS_ALIVE (event_thread);
  1454. }
  1455. bool enabled;
  1456. switch (mode)
  1457. {
  1458. case JVMTI_DISABLE:
  1459. enabled = false;
  1460. break;
  1461. case JVMTI_ENABLE:
  1462. enabled = true;
  1463. break;
  1464. default:
  1465. return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  1466. }
  1467. switch (type)
  1468. {
  1469. case JVMTI_EVENT_VM_INIT:
  1470. case JVMTI_EVENT_VM_DEATH:
  1471. case JVMTI_EVENT_THREAD_START:
  1472. case JVMTI_EVENT_VM_START:
  1473. case JVMTI_EVENT_COMPILED_METHOD_LOAD:
  1474. case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
  1475. case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
  1476. case JVMTI_EVENT_DATA_DUMP_REQUEST:
  1477. ILLEGAL_ARGUMENT (event_thread != NULL);
  1478. break;
  1479. case JVMTI_EVENT_THREAD_END:
  1480. case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
  1481. case JVMTI_EVENT_CLASS_LOAD:
  1482. case JVMTI_EVENT_CLASS_PREPARE:
  1483. case JVMTI_EVENT_EXCEPTION:
  1484. case JVMTI_EVENT_EXCEPTION_CATCH:
  1485. case JVMTI_EVENT_SINGLE_STEP:
  1486. case JVMTI_EVENT_FRAME_POP:
  1487. case JVMTI_EVENT_BREAKPOINT:
  1488. case JVMTI_EVENT_FIELD_ACCESS:
  1489. case JVMTI_EVENT_FIELD_MODIFICATION:
  1490. case JVMTI_EVENT_METHOD_ENTRY:
  1491. case JVMTI_EVENT_METHOD_EXIT:
  1492. case JVMTI_EVENT_NATIVE_METHOD_BIND:
  1493. case JVMTI_EVENT_MONITOR_WAIT:
  1494. case JVMTI_EVENT_MONITOR_WAITED:
  1495. case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
  1496. case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
  1497. case JVMTI_EVENT_GARBAGE_COLLECTION_START:
  1498. case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
  1499. case JVMTI_EVENT_OBJECT_FREE:
  1500. case JVMTI_EVENT_VM_OBJECT_ALLOC:
  1501. break;
  1502. default:
  1503. return JVMTI_ERROR_INVALID_EVENT_TYPE;
  1504. }
  1505. env->thread[EVENT_INDEX(type)] = event_thread;
  1506. env->enabled[EVENT_INDEX(type)] = enabled;
  1507. check_enabled_event (type);
  1508. return JVMTI_ERROR_NONE;
  1509. }
  1510. static jvmtiError JNICALL
  1511. _Jv_JVMTI_SetEventCallbacks (jvmtiEnv *env,
  1512. const jvmtiEventCallbacks *callbacks,
  1513. jint size_of_callbacks)
  1514. {
  1515. REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  1516. ILLEGAL_ARGUMENT (size_of_callbacks < 0);
  1517. // Copy the list of callbacks into the environment
  1518. memcpy (&env->callbacks, callbacks, sizeof (jvmtiEventCallbacks));
  1519. /* Check which events are now enabeld (JVMTI makes no requirements
  1520. about the order in which SetEventCallbacks and SetEventNotifications
  1521. are called. So we must check all events here. */
  1522. check_enabled_events ();
  1523. return JVMTI_ERROR_NONE;
  1524. }
  1525. static jvmtiError JNICALL
  1526. _Jv_JVMTI_GetErrorName (MAYBE_UNUSED jvmtiEnv *env, jvmtiError error,
  1527. char **name_ptr)
  1528. {
  1529. NULL_CHECK (name_ptr);
  1530. const char *name;
  1531. switch (error)
  1532. {
  1533. case JVMTI_ERROR_NONE:
  1534. name = "none";
  1535. break;
  1536. case JVMTI_ERROR_NULL_POINTER:
  1537. name = "null pointer";
  1538. break;
  1539. case JVMTI_ERROR_OUT_OF_MEMORY:
  1540. name = "out of memory";
  1541. break;
  1542. case JVMTI_ERROR_ACCESS_DENIED:
  1543. name = "access denied";
  1544. break;
  1545. case JVMTI_ERROR_WRONG_PHASE:
  1546. name = "wrong phase";
  1547. break;
  1548. case JVMTI_ERROR_INTERNAL:
  1549. name = "internal error";
  1550. break;
  1551. case JVMTI_ERROR_UNATTACHED_THREAD:
  1552. name = "unattached thread";
  1553. break;
  1554. case JVMTI_ERROR_INVALID_ENVIRONMENT:
  1555. name = "invalid environment";
  1556. break;
  1557. case JVMTI_ERROR_INVALID_PRIORITY:
  1558. name = "invalid priority";
  1559. break;
  1560. case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
  1561. name = "thread not suspended";
  1562. break;
  1563. case JVMTI_ERROR_THREAD_SUSPENDED:
  1564. name = "thread suspended";
  1565. break;
  1566. case JVMTI_ERROR_THREAD_NOT_ALIVE:
  1567. name = "thread not alive";
  1568. break;
  1569. case JVMTI_ERROR_CLASS_NOT_PREPARED:
  1570. name = "class not prepared";
  1571. break;
  1572. case JVMTI_ERROR_NO_MORE_FRAMES:
  1573. name = "no more frames";
  1574. break;
  1575. case JVMTI_ERROR_OPAQUE_FRAME:
  1576. name = "opaque frame";
  1577. break;
  1578. case JVMTI_ERROR_DUPLICATE:
  1579. name = "duplicate";
  1580. break;
  1581. case JVMTI_ERROR_NOT_FOUND:
  1582. name = "not found";
  1583. break;
  1584. case JVMTI_ERROR_NOT_MONITOR_OWNER:
  1585. name = "not monitor owner";
  1586. break;
  1587. case JVMTI_ERROR_INTERRUPT:
  1588. name = "interrupted";
  1589. break;
  1590. case JVMTI_ERROR_UNMODIFIABLE_CLASS:
  1591. name = "unmodifiable class";
  1592. break;
  1593. case JVMTI_ERROR_NOT_AVAILABLE:
  1594. name = "not available";
  1595. break;
  1596. case JVMTI_ERROR_ABSENT_INFORMATION:
  1597. name = "absent information";
  1598. break;
  1599. case JVMTI_ERROR_INVALID_EVENT_TYPE:
  1600. name = "invalid event type";
  1601. break;
  1602. case JVMTI_ERROR_NATIVE_METHOD:
  1603. name = "native method";
  1604. break;
  1605. case JVMTI_ERROR_INVALID_THREAD:
  1606. name = "invalid thread";
  1607. break;
  1608. case JVMTI_ERROR_INVALID_THREAD_GROUP:
  1609. name = "invalid thread group";
  1610. break;
  1611. case JVMTI_ERROR_INVALID_OBJECT:
  1612. name = "invalid object";
  1613. break;
  1614. case JVMTI_ERROR_INVALID_CLASS:
  1615. name = "invalid class";
  1616. break;
  1617. case JVMTI_ERROR_INVALID_METHODID:
  1618. name = "invalid method ID";
  1619. break;
  1620. case JVMTI_ERROR_INVALID_LOCATION:
  1621. name = "invalid location";
  1622. break;
  1623. case JVMTI_ERROR_INVALID_FIELDID:
  1624. name = "invalid field ID";
  1625. break;
  1626. case JVMTI_ERROR_TYPE_MISMATCH:
  1627. name = "type mismatch";
  1628. break;
  1629. case JVMTI_ERROR_INVALID_SLOT:
  1630. name = "invalid slot";
  1631. break;
  1632. case JVMTI_ERROR_INVALID_MONITOR:
  1633. name = "invalid monitor";
  1634. break;
  1635. case JVMTI_ERROR_INVALID_CLASS_FORMAT:
  1636. name = "invalid class format";
  1637. break;
  1638. case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
  1639. name = "circular class definition";
  1640. break;
  1641. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
  1642. name = "unsupported redefinition: method added";
  1643. break;
  1644. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
  1645. name = "unsupported redefinition: schema changed";
  1646. break;
  1647. case JVMTI_ERROR_INVALID_TYPESTATE:
  1648. name = "invalid type state";
  1649. break;
  1650. case JVMTI_ERROR_FAILS_VERIFICATION:
  1651. name = "fails verification";
  1652. break;
  1653. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
  1654. name = "unsupported redefinition: hierarchy changed";
  1655. break;
  1656. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
  1657. name = "unsupported redefinition: method deleted";
  1658. break;
  1659. case JVMTI_ERROR_UNSUPPORTED_VERSION:
  1660. name = "unsupported version";
  1661. break;
  1662. case JVMTI_ERROR_NAMES_DONT_MATCH:
  1663. name = "names do not match";
  1664. break;
  1665. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
  1666. name = "unsupported redefinition: class modifiers changed";
  1667. break;
  1668. case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
  1669. name = "unsupported redefinition: method modifiers changed";
  1670. break;
  1671. case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
  1672. name = "must possess capability";
  1673. break;
  1674. case JVMTI_ERROR_ILLEGAL_ARGUMENT:
  1675. name = "illegal argument";
  1676. break;
  1677. default:
  1678. return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  1679. }
  1680. *name_ptr = (char *) _Jv_MallocUnchecked (strlen (name) + 1);
  1681. if (*name_ptr == NULL)
  1682. return JVMTI_ERROR_OUT_OF_MEMORY;
  1683. strcpy (*name_ptr, name);
  1684. return JVMTI_ERROR_NONE;
  1685. }
  1686. #define RESERVED NULL
  1687. #define UNIMPLEMENTED NULL
  1688. struct _Jv_jvmtiEnv _Jv_JVMTI_Interface =
  1689. {
  1690. RESERVED, // reserved1
  1691. _Jv_JVMTI_SetEventNotificationMode, // SetEventNotificationMode
  1692. RESERVED, // reserved3
  1693. _Jv_JVMTI_GetAllThreads, // GetAllThreads
  1694. _Jv_JVMTI_SuspendThread, // SuspendThread
  1695. _Jv_JVMTI_ResumeThread, // ResumeThread
  1696. UNIMPLEMENTED, // StopThread
  1697. _Jv_JVMTI_InterruptThread, // InterruptThread
  1698. UNIMPLEMENTED, // GetThreadInfo
  1699. UNIMPLEMENTED, // GetOwnedMonitorInfo
  1700. UNIMPLEMENTED, // GetCurrentContendedMonitor
  1701. UNIMPLEMENTED, // RunAgentThread
  1702. UNIMPLEMENTED, // GetTopThreadGroups
  1703. UNIMPLEMENTED, // GetThreadGroupInfo
  1704. UNIMPLEMENTED, // GetThreadGroupChildren
  1705. _Jv_JVMTI_GetFrameCount, // GetFrameCount
  1706. _Jv_JVMTI_GetThreadState, // GetThreadState
  1707. RESERVED, // reserved18
  1708. UNIMPLEMENTED, // GetFrameLocation
  1709. UNIMPLEMENTED, // NotifyPopFrame
  1710. _Jv_JVMTI_GetLocalObject, // GetLocalObject
  1711. _Jv_JVMTI_GetLocalInt, // GetLocalInt
  1712. _Jv_JVMTI_GetLocalLong, // GetLocalLong
  1713. _Jv_JVMTI_GetLocalFloat, // GetLocalFloat
  1714. _Jv_JVMTI_GetLocalDouble, // GetLocalDouble
  1715. _Jv_JVMTI_SetLocalObject, // SetLocalObject
  1716. _Jv_JVMTI_SetLocalInt, // SetLocalInt
  1717. _Jv_JVMTI_SetLocalLong, // SetLocalLong
  1718. _Jv_JVMTI_SetLocalFloat, // SetLocalFloat
  1719. _Jv_JVMTI_SetLocalDouble, // SetLocalDouble
  1720. _Jv_JVMTI_CreateRawMonitor, // CreateRawMonitor
  1721. _Jv_JVMTI_DestroyRawMonitor, // DestroyRawMonitor
  1722. _Jv_JVMTI_RawMonitorEnter, // RawMonitorEnter
  1723. _Jv_JVMTI_RawMonitorExit, // RawMonitorExit
  1724. _Jv_JVMTI_RawMonitorWait, // RawMonitorWait
  1725. _Jv_JVMTI_RawMonitorNotify, // RawMonitorNotify
  1726. _Jv_JVMTI_RawMonitorNotifyAll, // RawMonitorNotifyAll
  1727. _Jv_JVMTI_SetBreakpoint, // SetBreakpoint
  1728. _Jv_JVMTI_ClearBreakpoint, // ClearBreakpoint
  1729. RESERVED, // reserved40
  1730. UNIMPLEMENTED, // SetFieldAccessWatch
  1731. UNIMPLEMENTED, // ClearFieldAccessWatch
  1732. UNIMPLEMENTED, // SetFieldModificationWatch
  1733. UNIMPLEMENTED, // ClearFieldModificationWatch
  1734. RESERVED, // reserved45
  1735. _Jv_JVMTI_Allocate, // Allocate
  1736. _Jv_JVMTI_Deallocate, // Deallocate
  1737. UNIMPLEMENTED, // GetClassSignature
  1738. _Jv_JVMTI_GetClassStatus, // GetClassStatus
  1739. UNIMPLEMENTED, // GetSourceFileName
  1740. _Jv_JVMTI_GetClassModifiers, // GetClassModifiers
  1741. _Jv_JVMTI_GetClassMethods, // GetClassMethods
  1742. UNIMPLEMENTED, // GetClassFields
  1743. UNIMPLEMENTED, // GetImplementedInterfaces
  1744. _Jv_JVMTI_IsInterface, // IsInterface
  1745. _Jv_JVMTI_IsArrayClass, // IsArrayClass
  1746. _Jv_JVMTI_GetClassLoader, // GetClassLoader
  1747. _Jv_JVMTI_GetObjectHashCode, // GetObjectHashCode
  1748. UNIMPLEMENTED, // GetObjectMonitorUsage
  1749. UNIMPLEMENTED, // GetFieldName
  1750. UNIMPLEMENTED, // GetFieldDeclaringClass
  1751. _Jv_JVMTI_GetFieldModifiers, // GetFieldModifiers
  1752. _Jv_JVMTI_IsFieldSynthetic, // IsFieldSynthetic
  1753. _Jv_JVMTI_GetMethodName, // GetMethodName
  1754. _Jv_JVMTI_GetMethodDeclaringClass, // GetMethodDeclaringClass
  1755. _Jv_JVMTI_GetMethodModifiers, // GetMethodModifers
  1756. RESERVED, // reserved67
  1757. _Jv_JVMTI_GetMaxLocals, // GetMaxLocals
  1758. _Jv_JVMTI_GetArgumentsSize, // GetArgumentsSize
  1759. _Jv_JVMTI_GetLineNumberTable, // GetLineNumberTable
  1760. UNIMPLEMENTED, // GetMethodLocation
  1761. _Jv_JVMTI_GetLocalVariableTable, // GetLocalVariableTable
  1762. RESERVED, // reserved73
  1763. RESERVED, // reserved74
  1764. UNIMPLEMENTED, // GetBytecodes
  1765. _Jv_JVMTI_IsMethodNative, // IsMethodNative
  1766. _Jv_JVMTI_IsMethodSynthetic, // IsMethodSynthetic
  1767. UNIMPLEMENTED, // GetLoadedClasses
  1768. _Jv_JVMTI_GetClassLoaderClasses, // GetClassLoaderClasses
  1769. UNIMPLEMENTED, // PopFrame
  1770. RESERVED, // reserved81
  1771. RESERVED, // reserved82
  1772. RESERVED, // reserved83
  1773. RESERVED, // reserved84
  1774. RESERVED, // reserved85
  1775. RESERVED, // reserved86
  1776. UNIMPLEMENTED, // RedefineClasses
  1777. UNIMPLEMENTED, // GetVersionNumber
  1778. UNIMPLEMENTED, // GetCapabilities
  1779. UNIMPLEMENTED, // GetSourceDebugExtension
  1780. UNIMPLEMENTED, // IsMethodObsolete
  1781. UNIMPLEMENTED, // SuspendThreadList
  1782. UNIMPLEMENTED, // ResumeThreadList
  1783. RESERVED, // reserved94
  1784. RESERVED, // reserved95
  1785. RESERVED, // reserved96
  1786. RESERVED, // reserved97
  1787. RESERVED, // reserved98
  1788. RESERVED, // reserved99
  1789. UNIMPLEMENTED, // GetAllStackTraces
  1790. UNIMPLEMENTED, // GetThreadListStackTraces
  1791. UNIMPLEMENTED, // GetThreadLocalStorage
  1792. UNIMPLEMENTED, // SetThreadLocalStorage
  1793. _Jv_JVMTI_GetStackTrace, // GetStackTrace
  1794. RESERVED, // reserved105
  1795. UNIMPLEMENTED, // GetTag
  1796. UNIMPLEMENTED, // SetTag
  1797. _Jv_JVMTI_ForceGarbageCollection, // ForceGarbageCollection
  1798. UNIMPLEMENTED, // IterateOverObjectsReachable
  1799. UNIMPLEMENTED, // IterateOverReachableObjects
  1800. UNIMPLEMENTED, // IterateOverHeap
  1801. UNIMPLEMENTED, // IterateOverInstanceOfClass
  1802. RESERVED, // reserved113
  1803. UNIMPLEMENTED, // GetObjectsWithTags
  1804. RESERVED, // reserved115
  1805. RESERVED, // reserved116
  1806. RESERVED, // reserved117
  1807. RESERVED, // reserved118
  1808. RESERVED, // reserved119
  1809. _Jv_JVMTI_SetJNIFunctionTable, // SetJNIFunctionTable
  1810. _Jv_JVMTI_GetJNIFunctionTable, // GetJNIFunctionTable
  1811. _Jv_JVMTI_SetEventCallbacks, // SetEventCallbacks
  1812. UNIMPLEMENTED, // GenerateEvents
  1813. UNIMPLEMENTED, // GetExtensionFunctions
  1814. UNIMPLEMENTED, // GetExtensionEvents
  1815. UNIMPLEMENTED, // SetExtensionEventCallback
  1816. _Jv_JVMTI_DisposeEnvironment, // DisposeEnvironment
  1817. _Jv_JVMTI_GetErrorName, // GetErrorName
  1818. UNIMPLEMENTED, // GetJLocationFormat
  1819. UNIMPLEMENTED, // GetSystemProperties
  1820. _Jv_JVMTI_GetSystemProperty, // GetSystemProperty
  1821. _Jv_JVMTI_SetSystemProperty, // SetSystemProperty
  1822. UNIMPLEMENTED, // GetPhase
  1823. UNIMPLEMENTED, // GetCurrentThreadCpuTimerInfo
  1824. UNIMPLEMENTED, // GetCurrentThreadCpuTime
  1825. UNIMPLEMENTED, // GetThreadCpuTimerInfo
  1826. UNIMPLEMENTED, // GetThreadCpuTime
  1827. UNIMPLEMENTED, // GetTimerInfo
  1828. _Jv_JVMTI_GetTime, // GetTime
  1829. UNIMPLEMENTED, // GetPotentialCapabilities
  1830. RESERVED, // reserved141
  1831. UNIMPLEMENTED, // AddCapabilities
  1832. UNIMPLEMENTED, // RelinquishCapabilities
  1833. _Jv_JVMTI_GetAvailableProcessors, // GetAvailableProcessors
  1834. RESERVED, // reserved145
  1835. RESERVED, // reserved146
  1836. UNIMPLEMENTED, // GetEnvironmentLocalStorage
  1837. UNIMPLEMENTED, // SetEnvironmentLocalStorage
  1838. _Jv_JVMTI_AddToBootstrapClassLoaderSearch, // AddToBootstrapClassLoaderSearch
  1839. _Jv_JVMTI_SetVerboseFlag, // SetVerboseFlag
  1840. RESERVED, // reserved151
  1841. RESERVED, // reserved152
  1842. RESERVED, // reserved153
  1843. _Jv_JVMTI_GetObjectSize // GetObjectSize
  1844. };
  1845. _Jv_JVMTIEnv *
  1846. _Jv_GetJVMTIEnv (void)
  1847. {
  1848. _Jv_JVMTIEnv *env
  1849. = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
  1850. memset (env, 0, sizeof (_Jv_JVMTIEnv));
  1851. env->p = &_Jv_JVMTI_Interface;
  1852. struct jvmti_env_list *element
  1853. = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
  1854. element->env = env;
  1855. element->next = NULL;
  1856. _envListLock->writeLock ()->lock ();
  1857. if (_jvmtiEnvironments == NULL)
  1858. _jvmtiEnvironments = element;
  1859. else
  1860. {
  1861. struct jvmti_env_list *e;
  1862. for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
  1863. ;
  1864. e->next = element;
  1865. }
  1866. _envListLock->writeLock ()->unlock ();
  1867. /* Mark JVMTI active. This is used to force the interpreter
  1868. to use either debugging or non-debugging code. Once JVMTI
  1869. has been enabled, the non-debug interpreter cannot be used. */
  1870. JVMTI::enabled = true;
  1871. return env;
  1872. }
  1873. void
  1874. _Jv_JVMTI_Init ()
  1875. {
  1876. _jvmtiEnvironments = NULL;
  1877. _envListLock
  1878. = new java::util::concurrent::locks::ReentrantReadWriteLock ();
  1879. // No environments, so this should set all JVMTI:: members to false
  1880. check_enabled_events ();
  1881. }
  1882. static void
  1883. post_event (jvmtiEnv *env, jvmtiEvent type, jthread event_thread, va_list args)
  1884. {
  1885. #define ARG(Type,Name) Type Name = (Type) va_arg (args, Type)
  1886. #define GET_BOOLEAN_ARG(Name) \
  1887. ARG (int, b); \
  1888. jboolean Name = (b == 0) ? false : true
  1889. #define GET_CHAR_ARG(Name) \
  1890. ARG (int, c); \
  1891. char Name = static_cast<char> (c)
  1892. switch (type)
  1893. {
  1894. case JVMTI_EVENT_VM_INIT:
  1895. if (env->callbacks.VMInit != NULL)
  1896. {
  1897. ARG (JNIEnv *, jni_env);
  1898. env->callbacks.VMInit (env, jni_env, event_thread);
  1899. }
  1900. break;
  1901. case JVMTI_EVENT_VM_DEATH:
  1902. if (env->callbacks.VMDeath != NULL)
  1903. {
  1904. ARG (JNIEnv *, jni_env);
  1905. env->callbacks.VMDeath (env, jni_env);
  1906. }
  1907. break;
  1908. case JVMTI_EVENT_THREAD_START:
  1909. if (env->callbacks.ThreadStart != NULL)
  1910. {
  1911. ARG (JNIEnv *, jni_env);
  1912. env->callbacks.ThreadStart (env, jni_env, event_thread);
  1913. }
  1914. break;
  1915. case JVMTI_EVENT_THREAD_END:
  1916. if (env->callbacks.ThreadEnd != NULL)
  1917. {
  1918. ARG (JNIEnv *, jni_env);
  1919. env->callbacks.ThreadEnd (env, jni_env, event_thread);
  1920. }
  1921. break;
  1922. case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
  1923. if (env->callbacks.ClassFileLoadHook != NULL)
  1924. {
  1925. ARG (JNIEnv *, jni_env);
  1926. ARG (jclass, class_being_redefined);
  1927. ARG (jobject, loader);
  1928. ARG (const char *, name);
  1929. ARG (jobject, protection_domain);
  1930. ARG (jint, class_data_len);
  1931. ARG (const unsigned char *, class_data);
  1932. ARG (jint *, new_class_data_len);
  1933. ARG (unsigned char **, new_class_data);
  1934. env->callbacks.ClassFileLoadHook (env, jni_env,
  1935. class_being_redefined, loader,
  1936. name, protection_domain,
  1937. class_data_len, class_data,
  1938. new_class_data_len,
  1939. new_class_data);
  1940. }
  1941. break;
  1942. case JVMTI_EVENT_CLASS_LOAD:
  1943. if (env->callbacks.ClassLoad != NULL)
  1944. {
  1945. ARG (JNIEnv *, jni_env);
  1946. ARG (jclass, klass);
  1947. env->callbacks.ClassLoad (env, jni_env, event_thread, klass);
  1948. }
  1949. break;
  1950. case JVMTI_EVENT_CLASS_PREPARE:
  1951. if (env->callbacks.ClassPrepare != NULL)
  1952. {
  1953. ARG (JNIEnv *, jni_env);
  1954. ARG (jclass, klass);
  1955. env->callbacks.ClassPrepare (env, jni_env, event_thread, klass);
  1956. }
  1957. break;
  1958. case JVMTI_EVENT_VM_START:
  1959. if (env->callbacks.VMStart != NULL)
  1960. {
  1961. ARG (JNIEnv *, jni_env);
  1962. env->callbacks.VMStart (env, jni_env);
  1963. }
  1964. break;
  1965. case JVMTI_EVENT_EXCEPTION:
  1966. if (env->callbacks.Exception != NULL)
  1967. {
  1968. ARG (JNIEnv *, jni_env);
  1969. ARG (jmethodID, method);
  1970. ARG (jlocation, location);
  1971. ARG (jobject, exception);
  1972. ARG (jmethodID, catch_method);
  1973. ARG (jlocation, catch_location);
  1974. env->callbacks.Exception (env, jni_env, event_thread, method,
  1975. location, exception, catch_method,
  1976. catch_location);
  1977. }
  1978. break;
  1979. case JVMTI_EVENT_EXCEPTION_CATCH:
  1980. if (env->callbacks.ExceptionCatch != NULL)
  1981. {
  1982. ARG (JNIEnv *, jni_env);
  1983. ARG (jmethodID, method);
  1984. ARG (jlocation, location);
  1985. ARG (jobject, exception);
  1986. env->callbacks.ExceptionCatch (env, jni_env, event_thread, method,
  1987. location, exception);
  1988. }
  1989. break;
  1990. case JVMTI_EVENT_SINGLE_STEP:
  1991. if (env->callbacks.SingleStep != NULL)
  1992. {
  1993. ARG (JNIEnv *, jni_env);
  1994. ARG (jmethodID, method);
  1995. ARG (jlocation, location);
  1996. env->callbacks.SingleStep (env, jni_env, event_thread, method,
  1997. location);
  1998. }
  1999. break;
  2000. case JVMTI_EVENT_FRAME_POP:
  2001. if (env->callbacks.FramePop != NULL)
  2002. {
  2003. ARG (JNIEnv *, jni_env);
  2004. ARG (jmethodID, method);
  2005. GET_BOOLEAN_ARG (was_popped_by_exception);
  2006. env->callbacks.FramePop (env, jni_env, event_thread, method,
  2007. was_popped_by_exception);
  2008. }
  2009. break;
  2010. case JVMTI_EVENT_BREAKPOINT:
  2011. if (env->callbacks.Breakpoint != NULL)
  2012. {
  2013. ARG (JNIEnv *, jni_env);
  2014. ARG (jmethodID, method);
  2015. ARG (jlocation, location);
  2016. env->callbacks.Breakpoint (env, jni_env, event_thread, method,
  2017. location);
  2018. }
  2019. break;
  2020. case JVMTI_EVENT_FIELD_ACCESS:
  2021. if (env->callbacks.FieldAccess != NULL)
  2022. {
  2023. ARG (JNIEnv *, jni_env);
  2024. ARG (jmethodID, method);
  2025. ARG (jlocation, location);
  2026. ARG (jclass, field_class);
  2027. ARG (jobject, object);
  2028. ARG (jfieldID, field);
  2029. env->callbacks.FieldAccess (env, jni_env, event_thread, method,
  2030. location, field_class, object, field);
  2031. }
  2032. break;
  2033. case JVMTI_EVENT_FIELD_MODIFICATION:
  2034. if (env->callbacks.FieldModification != NULL)
  2035. {
  2036. ARG (JNIEnv *, jni_env);
  2037. ARG (jmethodID, method);
  2038. ARG (jlocation, location);
  2039. ARG (jclass, field_class);
  2040. ARG (jobject, object);
  2041. ARG (jfieldID, field);
  2042. GET_CHAR_ARG (signature_type);
  2043. ARG (jvalue, new_value);
  2044. env->callbacks.FieldModification (env, jni_env, event_thread, method,
  2045. location, field_class, object,
  2046. field, signature_type, new_value);
  2047. }
  2048. break;
  2049. case JVMTI_EVENT_METHOD_ENTRY:
  2050. if (env->callbacks.MethodEntry != NULL)
  2051. {
  2052. ARG (JNIEnv *, jni_env);
  2053. ARG (jmethodID, method);
  2054. env->callbacks.MethodEntry (env, jni_env, event_thread, method);
  2055. }
  2056. break;
  2057. case JVMTI_EVENT_METHOD_EXIT:
  2058. if (env->callbacks.MethodExit != NULL)
  2059. {
  2060. ARG (JNIEnv *, jni_env);
  2061. ARG (jmethodID, method);
  2062. GET_BOOLEAN_ARG (was_popped_by_exception);
  2063. ARG (jvalue, return_value);
  2064. env->callbacks.MethodExit (env, jni_env, event_thread, method,
  2065. was_popped_by_exception, return_value);
  2066. }
  2067. break;
  2068. case JVMTI_EVENT_NATIVE_METHOD_BIND:
  2069. if (env->callbacks.NativeMethodBind != NULL)
  2070. {
  2071. ARG (JNIEnv *, jni_env);
  2072. ARG (jmethodID, method);
  2073. ARG (void *, address);
  2074. ARG (void **, new_address_ptr);
  2075. env->callbacks.NativeMethodBind (env, jni_env, event_thread, method,
  2076. address, new_address_ptr);
  2077. }
  2078. break;
  2079. case JVMTI_EVENT_COMPILED_METHOD_LOAD:
  2080. if (env->callbacks.CompiledMethodLoad != NULL)
  2081. {
  2082. ARG (jmethodID, method);
  2083. ARG (jint, code_size);
  2084. ARG (const void *, code_addr);
  2085. ARG (jint, map_length);
  2086. ARG (const jvmtiAddrLocationMap *, map);
  2087. ARG (const void *, compile_info);
  2088. env->callbacks.CompiledMethodLoad (env, method, code_size, code_addr,
  2089. map_length, map, compile_info);
  2090. }
  2091. break;
  2092. case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
  2093. if (env->callbacks.CompiledMethodUnload != NULL)
  2094. {
  2095. ARG (jmethodID, method);
  2096. ARG (const void *, code_addr);
  2097. env->callbacks.CompiledMethodUnload (env, method, code_addr);
  2098. }
  2099. break;
  2100. case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
  2101. if (env->callbacks.DynamicCodeGenerated != NULL)
  2102. {
  2103. ARG (const char *, name);
  2104. ARG (const void *, address);
  2105. ARG (jint, length);
  2106. env->callbacks.DynamicCodeGenerated (env, name, address, length);
  2107. }
  2108. break;
  2109. case JVMTI_EVENT_DATA_DUMP_REQUEST:
  2110. if (env->callbacks.DataDumpRequest != NULL)
  2111. {
  2112. env->callbacks.DataDumpRequest (env);
  2113. }
  2114. break;
  2115. case JVMTI_EVENT_MONITOR_WAIT:
  2116. if (env->callbacks.MonitorWait != NULL)
  2117. {
  2118. ARG (JNIEnv *, jni_env);
  2119. ARG (jobject, object);
  2120. ARG (jlong, timeout);
  2121. env->callbacks.MonitorWait (env, jni_env, event_thread, object,
  2122. timeout);
  2123. }
  2124. break;
  2125. case JVMTI_EVENT_MONITOR_WAITED:
  2126. if (env->callbacks.MonitorWaited != NULL)
  2127. {
  2128. ARG (JNIEnv *, jni_env);
  2129. ARG (jobject, object);
  2130. GET_BOOLEAN_ARG (timed_out);
  2131. env->callbacks.MonitorWaited (env, jni_env, event_thread, object,
  2132. timed_out);
  2133. }
  2134. break;
  2135. case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
  2136. if (env->callbacks.MonitorContendedEnter != NULL)
  2137. {
  2138. ARG (JNIEnv *, jni_env);
  2139. ARG (jobject, object);
  2140. env->callbacks.MonitorContendedEnter (env, jni_env, event_thread,
  2141. object);
  2142. }
  2143. break;
  2144. case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
  2145. if (env->callbacks.MonitorContendedEntered != NULL)
  2146. {
  2147. ARG (JNIEnv *, jni_env);
  2148. ARG (jobject, object);
  2149. env->callbacks.MonitorContendedEntered (env, jni_env, event_thread,
  2150. object);
  2151. }
  2152. break;
  2153. case JVMTI_EVENT_GARBAGE_COLLECTION_START:
  2154. if (env->callbacks.GarbageCollectionStart != NULL)
  2155. {
  2156. env->callbacks.GarbageCollectionStart (env);
  2157. }
  2158. break;
  2159. case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
  2160. if (env->callbacks.GarbageCollectionFinish != NULL)
  2161. {
  2162. env->callbacks.GarbageCollectionFinish (env);
  2163. }
  2164. break;
  2165. case JVMTI_EVENT_OBJECT_FREE:
  2166. if (env->callbacks.ObjectFree != NULL)
  2167. {
  2168. ARG (jlong, tag);
  2169. env->callbacks.ObjectFree (env, tag);
  2170. }
  2171. break;
  2172. case JVMTI_EVENT_VM_OBJECT_ALLOC:
  2173. if (env->callbacks.VMObjectAlloc != NULL)
  2174. {
  2175. ARG (JNIEnv *, jni_env);
  2176. ARG (jobject, object);
  2177. ARG (jclass, object_class);
  2178. ARG (jlong, size);
  2179. env->callbacks.VMObjectAlloc (env, jni_env, event_thread,
  2180. object, object_class, size);
  2181. }
  2182. break;
  2183. default:
  2184. fprintf (stderr, "libgcj: post of unknown JVMTI event (%d)\n",
  2185. (int) type);
  2186. break;
  2187. }
  2188. va_end (args);
  2189. #undef ARG
  2190. #undef GET_BOOLEAN_ARG
  2191. #undef GET_CHAR_ARG
  2192. }
  2193. /* Post an event to requesting JVMTI environments
  2194. *
  2195. * This function should not be called without consulting the
  2196. * JVMTI_REQUESTED_EVENT macro first (for speed). It does no real
  2197. * harm (other than kill speed), since this function will still
  2198. * only send the event if it was properly requested by an environment.
  2199. */
  2200. void
  2201. _Jv_JVMTI_PostEvent (jvmtiEvent type, jthread event_thread, ...)
  2202. {
  2203. va_list args;
  2204. va_start (args, event_thread);
  2205. _envListLock->readLock ()->lock ();
  2206. struct jvmti_env_list *e;
  2207. FOREACH_ENVIRONMENT (e)
  2208. {
  2209. /* Events are only posted if the event was explicitly enabled,
  2210. it has a registered event handler, and the event thread
  2211. matches (either globally or restricted to a specific thread).
  2212. Here we check all but the event handler, which will be handled
  2213. by post_event. */
  2214. if (e->env->enabled[EVENT_INDEX(type)]
  2215. && (e->env->thread[EVENT_INDEX(type)] == NULL
  2216. || e->env->thread[EVENT_INDEX(type)] == event_thread))
  2217. {
  2218. post_event (e->env, type, event_thread, args);
  2219. }
  2220. }
  2221. _envListLock->readLock ()->unlock ();
  2222. va_end (args);
  2223. }