Path: blob/master/runtime/jcl/common/getstacktrace.c
6000 views
/*******************************************************************************1* Copyright (c) 1998, 2022 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122#include <string.h>2324#include "j9.h"25#include "j9cfg.h"26#include "j9consts.h"27#include "omrgcconsts.h"28#include "j9protos.h"29#include "jcl_internal.h"30#include "objhelp.h"31#include "ut_j9jcl.h"32#include "vmaccess.h"33343536j9object_t37getStackTraceForThread(J9VMThread *currentThread, J9VMThread *targetThread, UDATA skipCount)38{39J9JavaVM * vm = currentThread->javaVM;40J9InternalVMFunctions * vmfns = vm->internalVMFunctions;41j9object_t throwable = NULL;42J9StackWalkState walkState;43UDATA rc;4445/* Halt the target thread */46vmfns->haltThreadForInspection(currentThread, targetThread);4748/* walk stack and cache PCs */49walkState.walkThread = targetThread;50walkState.flags = J9_STACKWALK_CACHE_PCS | J9_STACKWALK_WALK_TRANSLATE_PC | J9_STACKWALK_SKIP_INLINES | J9_STACKWALK_INCLUDE_NATIVES | J9_STACKWALK_VISIBLE_ONLY;51/* If -XX:+ShowHiddenFrames option has not been set, skip hidden method frames */52if (J9_ARE_NO_BITS_SET(vm->runtimeFlags, J9_RUNTIME_SHOW_HIDDEN_FRAMES)) {53walkState.flags |= J9_STACKWALK_SKIP_HIDDEN_FRAMES;54}55walkState.skipCount = skipCount;56rc = vm->walkStackFrames(currentThread, &walkState);5758/* Now that the stack trace has been copied, resume the thread */59vmfns->resumeThreadForInspection(currentThread, targetThread);6061/* Check for stack walk failure */62if (rc != J9_STACKWALK_RC_NONE) {63vmfns->setNativeOutOfMemoryError(currentThread, 0, 0);64goto fail;65}6667throwable = createStackTraceThrowable(currentThread, walkState.cache, walkState.framesWalked);6869fail:70vmfns->freeStackWalkCaches(currentThread, &walkState);7172/* Return the result - any pending exception will be checked by the caller and the result discarded */73return throwable;74}7576j9object_t77createStackTraceThrowable(J9VMThread *currentThread, const UDATA *frames, UDATA maxFrames)78{79J9JavaVM *vm = currentThread->javaVM;80J9InternalVMFunctions *vmfns = vm->internalVMFunctions;81J9MemoryManagerFunctions *mmfns = vm->memoryManagerFunctions;82J9Class *throwableClass = NULL;83J9Class *arrayClass = NULL;84j9object_t throwable = NULL;85j9array_t walkback = NULL;86UDATA i;8788Assert_JCL_notNull(currentThread);89Assert_JCL_mustHaveVMAccess(currentThread);90if (maxFrames > 0) {91Assert_JCL_notNull(frames);92}9394/* Create the result array */95/* We may allocate an array of zero elements */9697#ifdef J9VM_ENV_DATA6498arrayClass = vm->longArrayClass;99#else100arrayClass = vm->intArrayClass;101#endif102walkback = (j9array_t)mmfns->J9AllocateIndexableObject(103currentThread, arrayClass, (U_32)maxFrames, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE);104if (walkback == NULL) {105goto fail_outOfMemory;106}107108for (i = 0; i < maxFrames; ++i) {109J9JAVAARRAYOFUDATA_STORE(currentThread, walkback, i, frames[i]);110}111112/* Create the Throwable and store the walkback in it */113114PUSH_OBJECT_IN_SPECIAL_FRAME(currentThread, (j9object_t)walkback);115throwableClass = vmfns->internalFindKnownClass(currentThread, J9VMCONSTANTPOOL_JAVALANGTHROWABLE, J9_FINDKNOWNCLASS_FLAG_INITIALIZE);116if (throwableClass == NULL) {117/* Exception already set */118DROP_OBJECT_IN_SPECIAL_FRAME(currentThread);119return NULL;120}121throwable = mmfns->J9AllocateObject(122currentThread, throwableClass, J9_GC_ALLOCATE_OBJECT_NON_INSTRUMENTABLE);123if (throwable == NULL) {124DROP_OBJECT_IN_SPECIAL_FRAME(currentThread);125goto fail_outOfMemory;126}127walkback = (j9array_t)POP_OBJECT_IN_SPECIAL_FRAME(currentThread);128J9VMJAVALANGTHROWABLE_SET_WALKBACK(currentThread, throwable, walkback);129130/* Return the result - any pending exception will be checked by the caller and the result discarded */131return throwable;132133fail_outOfMemory:134vmfns->setHeapOutOfMemoryError(currentThread);135return NULL;136}137138139140