147 148 private static String parseFileName(String opt) { 149 // opt starts with "file=" 150 if (opt.length() > 5) { 151 // pass whole "file=" string 152 String filename = opt.substring(5); 153 try { 154 // Get the canonical path - important to avoid just 155 // passing a "heap.bin" and having the dump created 156 // in the target VM working directory rather than the 157 // directory where jmap is executed. 158 return new File(filename).getCanonicalPath(); 159 } catch (IOException ioe) { 160 return null; 161 } 162 } 163 // no filename 164 return null; 165 } 166 167 private static String add_option(String cmd, String opt) { 168 if (cmd.isEmpty()) { 169 return opt; 170 } 171 return cmd + "," + opt; 172 } 173 174 private static void histo(String pid, String options) 175 throws AttachNotSupportedException, IOException, 176 UnsupportedEncodingException { 177 String liveopt = "-all"; 178 String filename = null; 179 String parallel = null; 180 String subopts[] = options.split(","); 181 boolean set_all = false; 182 boolean set_live = false; 183 String cmdline = ""; 184 185 for (int i = 0; i < subopts.length; i++) { 186 String subopt = subopts[i]; 187 if (subopt.equals("") || subopt.equals("all")) { 188 cmdline = add_option(cmdline, "-all"); 189 set_all = true; 190 } else if (subopt.equals("live")) { 191 // Add '-' for compatibility. 192 cmdline = add_option(cmdline, "-live"); 193 set_live = true; 194 } else if (subopt.startsWith("file=")) { 195 filename = parseFileName(subopt); 196 if (filename == null) { 197 usage(1); // invalid options or no filename 198 } 199 cmdline = add_option(cmdline, filename); 200 } else if (subopt.startsWith("parallel=")) { 201 parallel = subopt.substring("parallel=".length()); 202 if (parallel == null) { 203 usage(1); 204 } 205 // Add "parallelThreadsNum=<>" for later check 206 cmdline = add_option(cmdline, subopt); 207 } else { 208 usage(1); 209 } 210 } 211 if (set_live && set_all) { 212 usage(1); 213 } 214 215 System.out.flush(); 216 // inspectHeap is not the same as jcmd GC.class_histogram 217 executeCommandForPid(pid, "inspectheap", cmdline); 218 } 219 220 private static void dump(String pid, String options) 221 throws AttachNotSupportedException, IOException, 222 UnsupportedEncodingException { 223 224 String subopts[] = options.split(","); 225 String filename = null; 226 String liveopt = "-all"; 227 228 for (int i = 0; i < subopts.length; i++) { 229 String subopt = subopts[i]; 230 if (subopt.equals("live")) { 231 liveopt = "-live"; 232 } else if (subopt.startsWith("file=")) { 233 filename = parseFileName(subopt); 234 } 235 } 236 237 if (filename == null) { | 147 148 private static String parseFileName(String opt) { 149 // opt starts with "file=" 150 if (opt.length() > 5) { 151 // pass whole "file=" string 152 String filename = opt.substring(5); 153 try { 154 // Get the canonical path - important to avoid just 155 // passing a "heap.bin" and having the dump created 156 // in the target VM working directory rather than the 157 // directory where jmap is executed. 158 return new File(filename).getCanonicalPath(); 159 } catch (IOException ioe) { 160 return null; 161 } 162 } 163 // no filename 164 return null; 165 } 166 167 private static void histo(String pid, String options) 168 throws AttachNotSupportedException, IOException, 169 UnsupportedEncodingException { 170 String liveopt = "-all"; 171 String filename = null; 172 String parallel = null; 173 String subopts[] = options.split(","); 174 boolean set_all = false; 175 boolean set_live = false; 176 177 for (int i = 0; i < subopts.length; i++) { 178 String subopt = subopts[i]; 179 if (subopt.equals("") || subopt.equals("all")) { 180 set_all = true; 181 liveopt = "-all"; 182 } else if (subopt.equals("live")) { 183 // Add '-' for compatibility. 184 set_live = true; 185 liveopt = "-live"; 186 } else if (subopt.startsWith("file=")) { 187 filename = parseFileName(subopt); 188 if (filename == null) { 189 usage(1); // invalid options or no filename 190 } 191 } else if (subopt.startsWith("parallel=")) { 192 parallel = subopt.substring("parallel=".length()); 193 if (parallel == null) { 194 usage(1); 195 } 196 } else { 197 usage(1); 198 } 199 } 200 if (set_live && set_all) { 201 usage(1); 202 } 203 204 System.out.flush(); 205 // inspectHeap is not the same as jcmd GC.class_histogram 206 executeCommandForPid(pid, "inspectheap", liveopt, filename, parallel); 207 } 208 209 private static void dump(String pid, String options) 210 throws AttachNotSupportedException, IOException, 211 UnsupportedEncodingException { 212 213 String subopts[] = options.split(","); 214 String filename = null; 215 String liveopt = "-all"; 216 217 for (int i = 0; i < subopts.length; i++) { 218 String subopt = subopts[i]; 219 if (subopt.equals("live")) { 220 liveopt = "-live"; 221 } else if (subopt.startsWith("file=")) { 222 filename = parseFileName(subopt); 223 } 224 } 225 226 if (filename == null) { |