1 /*
2 * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #ifndef JDWP_UTIL_H
27 #define JDWP_UTIL_H
28
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34
35 #ifdef LINUX
36 // Note. On Alpine Linux pthread.h includes calloc/malloc functions declaration.
37 // We need to include pthread.h before the following stdlib names poisoning.
38 #include <pthread.h>
39 #endif
40
41 #ifdef DEBUG
42 /* Just to make sure these interfaces are not used here. */
43 #undef free
44 #define free(p) Do not use this interface.
45 #undef malloc
46 #define malloc(p) Do not use this interface.
47 #undef calloc
48 #define calloc(p) Do not use this interface.
49 #undef realloc
50 #define realloc(p) Do not use this interface.
51 #undef strdup
52 #define strdup(p) Do not use this interface.
53 #endif
54
55 #include "log_messages.h"
56 #include "vm_interface.h"
57 #include "JDWP.h"
58 #include "util_md.h"
59 #include "error_messages.h"
60 #include "debugInit.h"
61
62 /* To handle "format string is not a string literal" warning. */
63 #if !defined(_MSC_VER)
64 #define ATTRIBUTE_PRINTF(fmt_pos_num, vargs_pos_num) \
65 __attribute__((format(printf, fmt_pos_num, vargs_pos_num)))
66 #else
67 #define ATTRIBUTE_PRINTF(fmt_pos_num, vargs_pos_num)
68 #endif
69
70 /* Definition of a CommonRef tracked by the backend for the frontend */
71 typedef struct RefNode {
72 jlong seqNum; /* ID of reference, also key for hash table */
73 jobject ref; /* could be strong or weak */
74 struct RefNode *next; /* next RefNode* in bucket chain */
75 jint count; /* count of references */
76 jboolean isPinAll; /* true if this is a strong reference due to a commonRef_pinAll() */
77 jboolean isCommonPin; /* true if this is a strong reference due to a commonRef_pin() */
78 } RefNode;
79
80 /* Value of a NULL ID */
81 #define NULL_OBJECT_ID ((jlong)0)
82
83 /*
84 * Globals used throughout the back end
85 */
86
87 typedef jint FrameNumber;
88
89 typedef struct {
90 jvmtiEnv *jvmti;
91 JavaVM *jvm;
92 volatile jboolean vmDead; /* Once VM is dead it stays that way - don't put in init */
93 jboolean assertOn;
94 jboolean assertFatal;
95 jboolean vthreadsSupported; /* If true, debugging support for vthreads is enabled. */
96 jboolean includeVThreads; /* If true, VM.AllThreads includes vthreads. */
97 jboolean rememberVThreadsWhenDisconnected;
98 jboolean doerrorexit;
99 jboolean modifiedUtf8;
100 jboolean quiet;
101 jboolean jvmti_data_dump; /* If true, then support JVMTI DATA_DUMP_REQUEST events. */
102
103 char * options;
104
105 jclass classClass;
106 jclass threadClass;
107 jclass threadGroupClass;
108 jclass classLoaderClass;
109 jclass stringClass;
110 jclass systemClass;
111 jmethodID threadConstructor;
112 jmethodID threadSetDaemon;
113 jmethodID systemGetProperty;
114 jmethodID setProperty;
115 jthreadGroup systemThreadGroup;
116 jobject agent_properties;
117
118 jint cachedJvmtiVersion;
119 jvmtiCapabilities cachedJvmtiCapabilities;
120 jboolean haveCachedJvmtiCapabilities;
121 jvmtiEventCallbacks callbacks;
122
123 /* Various property values we should grab on initialization */
124 char* property_java_version; /* UTF8 java.version */
125 char* property_java_vm_name; /* UTF8 java.vm.name */
126 char* property_java_vm_info; /* UTF8 java.vm.info */
127 char* property_java_class_path; /* UTF8 java.class.path */
128 char* property_sun_boot_library_path; /* UTF8 sun.boot.library.path */
129 char* property_path_separator; /* UTF8 path.separator */
130 char* property_user_dir; /* UTF8 user.dir */
131
132 unsigned log_flags;
133
134 /* Common References static data */
135 jrawMonitorID refLock;
136 jlong nextSeqNum;
137 unsigned pinAllCount;
138 RefNode **objectsByID;
139 int objectsByIDsize;
140 int objectsByIDcount;
141
142 /* Indication that the agent has been loaded */
143 jboolean isLoaded;
144
145 /* Indication that VM_DEATH has been received and the JVMTI callbacks have been cleared. */
146 volatile jboolean jvmtiCallBacksCleared;
147
148 } BackendGlobalData;
149
150 extern BackendGlobalData * gdata;
151
152 /*
153 * Event Index for handlers
154 */
155
156 typedef enum {
157 EI_min = 1,
158
159 EI_SINGLE_STEP = 1,
160 EI_BREAKPOINT = 2,
161 EI_FRAME_POP = 3,
162 EI_EXCEPTION = 4,
163 EI_THREAD_START = 5,
164 EI_THREAD_END = 6,
165 EI_CLASS_PREPARE = 7,
166 EI_CLASS_UNLOAD = 8,
167 EI_CLASS_LOAD = 9,
168 EI_FIELD_ACCESS = 10,
169 EI_FIELD_MODIFICATION = 11,
170 EI_EXCEPTION_CATCH = 12,
171 EI_METHOD_ENTRY = 13,
172 EI_METHOD_EXIT = 14,
173 EI_MONITOR_CONTENDED_ENTER = 15,
174 EI_MONITOR_CONTENDED_ENTERED = 16,
175 EI_MONITOR_WAIT = 17,
176 EI_MONITOR_WAITED = 18,
177 EI_VM_INIT = 19,
178 EI_VM_DEATH = 20,
179 EI_VIRTUAL_THREAD_START = 21,
180 EI_VIRTUAL_THREAD_END = 22,
181
182 EI_max = 22
183 } EventIndex;
184
185 /* Agent errors that might be in a jvmtiError for JDWP or internal.
186 * (Done this way so that compiler allows it's use as a jvmtiError)
187 */
188 #define _AGENT_ERROR(x) ((jvmtiError)(JVMTI_ERROR_MAX+64+x))
189 #define AGENT_ERROR_INTERNAL _AGENT_ERROR(1)
190 #define AGENT_ERROR_VM_DEAD _AGENT_ERROR(2)
191 #define AGENT_ERROR_NO_JNI_ENV _AGENT_ERROR(3)
192 #define AGENT_ERROR_JNI_EXCEPTION _AGENT_ERROR(4)
193 #define AGENT_ERROR_JVMTI_INTERNAL _AGENT_ERROR(5)
194 #define AGENT_ERROR_JDWP_INTERNAL _AGENT_ERROR(6)
195 #define AGENT_ERROR_NOT_CURRENT_FRAME _AGENT_ERROR(7)
196 #define AGENT_ERROR_OUT_OF_MEMORY _AGENT_ERROR(8)
197 #define AGENT_ERROR_INVALID_TAG _AGENT_ERROR(9)
198 #define AGENT_ERROR_ALREADY_INVOKING _AGENT_ERROR(10)
199 #define AGENT_ERROR_INVALID_INDEX _AGENT_ERROR(11)
200 #define AGENT_ERROR_INVALID_LENGTH _AGENT_ERROR(12)
201 #define AGENT_ERROR_INVALID_STRING _AGENT_ERROR(13)
202 #define AGENT_ERROR_INVALID_CLASS_LOADER _AGENT_ERROR(14)
203 #define AGENT_ERROR_INVALID_ARRAY _AGENT_ERROR(15)
204 #define AGENT_ERROR_TRANSPORT_LOAD _AGENT_ERROR(16)
205 #define AGENT_ERROR_TRANSPORT_INIT _AGENT_ERROR(17)
206 #define AGENT_ERROR_NATIVE_METHOD _AGENT_ERROR(18)
207 #define AGENT_ERROR_INVALID_COUNT _AGENT_ERROR(19)
208 #define AGENT_ERROR_INVALID_FRAMEID _AGENT_ERROR(20)
209 #define AGENT_ERROR_NULL_POINTER _AGENT_ERROR(21)
210 #define AGENT_ERROR_ILLEGAL_ARGUMENT _AGENT_ERROR(22)
211 #define AGENT_ERROR_INVALID_THREAD _AGENT_ERROR(23)
212 #define AGENT_ERROR_INVALID_EVENT_TYPE _AGENT_ERROR(24)
213 #define AGENT_ERROR_INVALID_OBJECT _AGENT_ERROR(25)
214 #define AGENT_ERROR_NO_MORE_FRAMES _AGENT_ERROR(26)
215 #define AGENT_ERROR_INVALID_MODULE _AGENT_ERROR(27)
216
217 /* Combined event information */
218
219 typedef struct {
220
221 EventIndex ei;
222 jthread thread;
223 jboolean is_vthread;
224 jclass clazz;
225 jmethodID method;
226 jlocation location;
227 jobject object; /* possibly an exception or user object */
228
229 union {
230
231 /* ei = EI_FIELD_ACCESS */
232 struct {
233 jclass field_clazz;
234 jfieldID field;
235 } field_access;
236
237 /* ei = EI_FIELD_MODIFICATION */
238 struct {
239 jclass field_clazz;
240 jfieldID field;
241 char signature_type;
242 jvalue new_value;
243 } field_modification;
244
245 /* ei = EI_EXCEPTION */
246 struct {
247 jclass catch_clazz;
248 jmethodID catch_method;
249 jlocation catch_location;
250 } exception;
251
252 /* ei = EI_METHOD_EXIT */
253 struct {
254 jvalue return_value;
255 } method_exit;
256
257 /* For monitor wait events */
258 union {
259 /* ei = EI_MONITOR_WAIT */
260 jlong timeout;
261 /* ei = EI_MONITOR_WAITED */
262 jboolean timed_out;
263 } monitor;
264 } u;
265
266 } EventInfo;
267
268 /* Structure to hold dynamic array of objects */
269 typedef struct ObjectBatch {
270 jobject *objects;
271 jint count;
272 } ObjectBatch;
273
274 /*
275 * Modifier flags for classes, fields, methods
276 */
277 #define MOD_PUBLIC 0x0001 /* visible to everyone */
278 #define MOD_PRIVATE 0x0002 /* visible only to the defining class */
279 #define MOD_PROTECTED 0x0004 /* visible to subclasses */
280 #define MOD_STATIC 0x0008 /* instance variable is static */
281 #define MOD_FINAL 0x0010 /* no further subclassing, overriding */
282 #define MOD_SYNCHRONIZED 0x0020 /* wrap method call in monitor lock */
283 #define MOD_VOLATILE 0x0040 /* can cache in registers */
284 #define MOD_TRANSIENT 0x0080 /* not persistent */
285 #define MOD_NATIVE 0x0100 /* implemented in C */
286 #define MOD_INTERFACE 0x0200 /* class is an interface */
287 #define MOD_ABSTRACT 0x0400 /* no definition provided */
288 /*
289 * Additional modifiers not defined as such in the JVM spec
290 */
291 #define MOD_SYNTHETIC 0xf0000000 /* not in source code */
292
293 /*
294 * util funcs
295 */
296 void util_initialize(JNIEnv *env);
297 void util_reset(void);
298
299 struct PacketInputStream;
300 struct PacketOutputStream;
301
302 jint uniqueID(void);
303 jbyte referenceTypeTag(jclass clazz);
304 jbyte specificTypeKey(JNIEnv *env, jobject object);
305 jboolean isObjectTag(jbyte tag);
306 jvmtiError spawnNewThread(jvmtiStartFunction func, void *arg, char *name);
307 void writeCodeLocation(struct PacketOutputStream *out, jclass clazz,
308 jmethodID method, jlocation location);
309
310 jvmtiError classInstances(jclass klass, ObjectBatch *instances, int maxInstances);
311 jvmtiError classInstanceCounts(jint classCount, jclass *classes, jlong *counts);
312 jvmtiError objectReferrers(jobject obj, ObjectBatch *referrers, int maxObjects);
313
314 /*
315 * Command handling helpers shared among multiple command sets
316 */
317 int filterDebugThreads(jthread *threads, int count);
318
319
320 void sharedGetFieldValues(struct PacketInputStream *in,
321 struct PacketOutputStream *out,
322 jboolean isStatic);
323 jboolean sharedInvoke(struct PacketInputStream *in,
324 struct PacketOutputStream *out);
325
326 jvmtiError fieldSignature(jclass, jfieldID, char **, char **, char **);
327 jvmtiError fieldModifiers(jclass, jfieldID, jint *);
328 jvmtiError methodSignature(jmethodID, char **, char **, char **);
329 jvmtiError methodReturnType(jmethodID, char *);
330 jvmtiError methodModifiers(jmethodID, jint *);
331 jvmtiError methodClass(jmethodID, jclass *);
332 jvmtiError methodLocation(jmethodID, jlocation*, jlocation*);
333 jvmtiError classLoader(jclass, jobject *);
334
335 /*
336 * Thin wrappers on top of JNI
337 */
338 JNIEnv *getEnv(void);
339 jboolean isClass(jobject object);
340 jboolean isVThread(jobject object);
341 jboolean isThread(jobject object);
342 jboolean isThreadGroup(jobject object);
343 jboolean isString(jobject object);
344 jboolean isClassLoader(jobject object);
345 jboolean isArray(jobject object);
346
347 /*
348 * Thin wrappers on top of JVMTI
349 */
350 jvmtiError jvmtiGetCapabilities(jvmtiCapabilities *caps);
351 jint jvmtiMajorVersion(void);
352 jint jvmtiMinorVersion(void);
353 jint jvmtiMicroVersion(void);
354 jvmtiError getSourceDebugExtension(jclass clazz, char **extensionPtr);
355
356 jrawMonitorID debugMonitorCreate(char *name);
357 void debugMonitorEnter(jrawMonitorID theLock);
358 void debugMonitorExit(jrawMonitorID theLock);
359 void debugMonitorWait(jrawMonitorID theLock);
360 void debugMonitorNotify(jrawMonitorID theLock);
361 void debugMonitorNotifyAll(jrawMonitorID theLock);
362 void debugMonitorDestroy(jrawMonitorID theLock);
363
364 jthread *allThreads(jint *count);
365
366 void threadGroupInfo(jthreadGroup, jvmtiThreadGroupInfo *info);
367
368 jclass findClass(JNIEnv *env, const char * name);
369 jmethodID getMethod(JNIEnv *env, jclass clazz, const char * name, const char *signature);
370 char *getModuleName(jclass);
371 char *getClassname(jclass);
372 jvmtiError classSignature(jclass, char**, char**);
373 jint classStatus(jclass);
374 void writeGenericSignature(struct PacketOutputStream *, char *);
375 jboolean isMethodNative(jmethodID);
376 jboolean isMethodObsolete(jmethodID);
377 jvmtiError isMethodSynthetic(jmethodID, jboolean*);
378 jvmtiError isFieldSynthetic(jclass, jfieldID, jboolean*);
379
380 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2);
381
382 jint objectHashCode(jobject);
383
384 jvmtiError allInterfaces(jclass clazz, jclass **ppinterfaces, jint *count);
385 jvmtiError allLoadedClasses(jclass **ppclasses, jint *count);
386 jvmtiError allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *count);
387 jvmtiError allNestedClasses(jclass clazz, jclass **ppnested, jint *pcount);
388
389 void setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue);
390
391 #ifdef DEBUG
392 // APIs that can be called when debugging the debug agent
393 char* translateThreadState(jint flags);
394 char* getThreadName(jthread thread);
395 char* getMethodName(jmethodID method);
396 void printStackTrace(jthread thread);
397 void printThreadInfo(jthread thread);
398 #endif
399
400 void *jvmtiAllocate(jint numBytes);
401 void jvmtiDeallocate(void *buffer);
402
403 void eventIndexInit(void);
404 char* eventIndex2EventName(EventIndex ei);
405 jdwpEvent eventIndex2jdwp(EventIndex i);
406 jvmtiEvent eventIndex2jvmti(EventIndex i);
407 EventIndex jdwp2EventIndex(jdwpEvent eventType);
408 EventIndex jvmti2EventIndex(jvmtiEvent kind);
409
410 jvmtiError map2jvmtiError(jdwpError);
411 jdwpError map2jdwpError(jvmtiError);
412 jdwpThreadStatus map2jdwpThreadStatus(jint state);
413 jint map2jdwpSuspendStatus(jint state);
414 jint map2jdwpClassStatus(jint);
415
416 void log_debugee_location(const char *func,
417 jthread thread, jmethodID method, jlocation location);
418
419 /*
420 * Local Reference management. The two macros below are used
421 * throughout the back end whenever space for JNI local references
422 * is needed in the current frame.
423 */
424
425 void createLocalRefSpace(JNIEnv *env, jint capacity);
426
427 #define WITH_LOCAL_REFS(env, number) \
428 createLocalRefSpace(env, number); \
429 { /* BEGINNING OF WITH SCOPE */
430
431 #define END_WITH_LOCAL_REFS(env) \
432 JNI_FUNC_PTR(env,PopLocalFrame)(env, NULL); \
433 } /* END OF WITH SCOPE */
434
435 void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj);
436 void tossGlobalRef(JNIEnv *env, jobject *pobj);
437
438 jvmtiEnv* getSpecialJvmti(void);
439
440 #endif