hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m
author jwilhelm
Thu, 06 Jul 2017 01:50:26 +0200
changeset 46630 75aa3e39d02c
parent 42047 72bc0277f889
permissions -rw-r--r--
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8 8182656: Make the required changes in GC code to build on OSX 10 + Xcode 8 8182657: Make the required changes in Runtime code to build on OSX 10 + Xcode 8 8182658: Make the required changes in Compiler code to build on OSX 10 + Xcode 8 Reviewed-by: jwilhelm, ehelin, phh Contributed-by: phh <hohensee@amazon.com>, jwilhelm <jesper.wilhelmsson@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     1
/*
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 42047
diff changeset
     2
 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     4
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     7
 * published by the Free Software Foundation.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     8
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    13
 * accompanied this code).
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    14
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    18
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    21
 * questions.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    22
 *
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    23
 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    24
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    25
#include <objc/objc-runtime.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    26
#import <Foundation/Foundation.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    27
#import <JavaNativeFoundation/JavaNativeFoundation.h>
42047
72bc0277f889 8160376: DebuggerException: Can't attach symbolicator to the process
dsamersoff
parents: 35217
diff changeset
    28
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    29
25065
fbb17c582d13 8043340: [macosx] Fix hard-wired paths to JavaVM.framework
ddehaven
parents: 23480
diff changeset
    30
#include <jni.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    31
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    32
#import <mach/mach.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    33
#import <mach/mach_types.h>
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    34
#import <sys/sysctl.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    35
#import <stdio.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    36
#import <stdarg.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    37
#import <stdlib.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    38
#import <strings.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    39
#import <dlfcn.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    40
#import <limits.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    41
#import <errno.h>
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    42
#import <sys/types.h>
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    43
#import <sys/ptrace.h>
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    44
#include "libproc_impl.h"
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    45
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    46
#define UNSUPPORTED_ARCH "Unsupported architecture!"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    47
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    48
#if defined(x86_64) && !defined(amd64)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    49
#define amd64 1
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    50
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    51
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    52
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    53
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    54
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    55
#error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    56
#endif
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    57
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    58
static jfieldID symbolicatorID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    59
static jfieldID taskID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    60
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    61
static jfieldID p_ps_prochandle_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    62
static jfieldID loadObjectList_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    63
static jmethodID listAdd_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    65
static jmethodID createClosestSymbol_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    66
static jmethodID createLoadObject_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    67
static jmethodID getJavaThreadsInfo_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    68
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    69
// indicator if thread id (lwpid_t) was set
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    70
static bool _threads_filled = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    71
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    72
static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    73
  (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    74
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    75
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    76
static id getSymbolicator(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    77
  jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    78
  return (id)(intptr_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    79
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    80
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    81
static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    82
  (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    83
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    84
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    85
static task_t getTask(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    86
  jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    87
  return (task_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    88
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    89
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    90
#define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    91
#define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    92
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    93
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    94
#define CHECK_EXCEPTION_CLEAR if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); } 
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    95
#define CHECK_EXCEPTION_CLEAR_VOID if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return; } 
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    96
#define CHECK_EXCEPTION_CLEAR_(value) if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return value; } 
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    97
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    98
static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
    99
  jclass exceptionClass = (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException");
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   100
  CHECK_EXCEPTION;
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   101
  (*env)->ThrowNew(env, exceptionClass, errMsg);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   102
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   103
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   104
static struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   105
  jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   106
  return (struct ps_prochandle*)(intptr_t)ptr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   107
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   108
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   109
#if defined(__i386__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   110
    #define hsdb_thread_state_t     x86_thread_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   111
    #define hsdb_float_state_t      x86_float_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   112
    #define HSDB_THREAD_STATE       x86_THREAD_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   113
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   114
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   115
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   116
#elif defined(__x86_64__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   117
    #define hsdb_thread_state_t     x86_thread_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   118
    #define hsdb_float_state_t      x86_float_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   119
    #define HSDB_THREAD_STATE       x86_THREAD_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   120
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   121
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   122
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   123
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   124
    #error UNSUPPORTED_ARCH
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   125
#endif
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   126
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   127
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   128
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   129
 * Method:    init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   130
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   131
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   132
JNIEXPORT void JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   133
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) {
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   134
  symbolicatorID = (*env)->GetFieldID(env, cls, "symbolicator", "J");
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   135
  CHECK_EXCEPTION;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   136
  taskID = (*env)->GetFieldID(env, cls, "task", "J");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   137
  CHECK_EXCEPTION;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   138
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   139
  // for core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   140
  p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   141
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   142
  loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   143
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   144
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   145
  // methods we use
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   146
  createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   147
                    "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   148
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   149
  createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   150
                    "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   151
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   152
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   153
  // java.util.List method we call
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   154
  jclass listClass = (*env)->FindClass(env, "java/util/List");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   155
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   156
  listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   157
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   158
  getJavaThreadsInfo_ID = (*env)->GetMethodID(env, cls, "getJavaThreadsInfo",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   159
                                                     "()[J");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   160
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   161
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   162
  init_libproc(getenv("LIBSAPROC_DEBUG") != NULL);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   163
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   164
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   165
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   166
  (JNIEnv *env, jclass cls)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   167
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   168
#ifdef _LP64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   169
  return 8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   170
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   171
  #error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   172
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   173
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   174
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   175
/** called by Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   176
jlong lookupByNameIncore(
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   177
  JNIEnv *env, struct ps_prochandle *ph, jobject this_obj, jstring objectName, jstring symbolName)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   178
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   179
  const char *objectName_cstr, *symbolName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   180
  jlong addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   181
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   182
  objectName_cstr = NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   183
  if (objectName != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   184
    objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   185
    CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   186
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   187
  symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   188
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   189
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   190
  print_debug("look for %s \n", symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   191
  addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   192
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   193
  if (objectName_cstr != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   194
    (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   195
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   196
  (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   197
  return addr;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   198
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   199
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   200
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   201
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   202
 * Method:    lookupByName0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   203
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   204
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   205
JNIEXPORT jlong JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   206
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   207
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   208
  jstring objectName, jstring symbolName) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   209
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   210
  struct ps_prochandle* ph = get_proc_handle(env, this_obj);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   211
  if (ph != NULL && ph->core != NULL) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   212
    return lookupByNameIncore(env, ph, this_obj, objectName, symbolName);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   213
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   214
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   215
  jlong address = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   216
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   217
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   218
  NSString *symbolNameString = JNFJavaToNSString(env, symbolName);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   219
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   220
  print_debug("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   221
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   222
  id symbolicator = getSymbolicator(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   223
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   224
    uint64_t (*dynamicCall)(id, SEL, NSString *) = (uint64_t (*)(id, SEL, NSString *))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   225
    address = (jlong) dynamicCall(symbolicator, @selector(addressForSymbol:), symbolNameString);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   226
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   227
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   228
  print_debug("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   229
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   230
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   231
  return address;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   232
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   233
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   234
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   235
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   236
 * Method:    lookupByAddress0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   237
 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   238
 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   239
JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   240
  (JNIEnv *env, jobject this_obj, jlong addr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   241
  uintptr_t offset;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   242
  const char* sym = NULL;
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   243
  jstring sym_string;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   244
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   245
  struct ps_prochandle* ph = get_proc_handle(env, this_obj);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   246
  if (ph != NULL && ph->core != NULL) {
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   247
    sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   248
    if (sym == NULL) return 0;
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   249
    sym_string = (*env)->NewStringUTF(env, sym);
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   250
    CHECK_EXCEPTION_(0);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   251
    return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   252
                                                sym_string, (jlong)offset);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   253
  }
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   254
  return 0;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   255
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   256
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   257
/** called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   258
jbyteArray readBytesFromCore(
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   259
  JNIEnv *env, struct ps_prochandle *ph, jobject this_obj, jlong addr, jlong numBytes)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   260
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   261
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   262
  jbyteArray array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   263
  jbyte *bufPtr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   264
  ps_err_e err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   265
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   266
  array = (*env)->NewByteArray(env, numBytes);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   267
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   268
  bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   269
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   270
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   271
  err = ps_pread(ph, (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   272
  (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   273
  return (err == PS_OK)? array : 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   274
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   275
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   276
/*
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   277
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   278
 * Method:    readBytesFromProcess0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   279
 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   280
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   281
JNIEXPORT jbyteArray JNICALL
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   282
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   283
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   284
  jlong addr, jlong numBytes) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   285
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   286
  print_debug("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   287
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   288
  // must allocate storage instead of using former parameter buf
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   289
  jbyteArray array;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   290
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   291
  struct ps_prochandle* ph = get_proc_handle(env, this_obj);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   292
  if (ph != NULL && ph->core != NULL) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   293
    return readBytesFromCore(env, ph, this_obj, addr, numBytes);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   294
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   295
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   296
  array = (*env)->NewByteArray(env, numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   297
  CHECK_EXCEPTION_(0);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   298
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   299
  unsigned long alignedAddress;
15868
861a1da183b8 8008327: [parfait] Unitialized variable in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
morris
parents: 15738
diff changeset
   300
  unsigned long alignedLength = 0;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   301
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   302
  vm_offset_t *pages;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   303
  int *mapped;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   304
  long pageCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   305
  uint byteCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   306
  int i;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   307
  unsigned long remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   308
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   309
  alignedAddress = trunc_page(addr);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   310
  if (addr != alignedAddress) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   311
    alignedLength += addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   312
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   313
  alignedLength = round_page(numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   314
  pageCount = alignedLength/vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   315
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   316
  // Allocate storage for pages and flags.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   317
  pages = malloc(pageCount * sizeof(vm_offset_t));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   318
  mapped = calloc(pageCount, sizeof(int));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   319
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   320
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   321
  // Try to read each of the pages.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   322
  for (i = 0; i < pageCount; i++) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   323
    result = vm_read(gTask, alignedAddress + i*vm_page_size, vm_page_size, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   324
		     &pages[i], &byteCount);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   325
    mapped[i] = (result == KERN_SUCCESS); 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   326
    // assume all failures are unmapped pages
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   327
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   328
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   329
  print_debug("%ld pages\n", pageCount);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   330
	
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   331
  remaining = numBytes;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   332
	
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   333
  for (i = 0; i < pageCount; i++) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   334
    unsigned long len = vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   335
    unsigned long start = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   336
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   337
    if (i == 0) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   338
      start = addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   339
      len = vm_page_size - start;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   340
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   341
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   342
    if (i == (pageCount - 1)) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   343
      len = remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   344
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   345
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   346
    if (mapped[i]) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   347
      print_debug("page %d mapped (len %ld start %ld)\n", i, len, start);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   348
      (*env)->SetByteArrayRegion(env, array, 0, len, ((jbyte *) pages[i] + start));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   349
      vm_deallocate(mach_task_self(), pages[i], vm_page_size);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   350
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   351
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   352
    remaining -= len;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   353
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   354
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   355
  free (pages);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   356
  free (mapped);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   357
  return array;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   358
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   359
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   360
/** Only used for core file reading, set thread_id for threads which is got after core file parsed.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   361
  * Thread context is available in Mach-O core file but thread id is not. We can get thread id
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   362
  * from Threads which store all java threads information when they are created. Here we can identify
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   363
  * them as java threads by checking if a thread's rsp or rbp within a java thread's stack.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   364
  * Note Macosx uses unique_thread_id which is different from other platforms though printed ids
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   365
  * are still pthread id. Function BsdDebuggerLocal.getJavaThreadsInfo returns an array of long
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   366
  * integers to host all java threads' id, stack_start, stack_end as:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   367
  * [uid0, stack_start0, stack_end0, uid1, stack_start1, stack_end1, ...]
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   368
  *
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   369
  * The work cannot be done at init0 since Threads is not available yet(VM not initialized yet). 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   370
  * This function should be called only once if succeeded
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   371
  */ 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   372
bool fill_java_threads(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   373
  int n = 0, i = 0, j;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   374
  struct reg regs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   375
  
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   376
  jlongArray thrinfos = (*env)->CallObjectMethod(env, this_obj, getJavaThreadsInfo_ID);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   377
  CHECK_EXCEPTION_(false);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   378
  int len = (int)(*env)->GetArrayLength(env, thrinfos);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   379
  uint64_t* cinfos = (uint64_t *)(*env)->GetLongArrayElements(env, thrinfos, NULL);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   380
  CHECK_EXCEPTION_(false); 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   381
  n = get_num_threads(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   382
  print_debug("fill_java_threads called, num_of_thread = %d\n", n);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   383
  for (i = 0; i < n; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   384
    if (!get_nth_lwp_regs(ph, i, &regs)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   385
      print_debug("Could not get regs of thread %d, already set!\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   386
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   387
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   388
    for (j = 0; j < len; j += 3) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   389
      lwpid_t  uid = cinfos[j];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   390
      uint64_t beg = cinfos[j + 1];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   391
      uint64_t end = cinfos[j + 2]; 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   392
      if ((regs.r_rsp < end && regs.r_rsp >= beg) ||
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   393
          (regs.r_rbp < end && regs.r_rbp >= beg)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   394
        set_lwp_id(ph, i, uid);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   395
        break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   396
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   397
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   398
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   399
  (*env)->ReleaseLongArrayElements(env, thrinfos, (jlong*)cinfos, 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   400
  CHECK_EXCEPTION_(false);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   401
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   402
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   403
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   404
/* For core file only, called from
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   405
 * Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   406
 */
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   407
jlongArray getThreadIntegerRegisterSetFromCore(JNIEnv *env, jobject this_obj, long lwp_id, struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   408
  if (!_threads_filled)  {
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   409
    if (!fill_java_threads(env, this_obj, ph)) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   410
      throw_new_debugger_exception(env, "Failed to fill in threads");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   411
      return 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   412
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   413
      _threads_filled = true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   414
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   415
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   416
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   417
  struct reg gregs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   418
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   419
  jlongArray array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   420
  jlong *regs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   421
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   422
  if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   423
    THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   424
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   425
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   426
#undef NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   427
#undef REG_INDEX
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   428
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   429
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   430
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   431
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   432
  array = (*env)->NewLongArray(env, NPRGREG);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   433
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   434
  regs = (*env)->GetLongArrayElements(env, array, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   435
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   436
  regs[REG_INDEX(R15)] = gregs.r_r15;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   437
  regs[REG_INDEX(R14)] = gregs.r_r14;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   438
  regs[REG_INDEX(R13)] = gregs.r_r13;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   439
  regs[REG_INDEX(R12)] = gregs.r_r12;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   440
  regs[REG_INDEX(RBP)] = gregs.r_rbp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   441
  regs[REG_INDEX(RBX)] = gregs.r_rbx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   442
  regs[REG_INDEX(R11)] = gregs.r_r11;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   443
  regs[REG_INDEX(R10)] = gregs.r_r10;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   444
  regs[REG_INDEX(R9)]  = gregs.r_r9;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   445
  regs[REG_INDEX(R8)]  = gregs.r_r8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   446
  regs[REG_INDEX(RAX)] = gregs.r_rax;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   447
  regs[REG_INDEX(RCX)] = gregs.r_rcx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   448
  regs[REG_INDEX(RDX)] = gregs.r_rdx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   449
  regs[REG_INDEX(RSI)] = gregs.r_rsi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   450
  regs[REG_INDEX(RDI)] = gregs.r_rdi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   451
  regs[REG_INDEX(RIP)] = gregs.r_rip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   452
  regs[REG_INDEX(CS)]  = gregs.r_cs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   453
  regs[REG_INDEX(RSP)] = gregs.r_rsp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   454
  regs[REG_INDEX(SS)]  = gregs.r_ss;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   455
  regs[REG_INDEX(FSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   456
  regs[REG_INDEX(GSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   457
  regs[REG_INDEX(DS)] = gregs.r_ds;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   458
  regs[REG_INDEX(ES)] = gregs.r_es;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   459
  regs[REG_INDEX(FS)] = gregs.r_fs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   460
  regs[REG_INDEX(GS)] = gregs.r_gs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   461
  regs[REG_INDEX(TRAPNO)] = gregs.r_trapno;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   462
  regs[REG_INDEX(RFL)]    = gregs.r_rflags;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   463
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   464
#endif /* amd64 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   465
  (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   466
  return array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   467
}
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   468
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   469
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   470
 * Lookup the thread_t that corresponds to the given thread_id.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   471
 * The thread_id should be the result from calling thread_info() with THREAD_IDENTIFIER_INFO
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   472
 * and reading the m_ident_info.thread_id returned.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   473
 * The returned thread_t is the mach send right to the kernel port for the corresponding thread.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   474
 *
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   475
 * We cannot simply use the OSThread._thread_id field in the JVM. This is set to ::mach_thread_self()
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   476
 * in the VM, but that thread port is not valid for a remote debugger to access the thread.
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   477
 */
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   478
thread_t
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   479
lookupThreadFromThreadId(task_t task, jlong thread_id) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   480
  print_debug("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id);
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   481
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   482
  thread_array_t thread_list = NULL;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   483
  mach_msg_type_number_t thread_list_count = 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   484
  thread_t result_thread = 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   485
  int i;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   486
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   487
  // get the list of all the send rights
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   488
  kern_return_t result = task_threads(task, &thread_list, &thread_list_count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   489
  if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   490
    print_debug("task_threads returned 0x%x\n", result);
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   491
    return 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   492
  }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   493
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   494
  for(i = 0 ; i < thread_list_count; i++) {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   495
    thread_identifier_info_data_t m_ident_info;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   496
    mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   497
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   498
    // get the THREAD_IDENTIFIER_INFO for the send right
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   499
    result = thread_info(thread_list[i], THREAD_IDENTIFIER_INFO, (thread_info_t) &m_ident_info, &count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   500
    if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   501
      print_debug("thread_info returned 0x%x\n", result);
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   502
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   503
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   504
    
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   505
    // if this is the one we're looking for, return the send right
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   506
    if (thread_id == m_ident_info.thread_id)
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   507
    {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   508
      result_thread = thread_list[i];
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   509
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   510
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   511
  }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   512
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   513
  vm_size_t thread_list_size = (vm_size_t) (thread_list_count * sizeof (thread_t));
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   514
  vm_deallocate(mach_task_self(), (vm_address_t) thread_list, thread_list_count);
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   515
  
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   516
  return result_thread;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   517
}
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   518
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   519
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   520
/*
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   521
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   522
 * Method:    getThreadIntegerRegisterSet0
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   523
 * Signature: (J)[J
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   524
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   525
JNIEXPORT jlongArray JNICALL 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   526
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   527
  JNIEnv *env, jobject this_obj, 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   528
  jlong thread_id) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   529
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   530
  print_debug("getThreadRegisterSet0 called\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   531
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   532
  struct ps_prochandle* ph = get_proc_handle(env, this_obj);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   533
  if (ph != NULL && ph->core != NULL) {
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   534
    return getThreadIntegerRegisterSetFromCore(env, this_obj, thread_id, ph);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   535
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   536
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   537
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   538
  thread_t tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   539
  mach_msg_type_number_t count = HSDB_THREAD_STATE_COUNT;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   540
  hsdb_thread_state_t state;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   541
  jlongArray registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   542
  jlong *primitiveArray;
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   543
  task_t gTask = getTask(env, this_obj);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   544
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   545
  tid = lookupThreadFromThreadId(gTask, thread_id);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   546
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   547
  result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   548
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   549
  if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   550
    print_error("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   551
    return NULL;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   552
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   553
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   554
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   555
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   556
#undef REG_INDEX
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   557
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   558
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   559
  // 64 bit
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   560
  print_debug("Getting threads for a 64-bit process\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   561
  registerArray = (*env)->NewLongArray(env, NPRGREG);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   562
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   563
  primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   564
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   565
  primitiveArray[REG_INDEX(R15)] = state.__r15;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   566
  primitiveArray[REG_INDEX(R14)] = state.__r14;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   567
  primitiveArray[REG_INDEX(R13)] = state.__r13;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   568
  primitiveArray[REG_INDEX(R12)] = state.__r12;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   569
  primitiveArray[REG_INDEX(R11)] = state.__r11;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   570
  primitiveArray[REG_INDEX(R10)] = state.__r10;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   571
  primitiveArray[REG_INDEX(R9)]  = state.__r9;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   572
  primitiveArray[REG_INDEX(R8)]  = state.__r8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   573
  primitiveArray[REG_INDEX(RDI)] = state.__rdi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   574
  primitiveArray[REG_INDEX(RSI)] = state.__rsi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   575
  primitiveArray[REG_INDEX(RBP)] = state.__rbp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   576
  primitiveArray[REG_INDEX(RBX)] = state.__rbx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   577
  primitiveArray[REG_INDEX(RDX)] = state.__rdx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   578
  primitiveArray[REG_INDEX(RCX)] = state.__rcx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   579
  primitiveArray[REG_INDEX(RAX)] = state.__rax;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   580
  primitiveArray[REG_INDEX(TRAPNO)] = 0;            // trapno, not used
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   581
  primitiveArray[REG_INDEX(ERR)]    = 0;            // err, not used 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   582
  primitiveArray[REG_INDEX(RIP)] = state.__rip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   583
  primitiveArray[REG_INDEX(CS)]  = state.__cs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   584
  primitiveArray[REG_INDEX(RFL)] = state.__rflags;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   585
  primitiveArray[REG_INDEX(RSP)] = state.__rsp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   586
  primitiveArray[REG_INDEX(SS)] = 0;                // We don't have SS
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   587
  primitiveArray[REG_INDEX(FS)] = state.__fs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   588
  primitiveArray[REG_INDEX(GS)] = state.__gs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   589
  primitiveArray[REG_INDEX(ES)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   590
  primitiveArray[REG_INDEX(DS)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   591
  primitiveArray[REG_INDEX(FSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   592
  primitiveArray[REG_INDEX(GSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   593
  print_debug("set registers\n");
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   594
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   595
  (*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   596
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   597
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   598
#error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   599
#endif /* amd64 */
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   600
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   601
  return registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   602
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   603
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   604
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   605
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   606
 * Method:    translateTID0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   607
 * Signature: (I)I
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   608
 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   609
JNIEXPORT jint JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   610
Java_sun_jvm_hotspot_debugger_macosx_MacOSXDebuggerLocal_translateTID0(
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   611
  JNIEnv *env, jobject this_obj, jint tid) 
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   612
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   613
  print_debug("translateTID0 called on tid = 0x%x\n", (int)tid);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   614
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   615
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   616
  thread_t foreign_tid, usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   617
  mach_msg_type_name_t type;
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   618
  
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   619
  foreign_tid = tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   620
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   621
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   622
  result = mach_port_extract_right(gTask, foreign_tid, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   623
				   MACH_MSG_TYPE_COPY_SEND, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   624
				   &usable_tid, &type);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   625
  if (result != KERN_SUCCESS)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   626
    return -1;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   627
    
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   628
  print_debug("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   629
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   630
  return (jint) usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   631
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   632
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   633
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   634
static bool ptrace_continue(pid_t pid, int signal) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   635
  // pass the signal to the process so we don't swallow it
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   636
  int res;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   637
  if ((res = ptrace(PT_CONTINUE, pid, (caddr_t)1, signal)) < 0) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   638
    print_error("attach: ptrace(PT_CONTINUE, %d) failed with %d\n", pid, res);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   639
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   640
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   641
  return true;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   642
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   643
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   644
// waits until the ATTACH has stopped the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   645
// by signal SIGSTOP
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   646
static bool ptrace_waitpid(pid_t pid) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   647
  int ret;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   648
  int status;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   649
  while (true) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   650
    // Wait for debuggee to stop.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   651
    ret = waitpid(pid, &status, 0);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   652
    if (ret >= 0) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   653
      if (WIFSTOPPED(status)) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   654
        // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   655
        // will still be pending and delivered when the process is DETACHED and the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   656
        // will go to sleep.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   657
        if (WSTOPSIG(status) == SIGSTOP) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   658
          // Debuggee stopped by SIGSTOP.
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   659
          return true;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   660
        }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   661
        if (!ptrace_continue(pid, WSTOPSIG(status))) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   662
          print_error("attach: Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   663
          return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   664
        }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   665
      } else {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   666
        print_error("attach: waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   667
        return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   668
      }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   669
    } else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   670
      switch (errno) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   671
        case EINTR:
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   672
          continue;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   673
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   674
        case ECHILD:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   675
          print_error("attach: waitpid() failed. Child process pid (%d) does not exist \n", pid);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   676
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   677
        case EINVAL:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   678
          print_error("attach: waitpid() failed. Invalid options argument.\n");
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   679
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   680
        default:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   681
          print_error("attach: waitpid() failed. Unexpected error %d\n",errno);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   682
          break;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   683
      }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   684
      return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   685
    }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   686
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   687
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   688
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   689
// attach to a process/thread specified by "pid"
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   690
static bool ptrace_attach(pid_t pid) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   691
  int res;
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 42047
diff changeset
   692
  if ((res = ptrace(PT_ATTACHEXC, pid, 0, 0)) < 0) {
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 42047
diff changeset
   693
    print_error("ptrace(PT_ATTACHEXC, %d) failed with %d\n", pid, res);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   694
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   695
  } else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   696
    return ptrace_waitpid(pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   697
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   698
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   699
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   700
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   701
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   702
 * Method:    attach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   703
 * Signature: (I)V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   704
 */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   705
JNIEXPORT void JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   706
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   707
  JNIEnv *env, jobject this_obj, jint jpid)
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   708
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   709
  print_debug("attach0 called for jpid=%d\n", (int)jpid);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   710
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   711
JNF_COCOA_ENTER(env);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   712
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   713
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   714
  task_t gTask = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   715
  result = task_for_pid(mach_task_self(), jpid, &gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   716
  if (result != KERN_SUCCESS) {
17088
75082f0d93f2 8013364: SA-JDI exceptions caused by lack of permissions on OSX should be more verbose about issue cause
sla
parents: 17084
diff changeset
   717
    print_error("attach: task_for_pid(%d) failed: '%s' (%d)\n", (int)jpid, mach_error_string(result), result);
75082f0d93f2 8013364: SA-JDI exceptions caused by lack of permissions on OSX should be more verbose about issue cause
sla
parents: 17084
diff changeset
   718
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.");
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   719
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   720
  putTask(env, this_obj, gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   721
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   722
  // use ptrace to stop the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   723
  // on os x, ptrace only needs to be called on the process, not the individual threads
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   724
  if (ptrace_attach(jpid) != true) {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   725
    mach_port_deallocate(mach_task_self(), gTask);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   726
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   727
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   728
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   729
  id symbolicator = nil;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   730
  id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   731
  if (jrsSymbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   732
    id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   733
    symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   734
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   735
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   736
    CFRetain(symbolicator); // pin symbolicator while in java heap
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   737
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   738
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   739
  putSymbolicator(env, this_obj, symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   740
  if (symbolicator == nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   741
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   742
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   743
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   744
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   745
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   746
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   747
/** For core file, 
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   748
    called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   749
static void fillLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   750
  int n = 0, i = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   751
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   752
  // add load objects
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   753
  n = get_num_libs(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   754
  for (i = 0; i < n; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   755
     uintptr_t base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   756
     const char* name;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   757
     jobject loadObject;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   758
     jobject loadObjectList;
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   759
     jstring nameString;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   760
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   761
     base = get_lib_base(ph, i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   762
     name = get_lib_name(ph, i);
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   763
     nameString = (*env)->NewStringUTF(env, name);
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   764
     CHECK_EXCEPTION;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   765
     loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   766
                                            nameString, (jlong)0, (jlong)base);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   767
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   768
     loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   769
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   770
     (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   771
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   772
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   773
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   774
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   775
/*
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   776
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   777
 * Method:    attach0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   778
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   779
 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   780
JNIEXPORT void JNICALL
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   781
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2(
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   782
  JNIEnv *env, jobject this_obj, jstring execName, jstring coreName)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   783
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   784
  const char *execName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   785
  const char *coreName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   786
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   787
  struct ps_prochandle* ph;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   788
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   789
  execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   790
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   791
  coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   792
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   793
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   794
  print_debug("attach: %s %s\n", execName_cstr, coreName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   795
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   796
  if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   797
    (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   798
    (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   799
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   800
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   801
  (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   802
  (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   803
  (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   804
  fillLoadObjects(env, this_obj, ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   805
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   806
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   807
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   808
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   809
 * Method:    detach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   810
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   811
 */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   812
JNIEXPORT void JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   813
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   814
  JNIEnv *env, jobject this_obj)
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   815
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   816
  print_debug("detach0 called\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   817
  struct ps_prochandle* ph = get_proc_handle(env, this_obj);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   818
  if (ph != NULL && ph->core != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   819
     Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   820
     return;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   821
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   822
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   823
  task_t gTask = getTask(env, this_obj);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   824
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   825
  // detach from the ptraced process causing it to resume execution
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   826
  int pid;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   827
  kern_return_t k_res;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   828
  k_res = pid_for_task(gTask, &pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   829
  if (k_res != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   830
    print_error("detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   831
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   832
  else {
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   833
    int res = ptrace(PT_DETACH, pid, 0, 0);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   834
    if (res < 0) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   835
      print_error("detach: ptrace(PT_DETACH, %d) failed (%d)\n", pid, res);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   836
    }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   837
  }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   838
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   839
  mach_port_deallocate(mach_task_self(), gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   840
  id symbolicator = getSymbolicator(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   841
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   842
    CFRelease(symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   843
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   844
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   845
}