Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/jcl/common/getstacktrace.c
6000 views
1
/*******************************************************************************
2
* Copyright (c) 1998, 2022 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include <string.h>
24
25
#include "j9.h"
26
#include "j9cfg.h"
27
#include "j9consts.h"
28
#include "omrgcconsts.h"
29
#include "j9protos.h"
30
#include "jcl_internal.h"
31
#include "objhelp.h"
32
#include "ut_j9jcl.h"
33
#include "vmaccess.h"
34
35
36
37
j9object_t
38
getStackTraceForThread(J9VMThread *currentThread, J9VMThread *targetThread, UDATA skipCount)
39
{
40
J9JavaVM * vm = currentThread->javaVM;
41
J9InternalVMFunctions * vmfns = vm->internalVMFunctions;
42
j9object_t throwable = NULL;
43
J9StackWalkState walkState;
44
UDATA rc;
45
46
/* Halt the target thread */
47
vmfns->haltThreadForInspection(currentThread, targetThread);
48
49
/* walk stack and cache PCs */
50
walkState.walkThread = targetThread;
51
walkState.flags = J9_STACKWALK_CACHE_PCS | J9_STACKWALK_WALK_TRANSLATE_PC | J9_STACKWALK_SKIP_INLINES | J9_STACKWALK_INCLUDE_NATIVES | J9_STACKWALK_VISIBLE_ONLY;
52
/* If -XX:+ShowHiddenFrames option has not been set, skip hidden method frames */
53
if (J9_ARE_NO_BITS_SET(vm->runtimeFlags, J9_RUNTIME_SHOW_HIDDEN_FRAMES)) {
54
walkState.flags |= J9_STACKWALK_SKIP_HIDDEN_FRAMES;
55
}
56
walkState.skipCount = skipCount;
57
rc = vm->walkStackFrames(currentThread, &walkState);
58
59
/* Now that the stack trace has been copied, resume the thread */
60
vmfns->resumeThreadForInspection(currentThread, targetThread);
61
62
/* Check for stack walk failure */
63
if (rc != J9_STACKWALK_RC_NONE) {
64
vmfns->setNativeOutOfMemoryError(currentThread, 0, 0);
65
goto fail;
66
}
67
68
throwable = createStackTraceThrowable(currentThread, walkState.cache, walkState.framesWalked);
69
70
fail:
71
vmfns->freeStackWalkCaches(currentThread, &walkState);
72
73
/* Return the result - any pending exception will be checked by the caller and the result discarded */
74
return throwable;
75
}
76
77
j9object_t
78
createStackTraceThrowable(J9VMThread *currentThread, const UDATA *frames, UDATA maxFrames)
79
{
80
J9JavaVM *vm = currentThread->javaVM;
81
J9InternalVMFunctions *vmfns = vm->internalVMFunctions;
82
J9MemoryManagerFunctions *mmfns = vm->memoryManagerFunctions;
83
J9Class *throwableClass = NULL;
84
J9Class *arrayClass = NULL;
85
j9object_t throwable = NULL;
86
j9array_t walkback = NULL;
87
UDATA i;
88
89
Assert_JCL_notNull(currentThread);
90
Assert_JCL_mustHaveVMAccess(currentThread);
91
if (maxFrames > 0) {
92
Assert_JCL_notNull(frames);
93
}
94
95
/* Create the result array */
96
/* We may allocate an array of zero elements */
97
98
#ifdef J9VM_ENV_DATA64
99
arrayClass = vm->longArrayClass;
100
#else
101
arrayClass = vm->intArrayClass;
102
#endif
103
walkback = (j9array_t)mmfns->J9AllocateIndexableObject(
104
currentThread, arrayClass, (U_32)maxFrames, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE);
105
if (walkback == NULL) {
106
goto fail_outOfMemory;
107
}
108
109
for (i = 0; i < maxFrames; ++i) {
110
J9JAVAARRAYOFUDATA_STORE(currentThread, walkback, i, frames[i]);
111
}
112
113
/* Create the Throwable and store the walkback in it */
114
115
PUSH_OBJECT_IN_SPECIAL_FRAME(currentThread, (j9object_t)walkback);
116
throwableClass = vmfns->internalFindKnownClass(currentThread, J9VMCONSTANTPOOL_JAVALANGTHROWABLE, J9_FINDKNOWNCLASS_FLAG_INITIALIZE);
117
if (throwableClass == NULL) {
118
/* Exception already set */
119
DROP_OBJECT_IN_SPECIAL_FRAME(currentThread);
120
return NULL;
121
}
122
throwable = mmfns->J9AllocateObject(
123
currentThread, throwableClass, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE);
124
if (throwable == NULL) {
125
DROP_OBJECT_IN_SPECIAL_FRAME(currentThread);
126
goto fail_outOfMemory;
127
}
128
walkback = (j9array_t)POP_OBJECT_IN_SPECIAL_FRAME(currentThread);
129
J9VMJAVALANGTHROWABLE_SET_WALKBACK(currentThread, throwable, walkback);
130
131
/* Return the result - any pending exception will be checked by the caller and the result discarded */
132
return throwable;
133
134
fail_outOfMemory:
135
vmfns->setHeapOutOfMemoryError(currentThread);
136
return NULL;
137
}
138
139
140