2 * Copyright jattach authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 extern int is_openj9_process(int pid);
25 extern int jattach_openj9(int pid, int nspid, int argc, char** argv, int print_output);
26 extern int jattach_hotspot(int pid, int nspid, int argc, char** argv, int print_output);
31 __attribute__((visibility("default")))
32 int jattach(int pid, int argc, char** argv, int print_output) {
33 uid_t my_uid = geteuid();
34 gid_t my_gid = getegid();
35 uid_t target_uid = my_uid;
36 gid_t target_gid = my_gid;
38 if (get_process_info(pid, &target_uid, &target_gid, &nspid) < 0) {
39 fprintf(stderr, "Process %d not found\n", pid);
43 // Container support: switch to the target namespaces.
44 // Network and IPC namespaces are essential for OpenJ9 connection.
47 mnt_changed = enter_ns(pid, "mnt");
49 // In HotSpot, dynamic attach is allowed only for the clients with the same euid/egid.
50 // If we are running under root, switch to the required euid/egid automatically.
51 if ((my_gid != target_gid && setegid(target_gid) != 0) ||
52 (my_uid != target_uid && seteuid(target_uid) != 0)) {
53 perror("Failed to change credentials to match the target process");
57 get_tmp_path(mnt_changed > 0 ? nspid : pid);
59 // Make write() return EPIPE instead of abnormal process termination
60 signal(SIGPIPE, SIG_IGN);
62 if (is_openj9_process(nspid)) {
63 return jattach_openj9(pid, nspid, argc, argv, print_output);
65 return jattach_hotspot(pid, nspid, argc, argv, print_output);
69 #ifdef JATTACH_VERSION
71 int main(int argc, char** argv) {
73 printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
75 "Usage: jattach <pid> <cmd> [args ...]\n"
78 " load threaddump dumpheap setflag properties\n"
79 " jcmd inspectheap datadump printflag agentProperties\n"
84 int pid = atoi(argv[1]);
86 fprintf(stderr, "%s is not a valid process ID\n", argv[1]);
90 return jattach(pid, argc - 2, argv + 2, 1);
93 #endif // JATTACH_VERSION