src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m
author redestad
Fri, 18 Jan 2019 08:00:05 +0100
changeset 53380 c8208421a860
parent 52713 8586613959f0
child 58017 9726449d2644
permissions -rw-r--r--
8217325: Enable inlining of java_lang_Class::oop_size_raw Reviewed-by: shade, coleenp, rehn, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
     1
/*
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
     2
 * Copyright (c) 2002, 2018, 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>
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    36
#import <string.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    37
#import <stdarg.h>
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    38
#import <stdlib.h>
13873
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    39
#import <strings.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    40
#import <dlfcn.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    41
#import <limits.h>
7b72e3873785 6879063: SA should use hsdis for disassembly
minqi
parents: 10739
diff changeset
    42
#import <errno.h>
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    43
#import <sys/types.h>
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
    44
#import <sys/ptrace.h>
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    45
#include "libproc_impl.h"
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    46
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    47
#define UNSUPPORTED_ARCH "Unsupported architecture!"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    48
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    49
#if defined(x86_64) && !defined(amd64)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    50
#define amd64 1
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    51
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    52
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    53
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    54
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    55
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    56
#error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    57
#endif
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    58
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    59
static jfieldID symbolicatorID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    60
static jfieldID taskID = 0; // set in _init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
    61
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    62
static jfieldID p_ps_prochandle_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    63
static jfieldID loadObjectList_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    64
static jmethodID listAdd_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    65
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    66
static jmethodID createClosestSymbol_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    67
static jmethodID createLoadObject_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    68
static jmethodID getJavaThreadsInfo_ID = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    69
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    70
// indicator if thread id (lwpid_t) was set
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    71
static bool _threads_filled = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
    72
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    73
// mach_exc_server defined in the generated mach_excServer.c
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    74
extern boolean_t mach_exc_server(mach_msg_header_t *input_msg_hdr,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    75
                                 mach_msg_header_t *output_msg_hdr);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    76
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    77
kern_return_t catch_mach_exception_raise(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    78
  mach_port_t exception_port, mach_port_t thread,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    79
  mach_port_t task, exception_type_t exception,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    80
  mach_exception_data_t code,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    81
  mach_msg_type_number_t code_cnt);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    82
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    83
kern_return_t catch_mach_exception_raise_state(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    84
  mach_port_t exception_port, exception_type_t exception,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    85
  const mach_exception_data_t code, mach_msg_type_number_t code_cnt,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    86
  int *flavor, const thread_state_t old_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    87
  mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    88
  mach_msg_type_number_t *new_state_cnt);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    89
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    90
kern_return_t catch_mach_exception_raise_state_identity(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    91
  mach_port_t exception_port, mach_port_t thread, mach_port_t task,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    92
  exception_type_t exception, mach_exception_data_t code,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    93
  mach_msg_type_number_t code_cnt, int *flavor, thread_state_t old_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    94
  mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    95
  mach_msg_type_number_t *new_state_cnt);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    96
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    97
static struct exception_saved_state {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    98
  exception_mask_t       saved_masks[EXC_TYPES_COUNT];
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
    99
  mach_port_t            saved_ports[EXC_TYPES_COUNT];
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   100
  exception_behavior_t   saved_behaviors[EXC_TYPES_COUNT];
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   101
  thread_state_flavor_t  saved_flavors[EXC_TYPES_COUNT];
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   102
  mach_msg_type_number_t saved_exception_types_count;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   103
} exception_saved_state;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   104
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   105
static mach_port_t tgt_exception_port;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   106
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   107
// Mirrors __Reply__mach_exception_raise_t generated in mach_excServer.c
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   108
static struct rep_msg {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   109
  mach_msg_header_t header;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   110
  NDR_record_t ndr;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   111
  kern_return_t ret_code;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   112
} rep_msg;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   113
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   114
// Mirrors __Request__mach_exception_raise_t generated in mach_excServer.c
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   115
// with a large trailing pad to avoid MACH_MSG_RCV_TOO_LARGE
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   116
static struct exc_msg {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   117
  mach_msg_header_t header;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   118
  // start of the kernel processed data
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   119
  mach_msg_body_t msgh_body;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   120
  mach_msg_port_descriptor_t thread;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   121
  mach_msg_port_descriptor_t task;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   122
  // end of the kernel processed data
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   123
  NDR_record_t ndr;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   124
  exception_type_t exception;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   125
  mach_msg_type_number_t code_cnt;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   126
  mach_exception_data_t code; // an array of int64_t
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   127
  char pad[512];
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   128
} exc_msg;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   129
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   130
static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   131
  (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   132
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   133
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   134
static id getSymbolicator(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   135
  jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   136
  return (id)(intptr_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   137
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   138
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   139
static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   140
  (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   141
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   142
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   143
static task_t getTask(JNIEnv *env, jobject this_obj) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   144
  jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   145
  return (task_t)ptr;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   146
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   147
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   148
#define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   149
#define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   150
#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
   151
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   152
#define CHECK_EXCEPTION_CLEAR if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   153
#define CHECK_EXCEPTION_CLEAR_VOID if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionClear(env); return; }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   154
#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
   155
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   156
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
   157
  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
   158
  CHECK_EXCEPTION;
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   159
  (*env)->ThrowNew(env, exceptionClass, errMsg);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   160
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   161
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   162
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
   163
  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
   164
  return (struct ps_prochandle*)(intptr_t)ptr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   165
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   166
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   167
#if defined(__i386__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   168
    #define hsdb_thread_state_t     x86_thread_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   169
    #define hsdb_float_state_t      x86_float_state32_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   170
    #define HSDB_THREAD_STATE       x86_THREAD_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   171
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE32
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   172
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   173
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE32_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   174
#elif defined(__x86_64__)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   175
    #define hsdb_thread_state_t     x86_thread_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   176
    #define hsdb_float_state_t      x86_float_state64_t
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   177
    #define HSDB_THREAD_STATE       x86_THREAD_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   178
    #define HSDB_FLOAT_STATE        x86_FLOAT_STATE64
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   179
    #define HSDB_THREAD_STATE_COUNT x86_THREAD_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   180
    #define HSDB_FLOAT_STATE_COUNT  x86_FLOAT_STATE64_COUNT
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   181
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   182
    #error UNSUPPORTED_ARCH
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   183
#endif
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   184
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   185
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   186
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   187
 * Method:    init0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   188
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   189
 */
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   190
JNIEXPORT void JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   191
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv *env, jclass cls) {
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   192
  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
   193
  CHECK_EXCEPTION;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   194
  taskID = (*env)->GetFieldID(env, cls, "task", "J");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   195
  CHECK_EXCEPTION;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   196
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   197
  // for core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   198
  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
   199
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   200
  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
   201
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   202
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   203
  // methods we use
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   204
  createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   205
                    "(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
   206
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   207
  createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   208
                    "(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
   209
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   210
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   211
  // java.util.List method we call
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   212
  jclass listClass = (*env)->FindClass(env, "java/util/List");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   213
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   214
  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
   215
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   216
  getJavaThreadsInfo_ID = (*env)->GetMethodID(env, cls, "getJavaThreadsInfo",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   217
                                                     "()[J");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   218
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   219
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   220
  init_libproc(getenv("LIBSAPROC_DEBUG") != NULL);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   221
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   222
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   223
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
   224
  (JNIEnv *env, jclass cls)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   225
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   226
#ifdef _LP64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   227
  return 8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   228
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   229
  #error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   230
#endif
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   231
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   232
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   233
/** 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
   234
jlong lookupByNameIncore(
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   235
  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
   236
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   237
  const char *objectName_cstr, *symbolName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   238
  jlong addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   239
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   240
  objectName_cstr = NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   241
  if (objectName != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   242
    objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   243
    CHECK_EXCEPTION_(0);
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
  symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   246
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   247
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   248
  print_debug("look for %s \n", symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   249
  addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   250
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   251
  if (objectName_cstr != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   252
    (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   253
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   254
  (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   255
  return addr;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   256
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   257
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   258
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   259
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   260
 * Method:    lookupByName0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   261
 * Signature: (Ljava/lang/String;Ljava/lang/String;)J
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   262
 */
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   263
JNIEXPORT jlong JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   264
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   265
  JNIEnv *env, jobject this_obj,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   266
  jstring objectName, jstring symbolName)
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   267
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   268
  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
   269
  if (ph != NULL && ph->core != NULL) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   270
    return lookupByNameIncore(env, ph, this_obj, objectName, symbolName);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   271
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   272
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   273
  jlong address = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   274
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   275
JNF_COCOA_ENTER(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   276
  NSString *symbolNameString = JNFJavaToNSString(env, symbolName);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   277
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   278
  print_debug("lookupInProcess called for %s\n", [symbolNameString UTF8String]);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   279
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   280
  id symbolicator = getSymbolicator(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   281
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   282
    uint64_t (*dynamicCall)(id, SEL, NSString *) = (uint64_t (*)(id, SEL, NSString *))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   283
    address = (jlong) dynamicCall(symbolicator, @selector(addressForSymbol:), symbolNameString);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   284
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   285
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   286
  print_debug("address of symbol %s = %llx\n", [symbolNameString UTF8String], address);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   287
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   288
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   289
  return address;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   290
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   291
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   292
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   293
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   294
 * Method:    lookupByAddress0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   295
 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   296
 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   297
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
   298
  (JNIEnv *env, jobject this_obj, jlong addr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   299
  uintptr_t offset;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   300
  const char* sym = NULL;
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   301
  jstring sym_string;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   302
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   303
  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
   304
  if (ph != NULL && ph->core != NULL) {
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   305
    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
   306
    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
   307
    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
   308
    CHECK_EXCEPTION_(0);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   309
    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
   310
                                                sym_string, (jlong)offset);
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   311
  }
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   312
  return 0;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   313
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   314
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   315
/** 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
   316
jbyteArray readBytesFromCore(
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   317
  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
   318
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   319
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   320
  jbyteArray array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   321
  jbyte *bufPtr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   322
  ps_err_e err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   323
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   324
  array = (*env)->NewByteArray(env, numBytes);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   325
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   326
  bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   327
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   328
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   329
  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
   330
  (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   331
  return (err == PS_OK)? array : 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   332
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   333
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   334
/*
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   335
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   336
 * Method:    readBytesFromProcess0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   337
 * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   338
 */
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   339
JNIEXPORT jbyteArray JNICALL
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   340
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   341
  JNIEnv *env, jobject this_obj,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   342
  jlong addr, jlong numBytes)
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   343
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   344
  print_debug("readBytesFromProcess called. addr = %llx numBytes = %lld\n", addr, numBytes);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   345
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   346
  // must allocate storage instead of using former parameter buf
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   347
  jbyteArray array;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   348
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   349
  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
   350
  if (ph != NULL && ph->core != NULL) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   351
    return readBytesFromCore(env, ph, this_obj, addr, numBytes);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   352
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   353
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   354
  array = (*env)->NewByteArray(env, numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   355
  CHECK_EXCEPTION_(0);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   356
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   357
  unsigned long alignedAddress;
15868
861a1da183b8 8008327: [parfait] Unitialized variable in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
morris
parents: 15738
diff changeset
   358
  unsigned long alignedLength = 0;
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   359
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   360
  vm_offset_t *pages;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   361
  int *mapped;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   362
  long pageCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   363
  uint byteCount;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   364
  int i;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   365
  unsigned long remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   366
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   367
  alignedAddress = trunc_page(addr);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   368
  if (addr != alignedAddress) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   369
    alignedLength += addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   370
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   371
  alignedLength = round_page(numBytes);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   372
  pageCount = alignedLength/vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   373
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   374
  // Allocate storage for pages and flags.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   375
  pages = malloc(pageCount * sizeof(vm_offset_t));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   376
  mapped = calloc(pageCount, sizeof(int));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   377
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   378
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   379
  // Try to read each of the pages.
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   380
  for (i = 0; i < pageCount; i++) {
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   381
    result = vm_read(gTask, alignedAddress + i*vm_page_size, vm_page_size,
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   382
		     &pages[i], &byteCount);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   383
    mapped[i] = (result == KERN_SUCCESS);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   384
    // assume all failures are unmapped pages
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   385
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   386
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   387
  print_debug("%ld pages\n", pageCount);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   388
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   389
  remaining = numBytes;
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   390
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   391
  for (i = 0; i < pageCount; i++) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   392
    unsigned long len = vm_page_size;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   393
    unsigned long start = 0;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   394
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   395
    if (i == 0) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   396
      start = addr - alignedAddress;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   397
      len = vm_page_size - start;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   398
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   399
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   400
    if (i == (pageCount - 1)) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   401
      len = remaining;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   402
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   403
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   404
    if (mapped[i]) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   405
      print_debug("page %d mapped (len %ld start %ld)\n", i, len, start);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   406
      (*env)->SetByteArrayRegion(env, array, 0, len, ((jbyte *) pages[i] + start));
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   407
      vm_deallocate(mach_task_self(), pages[i], vm_page_size);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   408
    }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   409
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   410
    remaining -= len;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   411
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   412
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   413
  free (pages);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   414
  free (mapped);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   415
  return array;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   416
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   417
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   418
/** 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
   419
  * 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
   420
  * 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
   421
  * 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
   422
  * 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
   423
  * 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
   424
  * 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
   425
  * [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
   426
  *
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   427
  * The work cannot be done at init0 since Threads is not available yet(VM not initialized yet).
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   428
  * This function should be called only once if succeeded
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   429
  */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   430
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
   431
  int n = 0, i = 0, j;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   432
  struct reg regs;
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   433
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   434
  jlongArray thrinfos = (*env)->CallObjectMethod(env, this_obj, getJavaThreadsInfo_ID);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   435
  CHECK_EXCEPTION_(false);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   436
  int len = (int)(*env)->GetArrayLength(env, thrinfos);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   437
  uint64_t* cinfos = (uint64_t *)(*env)->GetLongArrayElements(env, thrinfos, NULL);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   438
  CHECK_EXCEPTION_(false);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   439
  n = get_num_threads(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   440
  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
   441
  for (i = 0; i < n; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   442
    if (!get_nth_lwp_regs(ph, i, &regs)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   443
      print_debug("Could not get regs of thread %d, already set!\n", i);
52713
8586613959f0 8214373: adjust usage of ReleaseLongArrayElements in MacosxDebuggerLocal
mbaesken
parents: 50818
diff changeset
   444
      (*env)->ReleaseLongArrayElements(env, thrinfos, (jlong*)cinfos, 0);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   445
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   446
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   447
    for (j = 0; j < len; j += 3) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   448
      lwpid_t  uid = cinfos[j];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   449
      uint64_t beg = cinfos[j + 1];
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   450
      uint64_t end = cinfos[j + 2];
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   451
      if ((regs.r_rsp < end && regs.r_rsp >= beg) ||
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   452
          (regs.r_rbp < end && regs.r_rbp >= beg)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   453
        set_lwp_id(ph, i, uid);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   454
        break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   455
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   456
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   457
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   458
  (*env)->ReleaseLongArrayElements(env, thrinfos, (jlong*)cinfos, 0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   459
  CHECK_EXCEPTION_(false);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   460
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   461
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   462
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   463
/* For core file only, called from
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   464
 * Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   465
 */
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   466
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
   467
  if (!_threads_filled)  {
17084
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   468
    if (!fill_java_threads(env, this_obj, ph)) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   469
      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
   470
      return 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   471
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   472
      _threads_filled = true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   473
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   474
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   475
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   476
  struct reg gregs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   477
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   478
  jlongArray array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   479
  jlong *regs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   480
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   481
  if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   482
    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
   483
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   484
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   485
#undef NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   486
#undef REG_INDEX
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   487
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   488
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   489
#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
   490
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   491
  array = (*env)->NewLongArray(env, NPRGREG);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   492
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   493
  regs = (*env)->GetLongArrayElements(env, array, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   494
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   495
  regs[REG_INDEX(R15)] = gregs.r_r15;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   496
  regs[REG_INDEX(R14)] = gregs.r_r14;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   497
  regs[REG_INDEX(R13)] = gregs.r_r13;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   498
  regs[REG_INDEX(R12)] = gregs.r_r12;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   499
  regs[REG_INDEX(RBP)] = gregs.r_rbp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   500
  regs[REG_INDEX(RBX)] = gregs.r_rbx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   501
  regs[REG_INDEX(R11)] = gregs.r_r11;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   502
  regs[REG_INDEX(R10)] = gregs.r_r10;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   503
  regs[REG_INDEX(R9)]  = gregs.r_r9;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   504
  regs[REG_INDEX(R8)]  = gregs.r_r8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   505
  regs[REG_INDEX(RAX)] = gregs.r_rax;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   506
  regs[REG_INDEX(RCX)] = gregs.r_rcx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   507
  regs[REG_INDEX(RDX)] = gregs.r_rdx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   508
  regs[REG_INDEX(RSI)] = gregs.r_rsi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   509
  regs[REG_INDEX(RDI)] = gregs.r_rdi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   510
  regs[REG_INDEX(RIP)] = gregs.r_rip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   511
  regs[REG_INDEX(CS)]  = gregs.r_cs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   512
  regs[REG_INDEX(RSP)] = gregs.r_rsp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   513
  regs[REG_INDEX(SS)]  = gregs.r_ss;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   514
  regs[REG_INDEX(FSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   515
  regs[REG_INDEX(GSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   516
  regs[REG_INDEX(DS)] = gregs.r_ds;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   517
  regs[REG_INDEX(ES)] = gregs.r_es;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   518
  regs[REG_INDEX(FS)] = gregs.r_fs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   519
  regs[REG_INDEX(GS)] = gregs.r_gs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   520
  regs[REG_INDEX(TRAPNO)] = gregs.r_trapno;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   521
  regs[REG_INDEX(RFL)]    = gregs.r_rflags;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   522
52713
8586613959f0 8214373: adjust usage of ReleaseLongArrayElements in MacosxDebuggerLocal
mbaesken
parents: 50818
diff changeset
   523
  (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   524
#endif /* amd64 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   525
  return array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   526
}
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   527
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   528
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   529
 * 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
   530
 * 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
   531
 * 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
   532
 * 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
   533
 *
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   534
 * 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
   535
 * 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
   536
 */
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   537
thread_t
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   538
lookupThreadFromThreadId(task_t task, jlong thread_id) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   539
  print_debug("lookupThreadFromThreadId thread_id=0x%llx\n", thread_id);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   540
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   541
  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
   542
  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
   543
  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
   544
  int i;
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   545
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   546
  // 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
   547
  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
   548
  if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   549
    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
   550
    return 0;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   551
  }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   552
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   553
  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
   554
    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
   555
    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
   556
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   557
    // 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
   558
    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
   559
    if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   560
      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
   561
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   562
    }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   563
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   564
    // 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
   565
    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
   566
    {
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   567
      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
   568
      break;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   569
    }
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   570
  }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   571
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   572
  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
   573
  vm_deallocate(mach_task_self(), (vm_address_t) thread_list, thread_list_count);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   574
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   575
  return result_thread;
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   576
}
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   577
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   578
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   579
/*
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   580
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   581
 * Method:    getThreadIntegerRegisterSet0
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   582
 * Signature: (J)[J
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   583
 */
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   584
JNIEXPORT jlongArray JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   585
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   586
  JNIEnv *env, jobject this_obj,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   587
  jlong thread_id)
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   588
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   589
  print_debug("getThreadRegisterSet0 called\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   590
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   591
  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
   592
  if (ph != NULL && ph->core != NULL) {
41fd7e2a5853 8013466: SA crashes when attaching to a process on OS X
sla
parents: 16351
diff changeset
   593
    return getThreadIntegerRegisterSetFromCore(env, this_obj, thread_id, ph);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   594
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   595
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   596
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   597
  thread_t tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   598
  mach_msg_type_number_t count = HSDB_THREAD_STATE_COUNT;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   599
  hsdb_thread_state_t state;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   600
  jlongArray registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   601
  jlong *primitiveArray;
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   602
  task_t gTask = getTask(env, this_obj);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   603
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   604
  tid = lookupThreadFromThreadId(gTask, thread_id);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   605
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   606
  result = thread_get_state(tid, HSDB_THREAD_STATE, (thread_state_t)&state, &count);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   607
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   608
  if (result != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   609
    print_error("getregs: thread_get_state(%d) failed (%d)\n", tid, result);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   610
    return NULL;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   611
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   612
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   613
#if amd64
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   614
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   615
#undef REG_INDEX
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   616
#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
   617
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   618
  // 64 bit
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   619
  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
   620
  registerArray = (*env)->NewLongArray(env, NPRGREG);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   621
  CHECK_EXCEPTION_(0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   622
  primitiveArray = (*env)->GetLongArrayElements(env, registerArray, NULL);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   623
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   624
  primitiveArray[REG_INDEX(R15)] = state.__r15;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   625
  primitiveArray[REG_INDEX(R14)] = state.__r14;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   626
  primitiveArray[REG_INDEX(R13)] = state.__r13;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   627
  primitiveArray[REG_INDEX(R12)] = state.__r12;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   628
  primitiveArray[REG_INDEX(R11)] = state.__r11;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   629
  primitiveArray[REG_INDEX(R10)] = state.__r10;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   630
  primitiveArray[REG_INDEX(R9)]  = state.__r9;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   631
  primitiveArray[REG_INDEX(R8)]  = state.__r8;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   632
  primitiveArray[REG_INDEX(RDI)] = state.__rdi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   633
  primitiveArray[REG_INDEX(RSI)] = state.__rsi;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   634
  primitiveArray[REG_INDEX(RBP)] = state.__rbp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   635
  primitiveArray[REG_INDEX(RBX)] = state.__rbx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   636
  primitiveArray[REG_INDEX(RDX)] = state.__rdx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   637
  primitiveArray[REG_INDEX(RCX)] = state.__rcx;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   638
  primitiveArray[REG_INDEX(RAX)] = state.__rax;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   639
  primitiveArray[REG_INDEX(TRAPNO)] = 0;            // trapno, not used
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   640
  primitiveArray[REG_INDEX(ERR)]    = 0;            // err, not used
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   641
  primitiveArray[REG_INDEX(RIP)] = state.__rip;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   642
  primitiveArray[REG_INDEX(CS)]  = state.__cs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   643
  primitiveArray[REG_INDEX(RFL)] = state.__rflags;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   644
  primitiveArray[REG_INDEX(RSP)] = state.__rsp;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   645
  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
   646
  primitiveArray[REG_INDEX(FS)] = state.__fs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   647
  primitiveArray[REG_INDEX(GS)] = state.__gs;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   648
  primitiveArray[REG_INDEX(ES)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   649
  primitiveArray[REG_INDEX(DS)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   650
  primitiveArray[REG_INDEX(FSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   651
  primitiveArray[REG_INDEX(GSBASE)] = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   652
  print_debug("set registers\n");
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   653
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   654
  (*env)->ReleaseLongArrayElements(env, registerArray, primitiveArray, 0);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   655
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   656
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   657
#error UNSUPPORTED_ARCH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   658
#endif /* amd64 */
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   659
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   660
  return registerArray;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   661
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   662
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   663
/*
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   664
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   665
 * Method:    translateTID0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   666
 * Signature: (I)I
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   667
 */
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   668
JNIEXPORT jint JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   669
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
   670
  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
   671
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   672
  print_debug("translateTID0 called on tid = 0x%x\n", (int)tid);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   673
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   674
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   675
  thread_t foreign_tid, usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   676
  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
   677
  
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   678
  foreign_tid = tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   679
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   680
  task_t gTask = getTask(env, this_obj);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   681
  result = mach_port_extract_right(gTask, foreign_tid, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   682
				   MACH_MSG_TYPE_COPY_SEND, 
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   683
				   &usable_tid, &type);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   684
  if (result != KERN_SUCCESS)
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   685
    return -1;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   686
    
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   687
  print_debug("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   688
    
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   689
  return (jint) usable_tid;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   690
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   691
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   692
// attach to a process/thread specified by "pid"
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   693
static bool ptrace_attach(pid_t pid) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   694
  errno = 0;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   695
  ptrace(PT_ATTACHEXC, pid, 0, 0);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   696
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   697
  if (errno != 0) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   698
    print_error("ptrace_attach: ptrace(PT_ATTACHEXC,...) failed: %s", strerror(errno));
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   699
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   700
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   701
  return true;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   702
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   703
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   704
kern_return_t catch_mach_exception_raise(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   705
  mach_port_t exception_port, mach_port_t thread_port, mach_port_t task_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   706
  exception_type_t exception_type, mach_exception_data_t codes,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   707
  mach_msg_type_number_t num_codes) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   708
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   709
  print_debug("catch_mach_exception_raise: Exception port = %d thread_port = %d "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   710
              "task port %d exc type = %d num_codes %d\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   711
              exception_port, thread_port, task_port, exception_type, num_codes);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   712
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   713
  // This message should denote a Unix soft signal, with
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   714
  // 1. the exception type = EXC_SOFTWARE
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   715
  // 2. codes[0] (which is the code) = EXC_SOFT_SIGNAL
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   716
  // 3. codes[1] (which is the sub-code) = SIGSTOP
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   717
  if (!(exception_type == EXC_SOFTWARE &&
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   718
        codes[0] == EXC_SOFT_SIGNAL    &&
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   719
        codes[num_codes -1] == SIGSTOP)) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   720
    print_error("catch_mach_exception_raise: Message doesn't denote a Unix "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   721
                "soft signal. exception_type = %d, codes[0] = %d, "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   722
                "codes[num_codes -1] = %d, num_codes = %d\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   723
                exception_type, codes[0], codes[num_codes - 1], num_codes);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   724
    return MACH_RCV_INVALID_TYPE;
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   725
  }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   726
  return KERN_SUCCESS;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   727
}
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   728
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   729
kern_return_t catch_mach_exception_raise_state(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   730
  mach_port_t exception_port, exception_type_t exception, const mach_exception_data_t code,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   731
  mach_msg_type_number_t code_cnt, int *flavor, const thread_state_t old_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   732
  mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   733
  mach_msg_type_number_t *new_state_cnt) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   734
  return MACH_RCV_INVALID_TYPE;
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   735
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   736
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   737
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   738
kern_return_t catch_mach_exception_raise_state_identity(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   739
  mach_port_t exception_port, mach_port_t thread, mach_port_t task,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   740
  exception_type_t exception, mach_exception_data_t code,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   741
  mach_msg_type_number_t code_cnt, int *flavor,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   742
  thread_state_t old_state, mach_msg_type_number_t old_state_cnt,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   743
  thread_state_t new_state, mach_msg_type_number_t *new_state_cnt) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   744
  return MACH_RCV_INVALID_TYPE;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   745
}
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   746
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   747
// wait to receive an exception message
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   748
static bool wait_for_exception() {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   749
  kern_return_t result;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   750
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   751
  result = mach_msg(&exc_msg.header,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   752
                    MACH_RCV_MSG,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   753
                    0,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   754
                    sizeof(exc_msg),
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   755
                    tgt_exception_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   756
                    MACH_MSG_TIMEOUT_NONE,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   757
                    MACH_PORT_NULL);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   758
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   759
  if (result != MACH_MSG_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   760
    print_error("attach: wait_for_exception: mach_msg() failed: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   761
                mach_error_string(result), result);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   762
    return false;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   763
  }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   764
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   765
  if (mach_exc_server(&exc_msg.header, &rep_msg.header) == false ||
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   766
      rep_msg.ret_code != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   767
    print_error("attach: wait_for_exception: mach_exc_server failure\n");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   768
    if (rep_msg.ret_code != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   769
      print_error("catch_mach_exception_raise() failed '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   770
                  mach_error_string(rep_msg.ret_code), rep_msg.ret_code);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   771
    }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   772
    return false;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   773
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   774
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   775
  print_debug("reply msg from mach_exc_server: (msg->{bits = %#x, size = %u, "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   776
              "remote_port = %#x, local_port = %#x, reserved = 0x%x, id = 0x%x},)",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   777
              rep_msg.header.msgh_bits, rep_msg.header.msgh_size,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   778
              rep_msg.header.msgh_remote_port, rep_msg.header.msgh_local_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   779
              rep_msg.header.msgh_reserved, rep_msg.header.msgh_id);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   780
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   781
  return true;
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   782
}
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   783
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   784
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   785
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   786
 * Method:    attach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   787
 * Signature: (I)V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   788
 */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   789
JNIEXPORT void JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   790
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
   791
  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
   792
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   793
  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
   794
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   795
JNF_COCOA_ENTER(env);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   796
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   797
  kern_return_t result;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   798
  task_t gTask = 0;
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   799
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   800
  result = task_for_pid(mach_task_self(), jpid, &gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   801
  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
   802
    print_error("attach: task_for_pid(%d) failed: '%s' (%d)\n", (int)jpid, mach_error_string(result), result);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   803
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   804
      "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
   805
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   806
  putTask(env, this_obj, gTask);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   807
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   808
  // Allocate an exception port.
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   809
  result = mach_port_allocate(mach_task_self(),
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   810
                              MACH_PORT_RIGHT_RECEIVE,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   811
                              &tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   812
  if (result != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   813
    print_error("attach: mach_port_allocate() for tgt_exception_port failed: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   814
                mach_error_string(result), result);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   815
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   816
      "Can't attach to the process. Couldn't allocate an exception port.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   817
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   819
  // Enable the new exception port to send messages.
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   820
  result = mach_port_insert_right (mach_task_self(),
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   821
                                   tgt_exception_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   822
                                   tgt_exception_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   823
                                   MACH_MSG_TYPE_MAKE_SEND);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   824
  if (result != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   825
    print_error("attach: mach_port_insert_right() failed for port 0x%x: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   826
                tgt_exception_port, mach_error_string(result), result);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   827
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   828
      "Can't attach to the process. Couldn't add send privileges to the exception port.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   829
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   830
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   831
  // Save the existing original exception ports registered with the target
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   832
  // process (for later restoration while detaching from the process).
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   833
  result = task_get_exception_ports(gTask,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   834
                                    EXC_MASK_ALL,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   835
                                    exception_saved_state.saved_masks,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   836
                                    &exception_saved_state.saved_exception_types_count,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   837
                                    exception_saved_state.saved_ports,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   838
                                    exception_saved_state.saved_behaviors,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   839
                                    exception_saved_state.saved_flavors);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   840
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   841
  if (result != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   842
    print_error("attach: task_get_exception_ports() failed: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   843
                mach_error_string(result), result);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   844
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   845
      "Can't attach to the process. Could not get the target exception ports.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   846
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   847
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   848
  // register the exception port to be used for all future exceptions with the
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   849
  // target process.
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   850
  result = task_set_exception_ports(gTask,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   851
                                    EXC_MASK_ALL,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   852
                                    tgt_exception_port,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   853
                                    EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   854
                                    THREAD_STATE_NONE);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   855
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   856
  if (result != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   857
    print_error("attach: task_set_exception_ports() failed -- port 0x%x: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   858
                tgt_exception_port, mach_error_string(result), result);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   859
    mach_port_deallocate(mach_task_self(), gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   860
    mach_port_deallocate(mach_task_self(), tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   861
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   862
      "Can't attach to the process. Could not register the exception port "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   863
      "with the target process.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   864
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   865
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   866
  // use ptrace to stop the process
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   867
  // 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
   868
  if (ptrace_attach(jpid) != true) {
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   869
    print_error("attach: ptrace failure in attaching to %d\n", (int)jpid);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   870
    mach_port_deallocate(mach_task_self(), gTask);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   871
    mach_port_deallocate(mach_task_self(), tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   872
    THROW_NEW_DEBUGGER_EXCEPTION("Can't ptrace attach to the process");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   873
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   874
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   875
  if (wait_for_exception() != true) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   876
    mach_port_deallocate(mach_task_self(), gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   877
    mach_port_deallocate(mach_task_self(), tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   878
    THROW_NEW_DEBUGGER_EXCEPTION(
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   879
      "Can't attach to the process. Issues with reception of the exception message.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   880
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   881
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   882
  // suspend all the threads in the task
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   883
  result = task_suspend(gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   884
  if (result != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   885
    print_error("attach: task_suspend() failed: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   886
                mach_error_string(result), result);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   887
    mach_port_deallocate(mach_task_self(), gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   888
    mach_port_deallocate(mach_task_self(), tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   889
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach. Unable to suspend all the threads in the task.");
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   890
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
   891
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   892
  id symbolicator = nil;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   893
  id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   894
  if (jrsSymbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   895
    id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   896
    symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   897
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   898
  if (symbolicator != nil) {
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   899
    CFRetain(symbolicator); // pin symbolicator while in java heap
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   900
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   901
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   902
  putSymbolicator(env, this_obj, symbolicator);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   903
  if (symbolicator == nil) {
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   904
    mach_port_deallocate(mach_task_self(), gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   905
    mach_port_deallocate(mach_task_self(), tgt_exception_port);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   906
    THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   907
  }
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   908
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   909
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   910
}
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   911
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   912
/** For core file,
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   913
    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
   914
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
   915
  int n = 0, i = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   916
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   917
  // add load objects
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   918
  n = get_num_libs(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   919
  for (i = 0; i < n; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   920
     uintptr_t base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   921
     const char* name;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   922
     jobject loadObject;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   923
     jobject loadObjectList;
23480
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   924
     jstring nameString;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   925
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   926
     base = get_lib_base(ph, i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   927
     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
   928
     nameString = (*env)->NewStringUTF(env, name);
f00b06ce57c5 8034034: [parfait] JNI exc. pending in hotspot/agent/src/os/bsd/MacosxDebuggerLocal.m
ccheung
parents: 17088
diff changeset
   929
     CHECK_EXCEPTION;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   930
     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
   931
                                            nameString, (jlong)0, (jlong)base);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   932
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   933
     loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   934
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   935
     (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   936
     CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   937
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   938
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   939
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   940
/*
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   941
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   942
 * Method:    attach0
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   943
 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   944
 */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   945
JNIEXPORT void JNICALL
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   946
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
   947
  JNIEnv *env, jobject this_obj, jstring execName, jstring coreName)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   948
{
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   949
  const char *execName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   950
  const char *coreName_cstr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   951
  jboolean isCopy;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   952
  struct ps_prochandle* ph;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   953
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   954
  execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   955
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   956
  coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   957
  CHECK_EXCEPTION;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   958
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   959
  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
   960
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   961
  if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   962
    (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   963
    (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   964
    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
   965
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   966
  (*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
   967
  (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   968
  (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   969
  fillLoadObjects(env, this_obj, ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   970
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   971
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   972
static void detach_cleanup(task_t gTask, JNIEnv *env, jobject this_obj, bool throw_exception) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   973
  mach_port_deallocate(mach_task_self(), tgt_exception_port);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   974
  mach_port_deallocate(mach_task_self(), gTask);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   975
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   976
  id symbolicator = getSymbolicator(env, this_obj);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   977
  if (symbolicator != nil) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   978
    CFRelease(symbolicator);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   979
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   980
  if (throw_exception) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   981
    THROW_NEW_DEBUGGER_EXCEPTION("Cannot detach.");
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   982
  }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   983
}
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
   984
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   985
/*
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   986
 * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   987
 * Method:    detach0
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   988
 * Signature: ()V
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
   989
 */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   990
JNIEXPORT void JNICALL
15593
860cf6c70c06 8006423: SA: NullPointerException in sun.jvm.hotspot.debugger.bsd.BsdThread.getContext(BsdThread.java:67)
sla
parents: 13873
diff changeset
   991
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   992
  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
   993
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   994
  print_debug("detach0 called\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   995
  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
   996
  if (ph != NULL && ph->core != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   997
     Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   998
     return;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
   999
  }
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
  1000
JNF_COCOA_ENTER(env);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1001
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
  1002
  task_t gTask = getTask(env, this_obj);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1003
  kern_return_t k_res = 0;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1004
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1005
  // Restore the pre-saved original exception ports registered with the target process
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1006
  for (uint32_t i = 0; i < exception_saved_state.saved_exception_types_count; ++i) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1007
    k_res = task_set_exception_ports(gTask,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1008
                                     exception_saved_state.saved_masks[i],
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1009
                                     exception_saved_state.saved_ports[i],
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1010
                                     exception_saved_state.saved_behaviors[i],
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1011
                                     exception_saved_state.saved_flavors[i]);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1012
    if (k_res != KERN_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1013
      print_error("detach: task_set_exception_ports failed with %d while "
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1014
                  "restoring the target exception ports.\n", k_res);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1015
      detach_cleanup(gTask, env, this_obj, true);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1016
    }
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1017
  }
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1018
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1019
  // 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
  1020
  int pid;
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1021
  k_res = pid_for_task(gTask, &pid);
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1022
  if (k_res != KERN_SUCCESS) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
  1023
    print_error("detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1024
    detach_cleanup(gTask, env, this_obj, true);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1025
  }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1026
  else {
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1027
    errno = 0;
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1028
    ptrace(PT_DETACH, pid, (caddr_t)1, 0);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1029
    if (errno != 0) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1030
      print_error("detach: ptrace(PT_DETACH,...) failed: %s", strerror(errno));
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1031
      detach_cleanup(gTask, env, this_obj, true);
15738
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1032
    }
fb4662f9df5b 8008102: SA on OS X does not stop the attached process
sla
parents: 15593
diff changeset
  1033
  }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 15868
diff changeset
  1034
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1035
  // reply to the previous exception message
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1036
  k_res = mach_msg(&rep_msg.header,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1037
                   MACH_SEND_MSG| MACH_SEND_INTERRUPT,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1038
                   rep_msg.header.msgh_size,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1039
                   0,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1040
                   MACH_PORT_NULL,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1041
                   MACH_MSG_TIMEOUT_NONE,
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1042
                   MACH_PORT_NULL);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1043
  if (k_res != MACH_MSG_SUCCESS) {
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1044
    print_error("detach: mach_msg() for replying to pending exceptions failed: '%s' (%d)\n",
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1045
                 mach_error_string(k_res), k_res);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1046
    detach_cleanup(gTask, env, this_obj, true);
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
  1047
  }
50818
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1048
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1049
  detach_cleanup(gTask, env, this_obj, false);
e46b9e514479 8189429: SA: MacOSX: Replace the deprecated PT_ATTACH with PT_ATTACHEXC
jgeorge
parents: 47635
diff changeset
  1050
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
  1051
JNF_COCOA_EXIT(env);
91935236600e 7098194: integrate macosx-port changes
dcubed
parents:
diff changeset
  1052
}