1 /*
2 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include <objc/objc-runtime.h>
26 #import <Foundation/Foundation.h>
27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
28 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
29
30 #include <jni.h>
31
32 #import <mach/mach.h>
33 #import <mach/mach_types.h>
34 #import <sys/sysctl.h>
35 #import <stdio.h>
36 #import <stdarg.h>
37 #import <stdlib.h>
38 #import <strings.h>
39 #import <dlfcn.h>
40 #import <limits.h>
41 #import <errno.h>
42 #import <sys/types.h>
43 #import <sys/ptrace.h>
44 #include "libproc_impl.h"
45
46 #define UNSUPPORTED_ARCH "Unsupported architecture!"
47
48 #if defined(x86_64) && !defined(amd64)
49 #define amd64 1
50 #endif
51
52 #if amd64
53 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
54 #else
55 #error UNSUPPORTED_ARCH
56 #endif
57
58 static jfieldID symbolicatorID = 0; // set in _init0
59 static jfieldID taskID = 0; // set in _init0
60
61 static jfieldID p_ps_prochandle_ID = 0;
62 static jfieldID loadObjectList_ID = 0;
63 static jmethodID listAdd_ID = 0;
64
65 static jmethodID createClosestSymbol_ID = 0;
66 static jmethodID createLoadObject_ID = 0;
67 static jmethodID getJavaThreadsInfo_ID = 0;
68
69 // indicator if thread id (lwpid_t) was set
70 static bool _threads_filled = false;
71
72 static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
73 (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
74 }
75
76 static id getSymbolicator(JNIEnv *env, jobject this_obj) {
77 jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
78 return (id)(intptr_t)ptr;
79 }
80
81 static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
82 (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
83 }
84
85 static task_t getTask(JNIEnv *env, jobject this_obj) {
86 jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
87 return (task_t)ptr;
88 }
89
90 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
91 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
613 print_debug("translateTID0 called on tid = 0x%x\n", (int)tid);
614
615 kern_return_t result;
616 thread_t foreign_tid, usable_tid;
617 mach_msg_type_name_t type;
618
619 foreign_tid = tid;
620
621 task_t gTask = getTask(env, this_obj);
622 result = mach_port_extract_right(gTask, foreign_tid,
623 MACH_MSG_TYPE_COPY_SEND,
624 &usable_tid, &type);
625 if (result != KERN_SUCCESS)
626 return -1;
627
628 print_debug("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
629
630 return (jint) usable_tid;
631 }
632
633
634 static bool ptrace_continue(pid_t pid, int signal) {
635 // pass the signal to the process so we don't swallow it
636 int res;
637 if ((res = ptrace(PT_CONTINUE, pid, (caddr_t)1, signal)) < 0) {
638 print_error("attach: ptrace(PT_CONTINUE, %d) failed with %d\n", pid, res);
639 return false;
640 }
641 return true;
642 }
643
644 // waits until the ATTACH has stopped the process
645 // by signal SIGSTOP
646 static bool ptrace_waitpid(pid_t pid) {
647 int ret;
648 int status;
649 while (true) {
650 // Wait for debuggee to stop.
651 ret = waitpid(pid, &status, 0);
652 if (ret >= 0) {
653 if (WIFSTOPPED(status)) {
654 // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
655 // will still be pending and delivered when the process is DETACHED and the process
656 // will go to sleep.
657 if (WSTOPSIG(status) == SIGSTOP) {
658 // Debuggee stopped by SIGSTOP.
659 return true;
660 }
661 if (!ptrace_continue(pid, WSTOPSIG(status))) {
662 print_error("attach: Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
663 return false;
664 }
665 } else {
666 print_error("attach: waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
667 return false;
668 }
669 } else {
670 switch (errno) {
671 case EINTR:
672 continue;
673 break;
674 case ECHILD:
675 print_error("attach: waitpid() failed. Child process pid (%d) does not exist \n", pid);
676 break;
677 case EINVAL:
678 print_error("attach: waitpid() failed. Invalid options argument.\n");
679 break;
680 default:
681 print_error("attach: waitpid() failed. Unexpected error %d\n",errno);
682 break;
683 }
684 return false;
685 }
686 }
687 }
688
689 // attach to a process/thread specified by "pid"
690 static bool ptrace_attach(pid_t pid) {
691 int res;
692 #ifdef __clang__
693 #pragma clang diagnostic push
694 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
695 #endif
696 if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
697 print_error("ptrace(PT_ATTACH, %d) failed with %d\n", pid, res);
698 #ifdef __clang__
699 #pragma clang diagnostic pop
700 #endif
701 return false;
702 } else {
703 return ptrace_waitpid(pid);
704 }
705 }
706
707 /*
708 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
709 * Method: attach0
710 * Signature: (I)V
711 */
712 JNIEXPORT void JNICALL
713 Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(
714 JNIEnv *env, jobject this_obj, jint jpid)
715 {
716 print_debug("attach0 called for jpid=%d\n", (int)jpid);
717
718 JNF_COCOA_ENTER(env);
719
720 kern_return_t result;
721 task_t gTask = 0;
722 result = task_for_pid(mach_task_self(), jpid, &gTask);
723 if (result != KERN_SUCCESS) {
724 print_error("attach: task_for_pid(%d) failed: '%s' (%d)\n", (int)jpid, mach_error_string(result), result);
725 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.");
726 }
727 putTask(env, this_obj, gTask);
728
729 // use ptrace to stop the process
730 // on os x, ptrace only needs to be called on the process, not the individual threads
731 if (ptrace_attach(jpid) != true) {
732 mach_port_deallocate(mach_task_self(), gTask);
733 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
734 }
735
736 id symbolicator = nil;
737 id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
738 if (jrsSymbolicator != nil) {
739 id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
740 symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
741 }
742 if (symbolicator != nil) {
743 CFRetain(symbolicator); // pin symbolicator while in java heap
744 }
745
746 putSymbolicator(env, this_obj, symbolicator);
747 if (symbolicator == nil) {
748 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
749 }
750
751 JNF_COCOA_EXIT(env);
752 }
753
754 /** For core file,
755 called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 */
756 static void fillLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
757 int n = 0, i = 0;
758
759 // add load objects
760 n = get_num_libs(ph);
761 for (i = 0; i < n; i++) {
762 uintptr_t base;
763 const char* name;
764 jobject loadObject;
765 jobject loadObjectList;
766 jstring nameString;
767
794 struct ps_prochandle* ph;
795
796 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
797 CHECK_EXCEPTION;
798 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
799 CHECK_EXCEPTION;
800
801 print_debug("attach: %s %s\n", execName_cstr, coreName_cstr);
802
803 if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
804 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
805 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
806 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
807 }
808 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
809 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
810 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
811 fillLoadObjects(env, this_obj, ph);
812 }
813
814 /*
815 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
816 * Method: detach0
817 * Signature: ()V
818 */
819 JNIEXPORT void JNICALL
820 Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
821 JNIEnv *env, jobject this_obj)
822 {
823 print_debug("detach0 called\n");
824 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
825 if (ph != NULL && ph->core != NULL) {
826 Prelease(ph);
827 return;
828 }
829 JNF_COCOA_ENTER(env);
830 task_t gTask = getTask(env, this_obj);
831
832 // detach from the ptraced process causing it to resume execution
833 int pid;
834 kern_return_t k_res;
835 k_res = pid_for_task(gTask, &pid);
836 if (k_res != KERN_SUCCESS) {
837 print_error("detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
838 }
839 else {
840 int res = ptrace(PT_DETACH, pid, 0, 0);
841 if (res < 0) {
842 print_error("detach: ptrace(PT_DETACH, %d) failed (%d)\n", pid, res);
843 }
844 }
845
846 mach_port_deallocate(mach_task_self(), gTask);
847 id symbolicator = getSymbolicator(env, this_obj);
848 if (symbolicator != nil) {
849 CFRelease(symbolicator);
850 }
851 JNF_COCOA_EXIT(env);
852 }
|
1 /*
2 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include <objc/objc-runtime.h>
26 #import <Foundation/Foundation.h>
27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
28 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
29
30 #include <jni.h>
31
32 #import <mach/mach.h>
33 #import <mach/mach_types.h>
34 #import <sys/sysctl.h>
35 #import <stdio.h>
36 #import <string.h>
37 #import <stdarg.h>
38 #import <stdlib.h>
39 #import <strings.h>
40 #import <dlfcn.h>
41 #import <limits.h>
42 #import <errno.h>
43 #import <sys/types.h>
44 #import <sys/ptrace.h>
45 #include "libproc_impl.h"
46
47 #define UNSUPPORTED_ARCH "Unsupported architecture!"
48
49 #if defined(x86_64) && !defined(amd64)
50 #define amd64 1
51 #endif
52
53 #if amd64
54 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
55 #else
56 #error UNSUPPORTED_ARCH
57 #endif
58
59 static jfieldID symbolicatorID = 0; // set in _init0
60 static jfieldID taskID = 0; // set in _init0
61
62 static jfieldID p_ps_prochandle_ID = 0;
63 static jfieldID loadObjectList_ID = 0;
64 static jmethodID listAdd_ID = 0;
65
66 static jmethodID createClosestSymbol_ID = 0;
67 static jmethodID createLoadObject_ID = 0;
68 static jmethodID getJavaThreadsInfo_ID = 0;
69
70 // indicator if thread id (lwpid_t) was set
71 static bool _threads_filled = false;
72
73 // mach_exc_server defined in the generated mach_excServer.c
74 extern boolean_t mach_exc_server(mach_msg_header_t *input_msg_hdr,
75 mach_msg_header_t *output_msg_hdr);
76
77 kern_return_t catch_mach_exception_raise(
78 mach_port_t exception_port, mach_port_t thread,
79 mach_port_t task, exception_type_t exception,
80 mach_exception_data_t code,
81 mach_msg_type_number_t code_cnt);
82
83 kern_return_t catch_mach_exception_raise_state(
84 mach_port_t exception_port, exception_type_t exception,
85 const mach_exception_data_t code, mach_msg_type_number_t code_cnt,
86 int *flavor, const thread_state_t old_state,
87 mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
88 mach_msg_type_number_t *new_state_cnt);
89
90 kern_return_t catch_mach_exception_raise_state_identity(
91 mach_port_t exception_port, mach_port_t thread, mach_port_t task,
92 exception_type_t exception, mach_exception_data_t code,
93 mach_msg_type_number_t code_cnt, int *flavor, thread_state_t old_state,
94 mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
95 mach_msg_type_number_t *new_state_cnt);
96
97 static struct exception_saved_state {
98 exception_mask_t saved_masks[EXC_TYPES_COUNT];
99 mach_port_t saved_ports[EXC_TYPES_COUNT];
100 exception_behavior_t saved_behaviors[EXC_TYPES_COUNT];
101 thread_state_flavor_t saved_flavors[EXC_TYPES_COUNT];
102 mach_msg_type_number_t saved_exception_types_count;
103 } exception_saved_state;
104
105 static mach_port_t tgt_exception_port;
106
107 // Mirrors __Reply__mach_exception_raise_t generated in mach_excServer.c
108 static struct rep_msg {
109 mach_msg_header_t header;
110 NDR_record_t ndr;
111 kern_return_t ret_code;
112 } rep_msg;
113
114 // Mirrors __Request__mach_exception_raise_t generated in mach_excServer.c
115 // with a large trailing pad to avoid MACH_MSG_RCV_TOO_LARGE
116 static struct exc_msg {
117 mach_msg_header_t header;
118 // start of the kernel processed data
119 mach_msg_body_t msgh_body;
120 mach_msg_port_descriptor_t thread;
121 mach_msg_port_descriptor_t task;
122 // end of the kernel processed data
123 NDR_record_t ndr;
124 exception_type_t exception;
125 mach_msg_type_number_t code_cnt;
126 mach_exception_data_t code; // an array of int64_t
127 char pad[512];
128 } exc_msg;
129
130 static void putSymbolicator(JNIEnv *env, jobject this_obj, id symbolicator) {
131 (*env)->SetLongField(env, this_obj, symbolicatorID, (jlong)(intptr_t)symbolicator);
132 }
133
134 static id getSymbolicator(JNIEnv *env, jobject this_obj) {
135 jlong ptr = (*env)->GetLongField(env, this_obj, symbolicatorID);
136 return (id)(intptr_t)ptr;
137 }
138
139 static void putTask(JNIEnv *env, jobject this_obj, task_t task) {
140 (*env)->SetLongField(env, this_obj, taskID, (jlong)task);
141 }
142
143 static task_t getTask(JNIEnv *env, jobject this_obj) {
144 jlong ptr = (*env)->GetLongField(env, this_obj, taskID);
145 return (task_t)ptr;
146 }
147
148 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
149 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
671 print_debug("translateTID0 called on tid = 0x%x\n", (int)tid);
672
673 kern_return_t result;
674 thread_t foreign_tid, usable_tid;
675 mach_msg_type_name_t type;
676
677 foreign_tid = tid;
678
679 task_t gTask = getTask(env, this_obj);
680 result = mach_port_extract_right(gTask, foreign_tid,
681 MACH_MSG_TYPE_COPY_SEND,
682 &usable_tid, &type);
683 if (result != KERN_SUCCESS)
684 return -1;
685
686 print_debug("translateTID0: 0x%x -> 0x%x\n", foreign_tid, usable_tid);
687
688 return (jint) usable_tid;
689 }
690
691 // attach to a process/thread specified by "pid"
692 static bool ptrace_attach(pid_t pid) {
693 errno = 0;
694 ptrace(PT_ATTACHEXC, pid, 0, 0);
695
696 if (errno != 0) {
697 print_error("ptrace_attach: ptrace(PT_ATTACHEXC,...) failed: %s", strerror(errno));
698 return false;
699 }
700 return true;
701 }
702
703 kern_return_t catch_mach_exception_raise(
704 mach_port_t exception_port, mach_port_t thread_port, mach_port_t task_port,
705 exception_type_t exception_type, mach_exception_data_t codes,
706 mach_msg_type_number_t num_codes) {
707
708 print_debug("catch_mach_exception_raise: Exception port = %d thread_port = %d "
709 "task port %d exc type = %d num_codes %d\n",
710 exception_port, thread_port, task_port, exception_type, num_codes);
711
712 // This message should denote a Unix soft signal, with
713 // 1. the exception type = EXC_SOFTWARE
714 // 2. codes[0] (which is the code) = EXC_SOFT_SIGNAL
715 // 3. codes[1] (which is the sub-code) = SIGSTOP
716 if (!(exception_type == EXC_SOFTWARE &&
717 codes[0] == EXC_SOFT_SIGNAL &&
718 codes[num_codes -1] == SIGSTOP)) {
719 print_error("catch_mach_exception_raise: Message doesn't denote a Unix "
720 "soft signal. exception_type = %d, codes[0] = %d, "
721 "codes[num_codes -1] = %d, num_codes = %d\n",
722 exception_type, codes[0], codes[num_codes - 1], num_codes);
723 return MACH_RCV_INVALID_TYPE;
724 }
725 return KERN_SUCCESS;
726 }
727
728 kern_return_t catch_mach_exception_raise_state(
729 mach_port_t exception_port, exception_type_t exception, const mach_exception_data_t code,
730 mach_msg_type_number_t code_cnt, int *flavor, const thread_state_t old_state,
731 mach_msg_type_number_t old_state_cnt, thread_state_t new_state,
732 mach_msg_type_number_t *new_state_cnt) {
733 return MACH_RCV_INVALID_TYPE;
734 }
735
736
737 kern_return_t catch_mach_exception_raise_state_identity(
738 mach_port_t exception_port, mach_port_t thread, mach_port_t task,
739 exception_type_t exception, mach_exception_data_t code,
740 mach_msg_type_number_t code_cnt, int *flavor,
741 thread_state_t old_state, mach_msg_type_number_t old_state_cnt,
742 thread_state_t new_state, mach_msg_type_number_t *new_state_cnt) {
743 return MACH_RCV_INVALID_TYPE;
744 }
745
746 // wait to receive an exception message
747 static bool wait_for_exception() {
748 kern_return_t result;
749
750 result = mach_msg(&exc_msg.header,
751 MACH_RCV_MSG,
752 0,
753 sizeof(exc_msg),
754 tgt_exception_port,
755 MACH_MSG_TIMEOUT_NONE,
756 MACH_PORT_NULL);
757
758 if (result != MACH_MSG_SUCCESS) {
759 print_error("attach: wait_for_exception: mach_msg() failed: '%s' (%d)\n",
760 mach_error_string(result), result);
761 return false;
762 }
763
764 if (mach_exc_server(&exc_msg.header, &rep_msg.header) == false ||
765 rep_msg.ret_code != KERN_SUCCESS) {
766 print_error("attach: wait_for_exception: mach_exc_server failure\n");
767 if (rep_msg.ret_code != KERN_SUCCESS) {
768 print_error("catch_mach_exception_raise() failed '%s' (%d)\n",
769 mach_error_string(rep_msg.ret_code), rep_msg.ret_code);
770 }
771 return false;
772 }
773
774 print_debug("reply msg from mach_exc_server: (msg->{bits = %#x, size = %u, "
775 "remote_port = %#x, local_port = %#x, reserved = 0x%x, id = 0x%x},)",
776 rep_msg.header.msgh_bits, rep_msg.header.msgh_size,
777 rep_msg.header.msgh_remote_port, rep_msg.header.msgh_local_port,
778 rep_msg.header.msgh_reserved, rep_msg.header.msgh_id);
779
780 return true;
781 }
782
783 /*
784 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
785 * Method: attach0
786 * Signature: (I)V
787 */
788 JNIEXPORT void JNICALL
789 Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(
790 JNIEnv *env, jobject this_obj, jint jpid)
791 {
792 print_debug("attach0 called for jpid=%d\n", (int)jpid);
793
794 JNF_COCOA_ENTER(env);
795
796 kern_return_t result;
797 task_t gTask = 0;
798
799 result = task_for_pid(mach_task_self(), jpid, &gTask);
800 if (result != KERN_SUCCESS) {
801 print_error("attach: task_for_pid(%d) failed: '%s' (%d)\n", (int)jpid, mach_error_string(result), result);
802 THROW_NEW_DEBUGGER_EXCEPTION(
803 "Can't attach to the process. Could be caused by an incorrect pid or lack of privileges.");
804 }
805 putTask(env, this_obj, gTask);
806
807 // Allocate an exception port.
808 result = mach_port_allocate(mach_task_self(),
809 MACH_PORT_RIGHT_RECEIVE,
810 &tgt_exception_port);
811 if (result != KERN_SUCCESS) {
812 print_error("attach: mach_port_allocate() for tgt_exception_port failed: '%s' (%d)\n",
813 mach_error_string(result), result);
814 THROW_NEW_DEBUGGER_EXCEPTION(
815 "Can't attach to the process. Couldn't allocate an exception port.");
816 }
817
818 // Enable the new exception port to send messages.
819 result = mach_port_insert_right (mach_task_self(),
820 tgt_exception_port,
821 tgt_exception_port,
822 MACH_MSG_TYPE_MAKE_SEND);
823 if (result != KERN_SUCCESS) {
824 print_error("attach: mach_port_insert_right() failed for port 0x%x: '%s' (%d)\n",
825 tgt_exception_port, mach_error_string(result), result);
826 THROW_NEW_DEBUGGER_EXCEPTION(
827 "Can't attach to the process. Couldn't add send privileges to the exception port.");
828 }
829
830 // Save the existing original exception ports registered with the target
831 // process (for later restoration while detaching from the process).
832 result = task_get_exception_ports(gTask,
833 EXC_MASK_ALL,
834 exception_saved_state.saved_masks,
835 &exception_saved_state.saved_exception_types_count,
836 exception_saved_state.saved_ports,
837 exception_saved_state.saved_behaviors,
838 exception_saved_state.saved_flavors);
839
840 if (result != KERN_SUCCESS) {
841 print_error("attach: task_get_exception_ports() failed: '%s' (%d)\n",
842 mach_error_string(result), result);
843 THROW_NEW_DEBUGGER_EXCEPTION(
844 "Can't attach to the process. Could not get the target exception ports.");
845 }
846
847 // register the exception port to be used for all future exceptions with the
848 // target process.
849 result = task_set_exception_ports(gTask,
850 EXC_MASK_ALL,
851 tgt_exception_port,
852 EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES,
853 THREAD_STATE_NONE);
854
855 if (result != KERN_SUCCESS) {
856 print_error("attach: task_set_exception_ports() failed -- port 0x%x: '%s' (%d)\n",
857 tgt_exception_port, mach_error_string(result), result);
858 mach_port_deallocate(mach_task_self(), gTask);
859 mach_port_deallocate(mach_task_self(), tgt_exception_port);
860 THROW_NEW_DEBUGGER_EXCEPTION(
861 "Can't attach to the process. Could not register the exception port "
862 "with the target process.");
863 }
864
865 // use ptrace to stop the process
866 // on os x, ptrace only needs to be called on the process, not the individual threads
867 if (ptrace_attach(jpid) != true) {
868 print_error("attach: ptrace failure in attaching to %d\n", (int)jpid);
869 mach_port_deallocate(mach_task_self(), gTask);
870 mach_port_deallocate(mach_task_self(), tgt_exception_port);
871 THROW_NEW_DEBUGGER_EXCEPTION("Can't ptrace attach to the process");
872 }
873
874 if (wait_for_exception() != true) {
875 mach_port_deallocate(mach_task_self(), gTask);
876 mach_port_deallocate(mach_task_self(), tgt_exception_port);
877 THROW_NEW_DEBUGGER_EXCEPTION(
878 "Can't attach to the process. Issues with reception of the exception message.");
879 }
880
881 // suspend all the threads in the task
882 result = task_suspend(gTask);
883 if (result != KERN_SUCCESS) {
884 print_error("attach: task_suspend() failed: '%s' (%d)\n",
885 mach_error_string(result), result);
886 mach_port_deallocate(mach_task_self(), gTask);
887 mach_port_deallocate(mach_task_self(), tgt_exception_port);
888 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach. Unable to suspend all the threads in the task.");
889 }
890
891 id symbolicator = nil;
892 id jrsSymbolicator = objc_lookUpClass("JRSSymbolicator");
893 if (jrsSymbolicator != nil) {
894 id (*dynamicCall)(id, SEL, pid_t) = (id (*)(id, SEL, pid_t))&objc_msgSend;
895 symbolicator = dynamicCall(jrsSymbolicator, @selector(symbolicatorForPid:), (pid_t)jpid);
896 }
897 if (symbolicator != nil) {
898 CFRetain(symbolicator); // pin symbolicator while in java heap
899 }
900
901 putSymbolicator(env, this_obj, symbolicator);
902 if (symbolicator == nil) {
903 mach_port_deallocate(mach_task_self(), gTask);
904 mach_port_deallocate(mach_task_self(), tgt_exception_port);
905 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach symbolicator to the process");
906 }
907
908 JNF_COCOA_EXIT(env);
909 }
910
911 /** For core file,
912 called from Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 */
913 static void fillLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
914 int n = 0, i = 0;
915
916 // add load objects
917 n = get_num_libs(ph);
918 for (i = 0; i < n; i++) {
919 uintptr_t base;
920 const char* name;
921 jobject loadObject;
922 jobject loadObjectList;
923 jstring nameString;
924
951 struct ps_prochandle* ph;
952
953 execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
954 CHECK_EXCEPTION;
955 coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
956 CHECK_EXCEPTION;
957
958 print_debug("attach: %s %s\n", execName_cstr, coreName_cstr);
959
960 if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
961 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
962 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
963 THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
964 }
965 (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
966 (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
967 (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
968 fillLoadObjects(env, this_obj, ph);
969 }
970
971 static void detach_cleanup(task_t gTask, JNIEnv *env, jobject this_obj, bool throw_exception) {
972 mach_port_deallocate(mach_task_self(), tgt_exception_port);
973 mach_port_deallocate(mach_task_self(), gTask);
974
975 id symbolicator = getSymbolicator(env, this_obj);
976 if (symbolicator != nil) {
977 CFRelease(symbolicator);
978 }
979 if (throw_exception) {
980 THROW_NEW_DEBUGGER_EXCEPTION("Cannot detach.");
981 }
982 }
983
984 /*
985 * Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
986 * Method: detach0
987 * Signature: ()V
988 */
989 JNIEXPORT void JNICALL
990 Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(
991 JNIEnv *env, jobject this_obj)
992 {
993 print_debug("detach0 called\n");
994 struct ps_prochandle* ph = get_proc_handle(env, this_obj);
995 if (ph != NULL && ph->core != NULL) {
996 Prelease(ph);
997 return;
998 }
999 JNF_COCOA_ENTER(env);
1000
1001 task_t gTask = getTask(env, this_obj);
1002 kern_return_t k_res = 0;
1003
1004 // Restore the pre-saved original exception ports registered with the target process
1005 for (uint32_t i = 0; i < exception_saved_state.saved_exception_types_count; ++i) {
1006 k_res = task_set_exception_ports(gTask,
1007 exception_saved_state.saved_masks[i],
1008 exception_saved_state.saved_ports[i],
1009 exception_saved_state.saved_behaviors[i],
1010 exception_saved_state.saved_flavors[i]);
1011 if (k_res != KERN_SUCCESS) {
1012 print_error("detach: task_set_exception_ports failed with %d while "
1013 "restoring the target exception ports.\n", k_res);
1014 detach_cleanup(gTask, env, this_obj, true);
1015 }
1016 }
1017
1018 // detach from the ptraced process causing it to resume execution
1019 int pid;
1020 k_res = pid_for_task(gTask, &pid);
1021 if (k_res != KERN_SUCCESS) {
1022 print_error("detach: pid_for_task(%d) failed (%d)\n", pid, k_res);
1023 detach_cleanup(gTask, env, this_obj, true);
1024 }
1025 else {
1026 errno = 0;
1027 ptrace(PT_DETACH, pid, (caddr_t)1, 0);
1028 if (errno != 0) {
1029 print_error("detach: ptrace(PT_DETACH,...) failed: %s", strerror(errno));
1030 detach_cleanup(gTask, env, this_obj, true);
1031 }
1032 }
1033
1034 // reply to the previous exception message
1035 k_res = mach_msg(&rep_msg.header,
1036 MACH_SEND_MSG| MACH_SEND_INTERRUPT,
1037 rep_msg.header.msgh_size,
1038 0,
1039 MACH_PORT_NULL,
1040 MACH_MSG_TIMEOUT_NONE,
1041 MACH_PORT_NULL);
1042 if (k_res != MACH_MSG_SUCCESS) {
1043 print_error("detach: mach_msg() for replying to pending exceptions failed: '%s' (%d)\n",
1044 mach_error_string(k_res), k_res);
1045 detach_cleanup(gTask, env, this_obj, true);
1046 }
1047
1048 detach_cleanup(gTask, env, this_obj, false);
1049
1050 JNF_COCOA_EXIT(env);
1051 }
|