Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide disassemblage for JIT methods #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ CMakeCache.txt
cmake_install.cmake
Makefile
*~
*.svg
out
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()
INCLUDE_DIRECTORIES(${JAVA_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${JAVA_INCLUDE_PATH2})

add_library(perfmap SHARED src/c/perf-map-agent.c src/c/perf-map-file.c)
add_library(perfmap SHARED src/c/perf-map-agent.c src/c/perf-map-file.c src/c/disassembling.c src/c/symtab.c src/c/salibelf.c src/c/libproc_impl.c)

find_package(Java REQUIRED)
include(UseJava)
Expand Down
18 changes: 18 additions & 0 deletions bin/java-dump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -e
set -x

CUR_DIR=`pwd`
ATTACH_JAR=attach-main.jar
PERF_MAP_DIR=$(dirname $(readlink -f $0))/..
ATTACH_JAR_PATH=$PERF_MAP_DIR/out/$ATTACH_JAR

if [ -z "$JAVA_HOME" ]; then
JAVA_HOME=/usr/lib/jvm/default-java
fi

[ -d "$JAVA_HOME" ] || JAVA_HOME=/etc/alternatives/java_sdk
[ -d "$JAVA_HOME" ] || (echo "JAVA_HOME directory at '$JAVA_HOME' does not exist." && false)

echo $* >> output.log
(cd $PERF_MAP_DIR/out && sudo -u johannes java -cp $ATTACH_JAR_PATH:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $*)
2 changes: 1 addition & 1 deletion bin/perf-java-top
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ PERF_MAP_DIR=$(dirname $(readlink -f $0))/..

PID=$1
$PERF_MAP_DIR/bin/create-java-perf-map.sh $PID "$PERF_MAP_OPTIONS"
sudo perf top -p $*
sudo perf top --objdump $PERF_MAP_DIR/bin/java-dump.sh -p $*
143 changes: 143 additions & 0 deletions src/c/disassembling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#define _GNU_SOURCE

#include <stdio.h>

#include <error.h>
#include <errno.h>
#include <dlfcn.h>
#include <link.h>
#include <jni.h>
#include <jvmti.h>

#include "disassembling.h"
#include "symtab.h"

load_library_f *load_library;
decode_nmethod_f *decode_nmethod;
checked_resolve_f *checked_resolve_jmethod_id;
decode_address_f *decode_address;

void* fdStream_vt;

typedef struct {
const char* typeName; // The type name containing the given field (example: "Klass")
const char* fieldName; // The field name within the type (example: "_name")
const char* typeString; // Quoted name of the type of this field (example: "Symbol*";
// parsed in Java to ensure type correctness
int32_t isStatic; // Indicates whether following field is an offset or an address
uint64_t offset; // Offset of field within structure; only used for nonstatic fields
void* address; // Address of field; only used for static fields
// ("offset" can not be reused because of apparent SparcWorks compiler bug
// in generation of initializer data)
} VMStructEntry;

typedef struct {
const char* typeName; // Type name (example: "methodOopDesc")
const char* superclassName; // Superclass name, or null if none (example: "oopDesc")
int32_t isOopType; // Does this type represent an oop typedef? (i.e., "methodOop" or
// "klassOop", but NOT "methodOopDesc")
int32_t isIntegerType; // Does this type represent an integer type (of arbitrary size)?
int32_t isUnsigned; // If so, is it unsigned?
uint64_t size; // Size, in bytes, of the type
} VMTypeEntry;

VMStructEntry *code_entry;
long code_offset;

void findCode(void *libjvm_handle) {
VMStructEntry *entry = *((VMStructEntry **)dlsym(libjvm_handle, "gHotSpotVMStructs"));

while (entry->fieldName) {
printf("%s.%s\n", entry->typeName, entry->fieldName);
if (strcmp(entry->typeName, "methodOopDesc") == 0 &&
strcmp(entry->fieldName, "_code") == 0) {
code_entry = entry;
code_offset = entry -> offset;
return;
}
entry++;
}
}

void decode(jmethodID method) {
char *methodoop = checked_resolve_jmethod_id(method);
void **code_ptr = methodoop + code_offset;
printf("a: %lx b: %lx oop: %lx code: %lx\n", *((void**)method), checked_resolve_jmethod_id(method), methodoop, *code_ptr);
decode_nmethod(*code_ptr, NULL);
}

struct link_map *map;
struct symtab *res;

void *find_symbol(char *symbol) {
int size;
void *ptr = search_symbol(res, map->l_addr, symbol, &size);
printf("%s Result: %lx\n", symbol, ptr);
return ptr;
}

void newFdStream(int fd, void *buffer) {
memset(buffer, 0, 0x70);
*((void**)(buffer + 0x00)) = fdStream_vt + 0x10;
*((void**)(buffer + 0x0c)) = 80; // width
*((int**) (buffer + 0x28)) = fd;
}

void accessDisassembler() {
//dlopen("/tmp/hsdis-amd64.so", RTLD_LAZY);
void *jvm = dlopen("libjvm.so", RTLD_LAZY);
printf("Got jvm %lx\n", jvm);

findCode(jvm);

dlinfo(jvm, RTLD_DI_LINKMAP, &map);
printf("Full name: %s, base: %lx\n", map->l_name, map->l_addr);

char *path = map->l_name;
FILE *file = fopen(path, "r");
printf("Got file %lx fd %d\n", file, fileno(file));
res = build_symtab(fileno(file), path);
printf("Got syms %lx\n", res);

char *sym = "_ZN12Disassembler12load_libraryEv";
int size;
void *ptr = search_symbol(res, map->l_addr, sym, &size);
printf("Result: %lx\n", ptr);
load_library = ptr;

char *decodeInsns = "_ZN12Disassembler6decodeEP7nmethodP12outputStream";
ptr = search_symbol(res, map->l_addr, decodeInsns, &size);
printf("decode Result: %lx\n", ptr);
decode_nmethod = ptr;

char *decodeCodeBlobInsns = "_ZN12Disassembler6decodeEP8CodeBlobP12outputStream";
ptr = search_symbol(res, map->l_addr, decodeCodeBlobInsns, &size);
printf("decodeCodeBlob Result: %lx\n", ptr);
decode_codeblob = ptr;

char *checked_str = "_ZN10JNIHandles26checked_resolve_jmethod_idEP10_jmethodID";
ptr = search_symbol(res, map->l_addr, checked_str, &size);
printf("checked resolve Result: %lx\n", ptr);
checked_resolve_jmethod_id = ptr;

char *decode_addr_str = "_ZN10decode_envC2EP8CodeBlobP12outputStream11CodeStrings";
ptr = search_symbol(res, map->l_addr, decode_addr_str, &size);
printf("decode_addr Result: %lx\n", ptr);
decode_address = ptr;

char *find_blob_str = "_ZN9CodeCache9find_blobEPv";
ptr = search_symbol(res, map->l_addr, find_blob_str, &size);
printf("find_blob Result: %lx\n", ptr);
find_blob = ptr;

fdStream_vt = find_symbol("_ZTV8fdStream");

_ps = find_symbol("_ps");

load_library();
}

//

//uintptr_t search_symbol(struct symtab* symtab, uintptr_t base,
// const char *sym_name, int *sym_size);
28 changes: 28 additions & 0 deletions src/c/disassembling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
void accessDisassembler();

typedef int(load_library_f());
load_library_f *load_library;

typedef void(decode_nmethod_f(void *,void*));
decode_nmethod_f *decode_nmethod;

typedef void(decode_codeblob_f(void *,void*));
decode_codeblob_f *decode_codeblob;

typedef void*(checked_resolve_f(jmethod));
checked_resolve_f *checked_resolve_jmethod_id;

//static void decode(address begin, address end, outputStream* st = NULL, CodeStrings c = CodeStrings());
typedef void(decode_address_f(void *, void*, void*, void*));
decode_address_f *decode_address;

long code_offset;

typedef void *(find_blob_f(void*));
find_blob_f *find_blob;

void newFdStream(int fd, void *buffer);

typedef void (_ps_f());
_ps_f *_ps;

54 changes: 54 additions & 0 deletions src/c/elfmacros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef _ELFMACROS_H_
#define _ELFMACROS_H_

#if defined(_LP64)
#define ELF_EHDR Elf64_Ehdr
#define ELF_SHDR Elf64_Shdr
#define ELF_PHDR Elf64_Phdr
#define ELF_SYM Elf64_Sym
#define ELF_NHDR Elf64_Nhdr
#define ELF_DYN Elf64_Dyn
#define ELF_ADDR Elf64_Addr

#define ELF_ST_TYPE ELF64_ST_TYPE

#else

#define ELF_EHDR Elf32_Ehdr
#define ELF_SHDR Elf32_Shdr
#define ELF_PHDR Elf32_Phdr
#define ELF_SYM Elf32_Sym
#define ELF_NHDR Elf32_Nhdr
#define ELF_DYN Elf32_Dyn
#define ELF_ADDR Elf32_Addr

#define ELF_ST_TYPE ELF32_ST_TYPE

#endif


#endif /* _ELFMACROS_H_ */
Loading