hotspot/src/os/solaris/dtrace/libjvm_db.c
author coleenp
Sun, 13 Apr 2008 17:43:42 -0400
changeset 360 21d113ecbf6a
parent 1 489c9b5090e2
child 670 ddf3e9583f2f
permissions -rw-r--r--
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <errno.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <gelf.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include "libjvm_db.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
#include "JvmOffsets.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#define LIBJVM_SO "libjvm.so"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#if defined(i386) || defined(__i386) || defined(__amd64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#ifdef COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#define X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#endif /* COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#endif /* i386 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
typedef struct {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    short     vf_cnt; /* number of recognized java vframes */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    short     bci;    /* current frame method byte code index */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    int       line;   /* current frame method source line */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    uint64_t new_fp; /* fp for the next frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    uint64_t new_pc; /* pc for the next frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    uint64_t new_sp; /* "raw" sp for the next frame (includes extension by interpreter/adapter */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    char      locinf; /* indicates there is valid location info */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
} Jframe_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
int Jlookup_by_regs(jvm_agent_t* J, const prgregset_t regs, char *name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
                    size_t size, Jframe_t *jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
int main(int arg) { return arg; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
static int debug = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
static void failed(int err, const char * file, int line) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    fprintf(stderr, "failed %d at %s:%d\n", err, file, line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
static void warn(const char * file, int line, const char * msg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    fprintf(stderr, "warning: %s at %s:%d\n", msg, file, line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
static void warn1(const char * file, int line, const char * msg, intptr_t arg1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    fprintf(stderr, "warning: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    fprintf(stderr, msg, arg1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    fprintf(stderr, " at %s:%d\n", file, line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
#define CHECK_FAIL(err) \
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        if (err != PS_OK) { failed(err, __FILE__, __LINE__); goto fail; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
#define WARN(msg)  warn(__FILE__, __LINE__, msg)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
#define WARN1(msg, arg1)  warn1(__FILE__, __LINE__, msg, arg1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
typedef struct VMStructEntry {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  const char * typeName;           /* The type name containing the given field (example: "Klass") */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  const char * fieldName;          /* The field name within the type           (example: "_name") */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  uint64_t address;                /* Address of field; only used for static fields */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                                   /* ("offset" can not be reused because of apparent SparcWorks compiler bug */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
                                   /* in generation of initializer data) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
} VMStructEntry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
/* Prototyping inlined methods */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
int sprintf(char *s, const char *format, ...);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
#define SZ16  sizeof(int16_t)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
#define SZ32  sizeof(int32_t)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
#define COMP_METHOD_SIGN '*'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
#define MAX_VFRAMES_CNT 256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
typedef struct vframe {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  uint64_t methodOop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  int32_t  sender_decode_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  int32_t  methodIdx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  int32_t  bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  int32_t  line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
} Vframe_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
typedef struct frame {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  uintptr_t fp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  uintptr_t pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  uintptr_t sp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  uintptr_t sender_sp; // The unextended sp of the caller
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
} Frame_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
typedef struct Nmethod_t {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  struct jvm_agent* J;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  Jframe_t *jframe;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  uint64_t nm;                  /* _nmethod */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  uint64_t pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  uint64_t pc_desc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  int32_t  orig_pc_offset;      /* _orig_pc_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  int32_t  instrs_beg;          /* _instructions_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  int32_t  instrs_end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  int32_t  deopt_beg;           /* _deoptimize_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  int32_t  scopes_data_beg;     /* _scopes_data_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  int32_t  scopes_data_end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  int32_t  oops_beg;            /* _oops_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  int32_t  oops_len;            /* _oops_length */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  int32_t  scopes_pcs_beg;      /* _scopes_pcs_offset */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  int32_t  scopes_pcs_end;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  int      vf_cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  Vframe_t vframes[MAX_VFRAMES_CNT];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
} Nmethod_t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
struct jvm_agent {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  struct ps_prochandle* P;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  uint64_t nmethod_vtbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  uint64_t CodeBlob_vtbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  uint64_t BufferBlob_vtbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  uint64_t RuntimeStub_vtbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  uint64_t Universe_methodKlassObj_address;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  uint64_t CodeCache_heap_address;
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   151
  uint64_t Universe_heap_base_address;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  /* Volatiles */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  uint64_t Universe_methodKlassObj;
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   155
  uint64_t Universe_heap_base;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  uint64_t CodeCache_low;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  uint64_t CodeCache_high;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  uint64_t CodeCache_segmap_low;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  uint64_t CodeCache_segmap_high;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  int32_t  SIZE_CodeCache_log2_segment;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  uint64_t methodOopPtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  uint64_t bcx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  Nmethod_t *N;                 /*Inlined methods support */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  Frame_t   prev_fr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  Frame_t   curr_fr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
read_string(struct ps_prochandle *P,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
        char *buf,              /* caller's buffer */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        size_t size,            /* upper limit on bytes to read */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
        uintptr_t addr)         /* address in process */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  int err = PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  while (size-- > 1 && err == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    err = ps_pread(P, addr, buf, 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    if (*buf == '\0') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    addr += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    buf += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   189
static int read_compressed_pointer(jvm_agent_t* J, uint64_t base, uint32_t *ptr) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   190
  int err = -1;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   191
  uint32_t ptr32;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   192
  err = ps_pread(J->P, base, &ptr32, sizeof(uint32_t));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   193
  *ptr = ptr32;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   194
  return err;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   195
}
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   196
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
static int read_pointer(jvm_agent_t* J, uint64_t base, uint64_t* ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  int err = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  uint32_t ptr32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  switch (DATA_MODEL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  case PR_MODEL_LP64:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    err = ps_pread(J->P, base, ptr, sizeof(uint64_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  case PR_MODEL_ILP32:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    err = ps_pread(J->P, base, &ptr32, sizeof(uint32_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    *ptr = ptr32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
static int read_string_pointer(jvm_agent_t* J, uint64_t base, const char ** stringp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  uint64_t ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  char buffer[1024];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  *stringp = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  err = read_pointer(J, base, &ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  if (ptr != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    err = read_string(J->P, buffer, sizeof(buffer), ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    *stringp = strdup(buffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
static int parse_vmstruct_entry(jvm_agent_t* J, uint64_t base, VMStructEntry* vmp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  uint64_t ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  err = read_string_pointer(J, base + OFFSET_VMStructEntrytypeName, &vmp->typeName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  err = read_string_pointer(J, base + OFFSET_VMStructEntryfieldName, &vmp->fieldName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  err = read_pointer(J, base + OFFSET_VMStructEntryaddress, &vmp->address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  if (vmp->typeName != NULL) free((void*)vmp->typeName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  if (vmp->fieldName != NULL) free((void*)vmp->fieldName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
static int parse_vmstructs(jvm_agent_t* J) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  VMStructEntry  vmVar;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  VMStructEntry* vmp = &vmVar;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  uint64_t gHotSpotVMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  psaddr_t sym_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  uint64_t base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  err = ps_pglobal_lookup(J->P, LIBJVM_SO, "gHotSpotVMStructs", &sym_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  err = read_pointer(J, sym_addr, &gHotSpotVMStructs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  base = gHotSpotVMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  err = PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  while (err == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    memset(vmp, 0, sizeof(VMStructEntry));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    err = parse_vmstruct_entry(J, base, vmp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    if (err != PS_OK || vmp->typeName == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    if (vmp->typeName[0] == 'C' && strcmp("CodeCache", vmp->typeName) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
      if (strcmp("_heap", vmp->fieldName) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
        err = read_pointer(J, vmp->address, &J->CodeCache_heap_address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    } else if (vmp->typeName[0] == 'U' && strcmp("Universe", vmp->typeName) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      if (strcmp("_methodKlassObj", vmp->fieldName) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
        J->Universe_methodKlassObj_address = vmp->address;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   282
      if (strcmp("_heap_base", vmp->fieldName) == 0) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   283
        J->Universe_heap_base_address = vmp->address;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   284
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    base += SIZE_VMStructEntry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    if (vmp->typeName != NULL) free((void*)vmp->typeName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    if (vmp->fieldName != NULL) free((void*)vmp->fieldName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  if (vmp->typeName != NULL) free((void*)vmp->typeName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  if (vmp->fieldName != NULL) free((void*)vmp->fieldName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
static int read_volatiles(jvm_agent_t* J) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  uint64_t ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  err = read_pointer(J, J->Universe_methodKlassObj_address, &J->Universe_methodKlassObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  CHECK_FAIL(err);
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   307
  err = read_pointer(J, J->Universe_heap_base_address, &J->Universe_heap_base);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   308
  CHECK_FAIL(err);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  err = read_pointer(J, J->CodeCache_heap_address + OFFSET_CodeHeap_memory +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
                     OFFSET_VirtualSpace_low, &J->CodeCache_low);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  err = read_pointer(J, J->CodeCache_heap_address + OFFSET_CodeHeap_memory +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
                     OFFSET_VirtualSpace_high, &J->CodeCache_high);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  err = read_pointer(J, J->CodeCache_heap_address + OFFSET_CodeHeap_segmap +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
                     OFFSET_VirtualSpace_low, &J->CodeCache_segmap_low);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  err = read_pointer(J, J->CodeCache_heap_address + OFFSET_CodeHeap_segmap +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
                     OFFSET_VirtualSpace_high, &J->CodeCache_segmap_high);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  err = ps_pread(J->P, J->CodeCache_heap_address + OFFSET_CodeHeap_log2_segment_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
                 &J->SIZE_CodeCache_log2_segment, sizeof(J->SIZE_CodeCache_log2_segment));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
static int codecache_contains(jvm_agent_t* J, uint64_t ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  /* make sure the code cache is up to date */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  return (J->CodeCache_low <= ptr && ptr < J->CodeCache_high);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
static uint64_t segment_for(jvm_agent_t* J, uint64_t p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  return (p - J->CodeCache_low) >> J->SIZE_CodeCache_log2_segment;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
static uint64_t block_at(jvm_agent_t* J, int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  return J->CodeCache_low + (i << J->SIZE_CodeCache_log2_segment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
static int find_start(jvm_agent_t* J, uint64_t ptr, uint64_t *startp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  *startp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  if (J->CodeCache_low <= ptr && ptr < J->CodeCache_high) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    int32_t used;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    uint64_t segment = segment_for(J, ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    uint64_t block = J->CodeCache_segmap_low;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    uint8_t tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    err = ps_pread(J->P, block + segment, &tag, sizeof(tag));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    if (tag == 0xff)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
      return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    while (tag > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
      err = ps_pread(J->P, block + segment, &tag, sizeof(tag));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
      segment -= tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    block = block_at(J, segment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    err = ps_pread(J->P, block + OFFSET_HeapBlockHeader_used, &used, sizeof(used));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    if (used) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
      *startp = block + SIZE_HeapBlockHeader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
static int find_symbol(jvm_agent_t* J, const char *name, uint64_t* valuep) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  psaddr_t sym_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  err = ps_pglobal_lookup(J->P, LIBJVM_SO, name, &sym_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  if (err != PS_OK) goto fail;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  *valuep = sym_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
static int find_jlong_constant(jvm_agent_t* J, const char *name, uint64_t* valuep) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  psaddr_t sym_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  int err = ps_pglobal_lookup(J->P, LIBJVM_SO, name, &sym_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  if (err == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    err = ps_pread(J->P, sym_addr, valuep, sizeof(uint64_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  *valuep = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
jvm_agent_t *Jagent_create(struct ps_prochandle *P, int vers) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  jvm_agent_t* J;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  if (vers != JVM_DB_VERSION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    errno = ENOTSUP;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  J = (jvm_agent_t*)calloc(sizeof(struct jvm_agent), 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  debug = getenv("LIBJVMDB_DEBUG") != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  if (debug) debug = 3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
      fprintf(stderr, "Jagent_create: debug=%d\n", debug);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      fprintf(stderr, "Jagent_create: R_SP=%d, R_FP=%d, POINTER_SIZE=%d\n", R_SP, R_FP, POINTER_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
#endif  /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  J->P = P;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  // Initialize the initial previous frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  J->prev_fr.fp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  J->prev_fr.pc = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  J->prev_fr.sp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  J->prev_fr.sender_sp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  err = find_symbol(J, "__1cHnmethodG__vtbl_", &J->nmethod_vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  err = find_symbol(J, "__1cKBufferBlobG__vtbl_", &J->BufferBlob_vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  if (err != PS_OK) J->BufferBlob_vtbl = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  err = find_symbol(J, "__1cICodeBlobG__vtbl_", &J->CodeBlob_vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  err = find_symbol(J, "__1cLRuntimeStubG__vtbl_", &J->RuntimeStub_vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  err = parse_vmstructs(J);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  err = read_volatiles(J);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  return J;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  Jagent_destroy(J);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
void Jagent_destroy(jvm_agent_t *J) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  if (J != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
    free(J);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
static int is_methodOop(jvm_agent_t* J, uint64_t methodOopPtr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  uint64_t klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  int err;
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   461
  // If heap_base is nonnull, this was a compressed oop.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   462
  if (J->Universe_heap_base != NULL) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   463
    uint32_t cklass;
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   464
    err = read_compressed_pointer(J, methodOopPtr + OFFSET_oopDesc_metadata,
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   465
          &cklass);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   466
    // decode heap oop, same as oop.inline.hpp
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   467
    klass = (uint64_t)((uintptr_t)J->Universe_heap_base +
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   468
            ((uintptr_t)cklass << 3));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   469
  } else {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   470
    err = read_pointer(J, methodOopPtr + OFFSET_oopDesc_metadata, &klass);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   471
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  if (err != PS_OK) goto fail;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  return klass == J->Universe_methodKlassObj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
name_for_methodOop(jvm_agent_t* J, uint64_t methodOopPtr, char * result, size_t size)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  short nameIndex;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  short signatureIndex;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  uint64_t constantPool;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  uint64_t constMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  uint64_t nameSymbol;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  uint64_t signatureSymbol;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  uint64_t klassPtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  uint64_t klassSymbol;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  short klassSymbolLength;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  short nameSymbolLength;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  short signatureSymbolLength;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  char * nameString = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  char * klassString = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  char * signatureString = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  int err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  err = read_pointer(J, methodOopPtr + OFFSET_methodOopDesc_constants, &constantPool);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  err = read_pointer(J, methodOopPtr + OFFSET_methodOopDesc_constMethod, &constMethod);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  /* To get name string */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  err = ps_pread(J->P, constMethod + OFFSET_constMethodOopDesc_name_index, &nameIndex, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  err = read_pointer(J, constantPool + nameIndex * POINTER_SIZE + SIZE_constantPoolOopDesc, &nameSymbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  err = ps_pread(J->P, nameSymbol + OFFSET_symbolOopDesc_length, &nameSymbolLength, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  nameString = (char*)calloc(nameSymbolLength + 1, 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  err = ps_pread(J->P, nameSymbol + OFFSET_symbolOopDesc_body, nameString, nameSymbolLength);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  /* To get signature string */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  err = ps_pread(J->P, constMethod + OFFSET_constMethodOopDesc_signature_index, &signatureIndex, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  err = read_pointer(J, constantPool + signatureIndex * POINTER_SIZE + SIZE_constantPoolOopDesc, &signatureSymbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  err = ps_pread(J->P, signatureSymbol + OFFSET_symbolOopDesc_length, &signatureSymbolLength, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  signatureString = (char*)calloc(signatureSymbolLength + 1, 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  err = ps_pread(J->P, signatureSymbol + OFFSET_symbolOopDesc_body, signatureString, signatureSymbolLength);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  /* To get klass string */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  err = read_pointer(J, constantPool + OFFSET_constantPoolOopDesc_pool_holder, &klassPtr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  err = read_pointer(J, klassPtr + OFFSET_Klass_name + SIZE_oopDesc, &klassSymbol);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  err = ps_pread(J->P, klassSymbol + OFFSET_symbolOopDesc_length, &klassSymbolLength, 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  klassString = (char*)calloc(klassSymbolLength + 1, 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  err = ps_pread(J->P, klassSymbol + OFFSET_symbolOopDesc_body, klassString, klassSymbolLength);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  result[0] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  strncat(result, klassString, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  size -= strlen(klassString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  strncat(result, ".", size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  size -= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  strncat(result, nameString, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  size -= strlen(nameString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  strncat(result, signatureString, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  if (nameString != NULL) free(nameString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  if (klassString != NULL) free(klassString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
  if (signatureString != NULL) free(signatureString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
      fprintf(stderr, "name_for_methodOop: FAIL \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  if (nameString != NULL) free(nameString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  if (klassString != NULL) free(klassString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  if (signatureString != NULL) free(signatureString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
static int nmethod_info(Nmethod_t *N)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  jvm_agent_t *J = N->J;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  uint64_t    nm = N->nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
  if (debug > 2 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
      fprintf(stderr, "\t nmethod_info: BEGIN \n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  /* Instructions */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  err = ps_pread(J->P, nm + OFFSET_CodeBlob_instructions_offset, &N->instrs_beg, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  err = ps_pread(J->P, nm + OFFSET_CodeBlob_data_offset, &N->instrs_end, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  err = ps_pread(J->P, nm + OFFSET_nmethod_deoptimize_offset, &N->deopt_beg, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  err = ps_pread(J->P, nm + OFFSET_nmethod_orig_pc_offset, &N->orig_pc_offset, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  /* Oops */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  err = ps_pread(J->P, nm + OFFSET_CodeBlob_oops_offset, &N->oops_beg, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  err = ps_pread(J->P, nm + OFFSET_CodeBlob_oops_length, &N->oops_len, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  /* scopes_pcs */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  err = ps_pread(J->P, nm + OFFSET_nmethod_scopes_pcs_offset, &N->scopes_pcs_beg, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  err = ps_pread(J->P, nm + OFFSET_nmethod_handler_table_offset, &N->scopes_pcs_end, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  /* scopes_data */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  err = ps_pread(J->P, nm + OFFSET_nmethod_scopes_data_offset, &N->scopes_data_beg, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  if (debug > 2 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
      N->scopes_data_end = N->scopes_pcs_beg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
      fprintf(stderr, "\t nmethod_info: instrs_beg: %#x, instrs_end: %#x\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
                       N->instrs_beg, N->instrs_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
      fprintf(stderr, "\t nmethod_info: deopt_beg: %#x \n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
                       N->deopt_beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
      fprintf(stderr, "\t nmethod_info: orig_pc_offset: %#x \n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
                       N->orig_pc_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
      fprintf(stderr, "\t nmethod_info: oops_beg: %#x, oops_len: %#x\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
                       N->oops_beg, N->oops_len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
      fprintf(stderr, "\t nmethod_info: scopes_data_beg: %#x, scopes_data_end: %#x\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
                       N->scopes_data_beg, N->scopes_data_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
      fprintf(stderr, "\t nmethod_info: scopes_pcs_beg: %#x, scopes_pcs_end: %#x\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
                       N->scopes_pcs_beg, N->scopes_pcs_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
      fprintf(stderr, "\t nmethod_info: END \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
raw_read_int(jvm_agent_t* J, uint64_t *buffer, int32_t *val)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  int shift = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
  int value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  uint8_t ch = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
  int32_t  err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
  int32_t sum;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
  // Constants for UNSIGNED5 coding of Pack200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  // see compressedStream.hpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
    lg_H = 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
    H = 1<<lg_H,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
    BitsPerByte = 8,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
    L = (1<<BitsPerByte)-H,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  err = ps_pread(J->P, (*buffer)++, &ch, sizeof(uint8_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
      fprintf(stderr, "\t\t\t raw_read_int: *buffer: %#llx, ch: %#x\n", *buffer, ch);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  sum = ch;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  if ( sum >= L ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
    int32_t lg_H_i = lg_H;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
    // Read maximum of 5 total bytes (we've already read 1).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
    // See CompressedReadStream::read_int_mb
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
    for ( i = 0;  i < 4; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
      err = ps_pread(J->P, (*buffer)++, &ch, sizeof(uint8_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
      sum += ch << lg_H_i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
      if (ch < L ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
        *val = sum;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
        return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
      lg_H_i += lg_H;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  *val = sum;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
read_pair(jvm_agent_t* J, uint64_t *buffer, int32_t *bci, int32_t *line)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
  uint8_t next = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
  int32_t bci_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
  int32_t line_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
      fprintf(stderr, "\t\t read_pair: BEGIN\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
  err = ps_pread(J->P, (*buffer)++, &next, sizeof(uint8_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  if (next == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
      if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
          fprintf(stderr, "\t\t read_pair: END: next == 0\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
      return 1; /* stream terminated */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  if (next == 0xFF) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
      if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
          fprintf(stderr, "\t\t read_pair: END: next == 0xFF\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
      /* Escape character, regular compression used */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
      err = raw_read_int(J, buffer, &bci_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
      err = raw_read_int(J, buffer, &line_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
      *bci  += bci_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
      *line += line_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
      if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
          fprintf(stderr, "\t\t read_pair: delta = (line %d: %d)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
                          line_delta, bci_delta);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
          fprintf(stderr, "\t\t read_pair: unpack= (line %d: %d)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
                          *line, *bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
      /* Single byte compression used */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
      *bci  += next >> 3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
      *line += next & 0x7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
      if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
          fprintf(stderr, "\t\t read_pair: delta = (line %d: %d)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
                          next & 0x7, next >> 3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
          fprintf(stderr, "\t\t read_pair: unpack= (line %d: %d)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
                          *line, *bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
      fprintf(stderr, "\t\t read_pair: END\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
      fprintf(stderr, "\t\t read_pair: FAIL\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
line_number_from_bci(jvm_agent_t* J, Vframe_t *vf)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  uint64_t buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
  uint16_t code_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  uint64_t code_end_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
  uint64_t constMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  int8_t   access_flags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  int32_t  best_bci    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  int32_t  stream_bci  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  int32_t  stream_line = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  int32_t  err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
  if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
      char name[256];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
      err = name_for_methodOop(J, vf->methodOop, name, 256);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
      fprintf(stderr, "\t line_number_from_bci: BEGIN, method name: %s, targ bci: %d\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
                       name, vf->bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  err = read_pointer(J, vf->methodOop + OFFSET_methodOopDesc_constMethod, &constMethod);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  vf->line = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  err = ps_pread(J->P, constMethod + OFFSET_constMethodOopDesc_flags, &access_flags, sizeof(int8_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
  if (!(access_flags & constMethodOopDesc_has_linenumber_table)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
      if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
          fprintf(stderr, "\t line_number_from_bci: END: !HAS_LINE_NUMBER_TABLE \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
      return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  /*  The line numbers are a short array of 2-tuples [start_pc, line_number].
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
   *  Not necessarily sorted and not necessarily one-to-one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
  err = ps_pread(J->P, constMethod + OFFSET_constMethodOopDesc_code_size, &code_size, SZ16);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  /* inlined_table_start() */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
  code_end_delta = (uint64_t) (access_flags & AccessFlags_NATIVE) ? 2*POINTER_SIZE : 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  buffer = constMethod + (uint64_t) SIZE_constMethodOopDesc + (uint64_t) code_size + code_end_delta;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
  if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
      fprintf(stderr, "\t\t line_number_from_bci: methodOop: %#llx, native: %d\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
                      vf->methodOop, (access_flags & AccessFlags_NATIVE));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
      fprintf(stderr, "\t\t line_number_from_bci: buffer: %#llx, code_size: %d\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
                      buffer, (int) code_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
  while (read_pair(J, &buffer, &stream_bci, &stream_line) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      if (stream_bci == vf->bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
          /* perfect match */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
          if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
              fprintf(stderr, "\t line_number_from_bci: END: exact line: %ld \n\n", vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
          vf->line = stream_line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
          return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
          /* update best_bci/line */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
          if (stream_bci < vf->bci && stream_bci >= best_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
              best_bci = stream_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
              vf->line = stream_line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
              if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
                  fprintf(stderr, "\t line_number_from_bci: best_bci: %ld, best_line: %ld\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
                                   best_bci, vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
      fprintf(stderr, "\t line_number_from_bci: END: line: %ld \n\n", vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
      fprintf(stderr, "\t line_number_from_bci: FAIL\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
get_real_pc(Nmethod_t *N, uint64_t pc_desc, uint64_t *real_pc)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
  int32_t pc_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  err = ps_pread(N->J->P, pc_desc + OFFSET_PcDesc_pc_offset, &pc_offset, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
  *real_pc = N->nm + N->instrs_beg + pc_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
      fprintf(stderr, "\t\t get_real_pc: pc_offset: %lx, real_pc: %llx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
                       pc_offset, *real_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
/* Finds a PcDesc with real-pc equal to N->pc */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
static int pc_desc_at(Nmethod_t *N)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  uint64_t pc_diff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  int32_t offs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
      fprintf(stderr, "\t pc_desc_at: BEGIN\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
  N->vf_cnt  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  N->pc_desc = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  for (offs = N->scopes_pcs_beg; offs < N->scopes_pcs_end; offs += SIZE_PcDesc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
      uint64_t pd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
      uint64_t best_pc_diff = 16;       /* some approximation */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
      uint64_t real_pc = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
      pd = N->nm + offs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
      err = get_real_pc(N, pd, &real_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
      pc_diff = real_pc - N->pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
      /* In general, this fragment should work */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
      if (pc_diff == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
          N->pc_desc = pd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
          if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
            fprintf(stderr, "\t pc_desc_at: END: pc_desc: FOUND: %#lx \n\n", pd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
          return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
      /* This fragment is to be able to find out an appropriate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
       * pc_desc entry even if pc_desc info is inaccurate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
      if (best_pc_diff > pc_diff && pc_diff > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
          best_pc_diff = pc_diff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
          N->pc_desc = pd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
      fprintf(stderr, "\t pc_desc_at: END: pc_desc NOT FOUND");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
      if (pc_diff < 20)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
          fprintf(stderr, ", best pc_diff: %d\n\n", pc_diff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
      else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
          fprintf(stderr, "\n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
scope_desc_at(Nmethod_t *N, int32_t decode_offset, Vframe_t *vf)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
  uint64_t buffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
  int32_t  err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
      fprintf(stderr, "\t\t scope_desc_at: BEGIN \n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
  buffer = N->nm + N->scopes_data_beg + decode_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  err = raw_read_int(N->J, &buffer, &vf->sender_decode_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
  err = raw_read_int(N->J, &buffer, &vf->methodIdx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
  err = raw_read_int(N->J, &buffer, &vf->bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
  if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
      fprintf(stderr, "\t\t scope_desc_at: sender_decode_offset: %#x\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
                      vf->sender_decode_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
      fprintf(stderr, "\t\t scope_desc_at: methodIdx: %d\n", vf->methodIdx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
      fprintf(stderr, "\t\t scope_desc_at: bci: %d\n", vf->bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
      fprintf(stderr, "\t\t scope_desc_at: END \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
scopeDesc_chain(Nmethod_t *N)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
  int32_t decode_offset = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
      fprintf(stderr, "\t scopeDesc_chain: BEGIN\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
  err = ps_pread(N->J->P, N->pc_desc + OFFSET_PcDesc_scope_decode_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
                 &decode_offset, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
  while (decode_offset > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
      if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
          fprintf(stderr, "\t scopeDesc_chain: decode_offset: %#x\n", decode_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
      Vframe_t *vf = &N->vframes[N->vf_cnt];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
      err = scope_desc_at(N, decode_offset, vf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
      if (vf->methodIdx > N->oops_len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
          fprintf(stderr, "\t scopeDesc_chain: (methodIdx > oops_len) !\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
          return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
      err = read_pointer(N->J, N->nm + N->oops_beg + (vf->methodIdx-1)*POINTER_SIZE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
                               &vf->methodOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
      if (vf->methodOop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
          N->vf_cnt++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
          err = line_number_from_bci(N->J, vf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
          CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
          if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
              fprintf(stderr, "\t scopeDesc_chain: methodOop: %#8llx, line: %ld\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
                              vf->methodOop, vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
      decode_offset = vf->sender_decode_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
  if (debug > 2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
      fprintf(stderr, "\t scopeDesc_chain: END \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
      fprintf(stderr, "\t scopeDesc_chain: FAIL \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
name_for_nmethod(jvm_agent_t* J,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
                 uint64_t nm,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
                 uint64_t pc,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
                 uint64_t methodOop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
                 char *result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
                 size_t size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
                 Jframe_t *jframe
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
  Nmethod_t *N;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
  Vframe_t *vf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
  int deoptimized = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
      fprintf(stderr, "name_for_nmethod: BEGIN: nmethod: %#llx, pc: %#llx\n", nm, pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
  if (J->N == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
    J->N = (Nmethod_t *) malloc(sizeof(Nmethod_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
  memset(J->N, 0, sizeof(Nmethod_t));   /* Initial stat: all values are zeros */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
  N     = J->N;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
  N->J  = J;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
  N->nm = nm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
  N->pc = pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
  N->jframe = jframe;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
  err = nmethod_info(N);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
      fprintf(stderr, "name_for_nmethod: pc: %#llx, deopt_pc:  %#llx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
              pc, N->nm + N->deopt_beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
  /* check for a deoptimized frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  if ( pc == N->nm + N->deopt_beg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
    uint64_t base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
    if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
        fprintf(stderr, "name_for_nmethod: found deoptimized frame\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
    if (J->prev_fr.sender_sp != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
      base = J->prev_fr.sender_sp + N->orig_pc_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
      base = J->curr_fr.sp + N->orig_pc_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
    err = read_pointer(J, base, &N->pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
    if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
        fprintf(stderr, "name_for_nmethod: found deoptimized frame converting pc from %#8llx to %#8llx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
        pc,  N->pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
    deoptimized = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
  err = pc_desc_at(N);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
  if (N->pc_desc > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
      jframe->locinf = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
      err = scopeDesc_chain(N);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
  result[0] = COMP_METHOD_SIGN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
  vf = &N->vframes[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
  if (N->vf_cnt > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
      jframe->vf_cnt = N->vf_cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
      jframe->bci  = vf->bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
      jframe->line = vf->line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
      err = name_for_methodOop(J, N->vframes[0].methodOop, result+1, size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
      err = name_for_methodOop(J, methodOop, result+1, size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
  if (deoptimized) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
    strncat(result + 1, " [deoptimized frame]; ", size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
    strncat(result + 1, " [compiled] ", size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
      fprintf(stderr, "name_for_nmethod: END: method name: %s, vf_cnt: %d\n\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
                      result, N->vf_cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
      fprintf(stderr, "name_for_nmethod: FAIL \n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
int is_bci(intptr_t bcx) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
  switch (DATA_MODEL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
  case PR_MODEL_LP64:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
    return ((uintptr_t) bcx) <= ((uintptr_t) MAX_METHOD_CODE_SIZE) ;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1067
  case PR_MODEL_ILP32:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
  default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
    return 0 <= bcx && bcx <= MAX_METHOD_CODE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
name_for_imethod(jvm_agent_t* J,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
                 uint64_t bcx,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
                 uint64_t methodOop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
                 char *result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
                 size_t size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
                 Jframe_t *jframe
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
  uint64_t bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
  uint64_t constMethod;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
  Vframe_t vframe = {0};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
  Vframe_t *vf = &vframe;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
  int32_t   err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
  err = read_pointer(J, methodOop + OFFSET_methodOopDesc_constMethod, &constMethod);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
  bci = is_bci(bcx) ? bcx : bcx - (constMethod + (uint64_t) SIZE_constMethodOopDesc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
      fprintf(stderr, "\t name_for_imethod: BEGIN: methodOop: %#llx\n", methodOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
  err = name_for_methodOop(J, methodOop, result, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
      fprintf(stderr, "\t name_for_imethod: method name: %s\n", result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
  if (bci > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
      vf->methodOop = methodOop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
      vf->bci       = bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
      err = line_number_from_bci(J, vf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
  jframe->bci  = vf->bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
  jframe->line = vf->line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
  jframe->locinf = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
      fprintf(stderr, "\t name_for_imethod: END: bci: %d, line: %d\n\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
                      vf->bci, vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
      fprintf(stderr, "\t name_for_imethod: FAIL\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
static int
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
name_for_codecache(jvm_agent_t* J, uint64_t fp, uint64_t pc, char * result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
                   size_t size, Jframe_t *jframe, int* is_interpreted)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
  uint64_t start;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
  uint64_t vtbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
  int32_t err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
  *is_interpreted = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
  result[0] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
  err = find_start(J, pc, &start);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
  err = read_pointer(J, start, &vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
  if (vtbl == J->nmethod_vtbl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
    uint64_t methodOop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
    err = read_pointer(J, start + OFFSET_nmethod_method, &methodOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
    if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
        fprintf(stderr, "name_for_codecache: start: %#8llx, pc: %#8llx, methodOop: %#8llx \n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
                        start, pc, methodOop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
    err = name_for_nmethod(J, start, pc, methodOop, result, size, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
  } else if (vtbl == J->BufferBlob_vtbl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
    const char * name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
    err = read_string_pointer(J, start + OFFSET_CodeBlob_name, &name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
    /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
     * Temporary usage of string "Interpreter".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
     * We need some other way to distinguish "StubRoutines"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
     * and regular interpreted frames.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
    if (err == PS_OK && strncmp(name, "Interpreter", 11) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
      *is_interpreted = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
      if (is_methodOop(J, J->methodOopPtr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
        return name_for_imethod(J, J->bcx, J->methodOopPtr, result, size, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
    if (err == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
      strncpy(result, name, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
      free((void*)name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
      strncpy(result, "<unknown BufferBlob>", size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
    /* return PS_OK; */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
    const char * name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
    err = read_string_pointer(J, start + OFFSET_CodeBlob_name, &name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
    if (err == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
      strncpy(result, name, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
      free((void*)name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
      strncpy(result, "<unknown CodeBlob>", size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
      WARN1("unknown CodeBlob: vtbl = 0x%x", vtbl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
  result[size-1] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
  if (vtbl != J->RuntimeStub_vtbl) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
    uint64_t trial_pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
    int frame_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
    err = ps_pread(J->P, start + OFFSET_CodeBlob_frame_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
                         &frame_size, SZ32);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
    // frame_size is in words, we want bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
    frame_size *= POINTER_SIZE; /* word => byte conversion */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
    /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
      Because c2 doesn't use FP as a framepointer the value of sp/fp we receive
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
      in the initial entry to a set of stack frames containing server frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
      will pretty much be nonsense. We can detect that nonsense by looking to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
      see if the PC we received is correct if we look at the expected storage
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
      location in relation to the FP (ie. POINTER_SIZE(FP) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
    err = read_pointer(J, fp + POINTER_SIZE , &trial_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
    if ( (err != PS_OK || trial_pc != pc) && frame_size > 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
      // Either we couldn't even read at the "fp" or the pc didn't match
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
      // both are sure clues that the fp is bogus. We no search the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
      // for a reasonable number of words trying to find the bogus fp
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
      // and the current pc in adjacent words. The we will be able to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
      // deduce an approximation of the frame pointer and actually get
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
      // the correct stack pointer. Which we can then unwind for the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
      // next frame.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
      int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
      uint64_t check;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
      uint64_t base = J->curr_fr.sp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
      uint64_t prev_fp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
      for ( i = 0; i < frame_size * 5 ; i++, base += POINTER_SIZE ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
        err = read_pointer(J, base , &check);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
        CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
        if (check == fp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
          base += POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
          err = read_pointer(J, base , &check);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
          CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
          if (check == pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
            if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
              fprintf(stderr, "name_for_codecache: found matching fp/pc combo at 0x%llx\n", base - POINTER_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
            prev_fp = base - 2 * POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
      if ( prev_fp != 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
        // real_sp is the sp we should have received for this frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
        uint64_t real_sp = prev_fp + 2 * POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
        // +POINTER_SIZE because callee owns the return address so caller's sp is +1 word
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
        jframe->new_sp = real_sp + frame_size + POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
        err = read_pointer(J, jframe->new_sp - POINTER_SIZE , &jframe->new_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
        CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
        err = read_pointer(J, jframe->new_sp - 2*POINTER_SIZE, &jframe->new_fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
        CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
        return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
    /* A prototype to workaround FP absence */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
    /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
     * frame_size can be 0 for StubRoutines (1) frame.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
     * In this case it should work with fp as usual.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
    if (frame_size > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
      jframe->new_fp = J->prev_fr.fp + frame_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
      jframe->new_sp = jframe->new_fp + 2 * POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
      memset(&J->curr_fr, 0, sizeof(Frame_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
      err = read_pointer(J,  fp, &jframe->new_fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
      err = read_pointer(J,  jframe->new_fp + POINTER_SIZE,  &jframe->new_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
      CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
    if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
      fprintf(stderr, "name_for_codecache: %s, frame_size=%#lx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
                       result, frame_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
      fprintf(stderr, "name_for_codecache: prev_fr.fp=%#lx, fp=%#lx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
                       J->prev_fr.fp, jframe->new_fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
#endif /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
int Jget_vframe(jvm_agent_t* J, int vframe_no,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
                char *name, size_t size, Jframe_t *jframe)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
  Nmethod_t *N = J->N;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
  Vframe_t  *vf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
  int32_t   err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
  if (vframe_no >= N->vf_cnt) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
     (void) sprintf(name, "Wrong inlinedMethod%1d()", vframe_no);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
     return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
  vf = N->vframes + vframe_no;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
  name[0] = COMP_METHOD_SIGN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
  err = name_for_methodOop(J, vf->methodOop, name + 1, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
  CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
  jframe->bci = vf->bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
  jframe->line = vf->line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
      fprintf(stderr, "\t Jget_vframe: method name: %s, line: %ld\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
                       name, vf->line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
  return PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
  if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
      fprintf(stderr, "\t Jget_vframe: FAIL\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
#define MAX_SYM_SIZE 256
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
int Jlookup_by_regs(jvm_agent_t* J, const prgregset_t regs, char *name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
                    size_t size, Jframe_t *jframe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
  uintptr_t fp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
  uintptr_t pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
  /* arguments given to read_pointer need to be worst case sized */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
  uint64_t methodOopPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
  uint64_t sender_sp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
  uint64_t bcx = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
  int is_interpreted = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
  int result = PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
  int err = PS_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
  if (J == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
  jframe->vf_cnt = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
  jframe->new_fp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
  jframe->new_pc = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
  jframe->line   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
  jframe->bci    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
  jframe->locinf = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
  read_volatiles(J);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
  pc = (uintptr_t) regs[R_PC];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
  J->curr_fr.pc = pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
  J->curr_fr.fp = regs[R_FP];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1341
  J->curr_fr.sp = regs[R_SP];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
      fprintf(stderr, "Jlookup_by_regs: BEGINs: fp=%#lx, pc=%#lx\n", regs[R_FP], pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
#if defined(sparc) || defined(__sparc)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
    /* The following workaround is for SPARC. CALL instruction occupates 8 bytes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
     * In the pcDesc structure return pc offset is recorded for CALL instructions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
     * regs[R_PC] contains a CALL instruction pc offset.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
    pc += 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1352
    bcx          = (uintptr_t) regs[R_L1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
    methodOopPtr = (uintptr_t) regs[R_L2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1354
    sender_sp = regs[R_I5];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
    if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
        fprintf(stderr, "\nregs[R_I1]=%lx, regs[R_I2]=%lx, regs[R_I5]=%lx, regs[R_L1]=%lx, regs[R_L2]=%lx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
                         regs[R_I1], regs[R_I2], regs[R_I5], regs[R_L1], regs[R_L2]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1358
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
#elif defined(i386) || defined(__i386) || defined(__amd64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1360
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1361
    fp = (uintptr_t) regs[R_FP];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
    if (J->prev_fr.fp == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
        /* A workaround for top java frames */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
        J->prev_fr.fp = (uintptr_t)(regs[R_SP] - 2 * POINTER_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
        J->prev_fr.fp = (uintptr_t)(regs[R_SP] - POINTER_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1368
#endif /* COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1370
    if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
        printf("Jlookup_by_regs: J->prev_fr.fp = %#lx\n", J->prev_fr.fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
    if (read_pointer(J,  fp + OFFSET_interpreter_frame_method, &methodOopPtr) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
      methodOopPtr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
    if (read_pointer(J,  fp + OFFSET_interpreter_frame_sender_sp, &sender_sp) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
      sender_sp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
    if (read_pointer(J,  fp + OFFSET_interpreter_frame_bcx_offset, &bcx) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
      bcx = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1382
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
#endif /* i386 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
  J->methodOopPtr = methodOopPtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
  J->bcx = bcx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1387
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1388
  /* On x86 with C2 JVM: native frame may have wrong regs[R_FP]
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1389
   * For example: JVM_SuspendThread frame poins to the top interpreted frame.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
   * If we call is_methodOop(J, methodOopPtr) before codecache_contains(J, pc)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1391
   * then we go over and omit both: nmethod and I2CAdapter frames.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1392
   * Note, that regs[R_PC] is always correct if frame defined correctly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
   * So it is better to call codecache_contains(J, pc) from the beginning.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1395
#ifndef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1396
  if (is_methodOop(J, J->methodOopPtr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1397
    result = name_for_imethod(J, bcx, J->methodOopPtr, name, size, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1398
    /* If the methodOopPtr is a method then this is highly likely to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
       an interpreter frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
    if (result >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1401
      is_interpreted = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
#endif /* ! X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
  if (codecache_contains(J, pc)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
    result = name_for_codecache(J, fp, pc, name, size, jframe, &is_interpreted);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
  else if (is_methodOop(J, J->methodOopPtr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
    result = name_for_imethod(J, bcx, J->methodOopPtr, name, size, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
    /* If the methodOopPtr is a method then this is highly likely to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
       an interpreter frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
    if (result >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
      is_interpreted = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1418
#endif /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1419
  else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
    if (debug) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
        fprintf(stderr, "Jlookup_by_regs: END with -1\n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
    result = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
  if (!is_interpreted) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1426
    sender_sp = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1428
  J->curr_fr.sender_sp = sender_sp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1431
  if (!J->curr_fr.fp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
    J->curr_fr.fp = (jframe->new_fp) ? jframe->new_fp : (uintptr_t)regs[R_FP];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
  if (!jframe->new_pc && jframe->new_fp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
    // This seems dubious
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
    read_pointer(J,  jframe->new_fp + POINTER_SIZE,  &jframe->new_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1437
    CHECK_FAIL(err);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
    if (debug > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1439
        printf("Jlookup_by_regs: (update pc) jframe->new_fp: %#llx, jframe->new_pc: %#llx\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
               jframe->new_fp, jframe->new_pc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
#endif /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
  J->prev_fr = J->curr_fr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1446
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1447
  if (debug)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1448
      fprintf(stderr, "Jlookup_by_regs: END\n\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
 fail:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
void update_gregs(prgregset_t gregs, Jframe_t jframe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1458
    if (debug > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
      fprintf(stderr, "update_gregs: before update sp = 0x%llx, fp = 0x%llx, pc = 0x%llx\n", gregs[R_SP], gregs[R_FP], gregs[R_PC]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1461
    /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1462
     * A workaround for java C2 frames with unconventional FP.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1463
     * may have to modify regset with new values for FP/PC/SP when needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1464
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1465
     if (jframe.new_sp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1466
         *((uintptr_t *) &gregs[R_SP]) = (uintptr_t) jframe.new_sp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1467
     } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1468
         // *((uintptr_t *) &gregs[R_SP]) = (uintptr_t) gregs[R_FP] + 2 * POINTER_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1469
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1470
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1471
     if (jframe.new_fp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1472
         *((uintptr_t *) &gregs[R_FP]) = (uintptr_t) jframe.new_fp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1473
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1474
     if (jframe.new_pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1475
         *((uintptr_t *) &gregs[R_PC]) = (uintptr_t) jframe.new_pc;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1476
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1477
    if (debug > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1478
      fprintf(stderr, "update_gregs: after update sp = 0x%llx, fp = 0x%llx, pc = 0x%llx\n", gregs[R_SP], gregs[R_FP], gregs[R_PC]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1479
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1480
#endif  /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1481
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1482
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1483
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1484
 * Iterates over java frames at current location given by 'gregs'.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1485
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1486
 *  Returns -1 if no java frames are present or if an error is encountered.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1487
 *  Returns the result of calling 'func' if the return value is non-zero.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1488
 *  Returns 0 otherwise.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1489
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1490
int Jframe_iter(jvm_agent_t *J, prgregset_t gregs, java_stack_f *func, void* cld) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1491
    char buf[MAX_SYM_SIZE + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1492
    Jframe_t jframe;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1493
    int i = 0, res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1494
#ifdef X86_COMPILER2
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1495
    if (debug > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1496
      fprintf(stderr, "Jframe_iter: Entry sp = 0x%llx, fp = 0x%llx, pc = 0x%llx\n", gregs[R_SP], gregs[R_FP], gregs[R_PC]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1497
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1498
#endif  /* X86_COMPILER2 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1499
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1500
    memset(&jframe, 0, sizeof(Jframe_t));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1501
    memset(buf, 0, sizeof(buf));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1502
    res =  Jlookup_by_regs(J, gregs, buf, sizeof(buf), &jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1503
    if (res != PS_OK)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1504
        return (-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1505
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1506
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1507
    res = func(cld, gregs, buf, (jframe.locinf)? jframe.bci : -1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1508
               jframe.line, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1509
    if (res != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1510
        update_gregs(gregs, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1511
        return (res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1512
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1513
    for (i = 1; i < jframe.vf_cnt; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1514
        Jget_vframe(J, i, buf, sizeof(buf), &jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1515
        res = func(cld, gregs, buf, (jframe.locinf)? jframe.bci : -1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1516
                   jframe.line, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1517
        if (res != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1518
            update_gregs(gregs, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1519
            return (res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1520
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1521
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1522
    update_gregs(gregs, jframe);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1523
    return (0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1524
}