Path: blob/master/src/jdk.hotspot.agent/linux/native/libsaproc/DwarfParser.cpp
41149 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020, 2021, NTT DATA.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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include <jni.h>2627#include "dwarf.hpp"28#include "libproc.h"2930#define CHECK_EXCEPTION if (env->ExceptionOccurred()) { return; }3132static jfieldID p_dwarf_context_ID = 0;33static jint sa_RAX = -1;34static jint sa_RDX = -1;35static jint sa_RCX = -1;36static jint sa_RBX = -1;37static jint sa_RSI = -1;38static jint sa_RDI = -1;39static jint sa_RBP = -1;40static jint sa_RSP = -1;41static jint sa_R8 = -1;42static jint sa_R9 = -1;43static jint sa_R10 = -1;44static jint sa_R11 = -1;45static jint sa_R12 = -1;46static jint sa_R13 = -1;47static jint sa_R14 = -1;48static jint sa_R15 = -1;4950static jlong get_dwarf_context(JNIEnv *env, jobject obj) {51return env->GetLongField(obj, p_dwarf_context_ID);52}5354#define SET_REG(env, reg, reg_cls) \55jfieldID reg##_ID = env->GetStaticFieldID(reg_cls, #reg, "I"); \56CHECK_EXCEPTION \57sa_##reg = env->GetStaticIntField(reg_cls, reg##_ID); \58CHECK_EXCEPTION5960/*61* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser62* Method: init063* Signature: ()V64*/65extern "C"66JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_init067(JNIEnv *env, jclass this_cls) {68jclass cls = env->FindClass("sun/jvm/hotspot/debugger/linux/amd64/DwarfParser");69CHECK_EXCEPTION70p_dwarf_context_ID = env->GetFieldID(cls, "p_dwarf_context", "J");71CHECK_EXCEPTION7273jclass reg_cls = env->FindClass("sun/jvm/hotspot/debugger/amd64/AMD64ThreadContext");74CHECK_EXCEPTION75SET_REG(env, RAX, reg_cls);76SET_REG(env, RDX, reg_cls);77SET_REG(env, RCX, reg_cls);78SET_REG(env, RBX, reg_cls);79SET_REG(env, RSI, reg_cls);80SET_REG(env, RDI, reg_cls);81SET_REG(env, RBP, reg_cls);82SET_REG(env, RSP, reg_cls);83SET_REG(env, R8, reg_cls);84SET_REG(env, R9, reg_cls);85SET_REG(env, R10, reg_cls);86SET_REG(env, R11, reg_cls);87SET_REG(env, R12, reg_cls);88SET_REG(env, R13, reg_cls);89SET_REG(env, R14, reg_cls);90SET_REG(env, R15, reg_cls);91}9293/*94* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser95* Method: createDwarfContext96* Signature: (J)J97*/98extern "C"99JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_createDwarfContext100(JNIEnv *env, jclass this_cls, jlong lib) {101DwarfParser *parser = new DwarfParser(reinterpret_cast<lib_info *>(lib));102if (!parser->is_parseable()) {103jclass ex_cls = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException");104if (!env->ExceptionOccurred()) {105env->ThrowNew(ex_cls, "DWARF not found");106}107delete parser;108return 0L;109}110111return reinterpret_cast<jlong>(parser);112}113114/*115* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser116* Method: destroyDwarfContext117* Signature: (J)V118*/119extern "C"120JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_destroyDwarfContext121(JNIEnv *env, jclass this_cls, jlong context) {122DwarfParser *parser = reinterpret_cast<DwarfParser *>(context);123delete parser;124}125126/*127* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser128* Method: isIn0129* Signature: (J)Z130*/131extern "C"132JNIEXPORT jboolean JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_isIn0133(JNIEnv *env, jobject this_obj, jlong pc) {134DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));135return static_cast<jboolean>(parser->is_in(pc));136}137138/*139* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser140* Method: processDwarf0141* Signature: (J)V142*/143extern "C"144JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_processDwarf0145(JNIEnv *env, jobject this_obj, jlong pc) {146DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));147if (!parser->process_dwarf(pc)) {148jclass ex_cls = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException");149if (!env->ExceptionOccurred()) {150env->ThrowNew(ex_cls, "Could not find PC in DWARF");151}152return;153}154}155156/*157* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser158* Method: getCFARegister159* Signature: ()I160*/161extern "C"162JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getCFARegister163(JNIEnv *env, jobject this_obj) {164DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));165switch (parser->get_cfa_register()) {166case RAX: return sa_RAX;167case RDX: return sa_RDX;168case RCX: return sa_RCX;169case RBX: return sa_RBX;170case RSI: return sa_RSI;171case RDI: return sa_RDI;172case RBP: return sa_RBP;173case RSP: return sa_RSP;174case R8: return sa_R8;175case R9: return sa_R9;176case R10: return sa_R10;177case R11: return sa_R11;178case R12: return sa_R12;179case R13: return sa_R13;180case R14: return sa_R14;181case R15: return sa_R15;182default: return -1;183}184}185186/*187* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser188* Method: getCFAOffset189* Signature: ()I190*/191extern "C"192JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getCFAOffset193(JNIEnv *env, jobject this_obj) {194DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));195return parser->get_cfa_offset();196}197198/*199* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser200* Method: getReturnAddressOffsetFromCFA201* Signature: ()I202*/203extern "C"204JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getReturnAddressOffsetFromCFA205(JNIEnv *env, jobject this_obj) {206DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));207return parser->get_ra_cfa_offset();208}209210/*211* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser212* Method: getBasePointerOffsetFromCFA213* Signature: ()I214*/215extern "C"216JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getBasePointerOffsetFromCFA217(JNIEnv *env, jobject this_obj) {218DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));219return parser->get_bp_cfa_offset();220}221222/*223* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser224* Method: isBPOffsetAvailable225* Signature: ()Z226*/227extern "C"228JNIEXPORT jboolean JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_isBPOffsetAvailable229(JNIEnv *env, jobject this_obj) {230DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));231return parser->is_bp_offset_available();232}233234235236