Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstack/utils/DefaultFormat.java
41159 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package utils;2324import java.util.Map;25import java.util.Scanner;26import java.util.regex.MatchResult;2728/**29*30* jstack default format 2008-03-05 18:36:26 Full thread dump Java HotSpot(TM)31* Client VM (11.0-b11 mixed mode):32*33* "Thread-16" #10 daemon prio=3 os_prio=0 tid=0x0814d800 nid=0x1d runnable34* [0xf394d000..0xf394d9f0] java.lang.Thread.State: RUNNABLE at35* java.net.SocketInputStream.socketRead0(Native Method) at36* java.net.SocketInputStream.read(SocketInputStream.java:129) at37* java.net.SocketInputStream.read(SocketInputStream.java:182) at38* java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2249)39* at40* java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2542)41* at42* java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)43* at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297) at44* java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at45* tmtools.share.debuggee.DebuggeeProtocolHandler.run(DebuggeeProtocolHandler.java:32)46*47* Locked ownable synchronizers: - None ....48*49* Note that os_prio field is optional and will be printed only if JVM was able50* to get native thread priority.51*/52public class DefaultFormat implements Format {5354protected String threadInfoPattern() {55return "^\"(.*)\"\\s(#\\d+\\s|)(daemon\\s|)prio=(.+)\\s(os_prio=(.+)\\s|)tid=(.+)\\snid=(.+)\\s("56+ Consts.UNKNOWN57+ "|runnable|sleeping|waiting\\son\\scondition|in\\sObject\\.wait\\(\\)|waiting\\sfor\\smonitor\\sentry)((.*))$";58}5960protected String methodInfoPattern() {61return "^\\s+at\\s(.+)\\((.*?)(\\:|\\))((.*?))\\)?$";62}6364protected String extendedStatusPattern() {65return "\\s+java\\.lang\\.Thread\\.State\\:\\s((.+))$";66}6768protected String jniGlobalRefInfoPattern() {69return "^JNI\\sglobal\\sreferences:\\s((.+))$";70}7172// Sample string that matches the pattern:73// waiting on <0x000000008f64e6d0> (a java.lang.Object)74protected String monitorInfoPattern() {75return "^\\s+\\-\\s(locked|waiting\\son|waiting\\sto\\slock)\\s\\<(.*)\\>\\s\\(((.*))\\)$";76}7778// Sample string that matches the pattern:79// waiting on <no object reference available>80protected String monitorInfoNoObjectRefPattern() {81return "^\\s+\\-\\s(locked|waiting\\son|waiting\\sto\\slock)\\s\\<(.*)\\>$";82}8384protected String vmVersionInfoPattern() {85return "Full\\sthread\\sdump\\s.*";86}8788protected String ownableSynchronizersPattern() {89return "^\\s+\\-\\s(\\<.*\\>\\s\\(((.*))\\)|None)$";90}9192public JStack parse(String stack) {93JStack result = new JStack();94Scanner scanner = new Scanner(stack);9596// parsing thread stacks97ThreadStack currentThreadStack = null;98MethodInfo currentMethodInfo = null;99100try {101while (scanner.hasNextLine()) {102String line = scanner.nextLine();103if (line.matches(threadInfoPattern())) {104currentThreadStack = parseThreadInfo(line);105result.addThreadStack(currentThreadStack.getThreadName(), currentThreadStack);106} else if (line.matches(methodInfoPattern())) {107currentMethodInfo = parseMethodInfo(line);108currentThreadStack.addMethod(currentMethodInfo);109} else if (line.matches(monitorInfoPattern())) {110MonitorInfo mi = parseMonitorInfo(line, monitorInfoPattern());111currentMethodInfo.getLocks().add(mi);112} else if (line.matches(monitorInfoNoObjectRefPattern())) {113MonitorInfo mi = parseMonitorInfo(line, monitorInfoNoObjectRefPattern());114currentMethodInfo.getLocks().add(mi);115} else if (line.matches(extendedStatusPattern())) {116currentThreadStack.setExtendedStatus(parseExtendedStatus(line));117} else if (line.matches(vmVersionInfoPattern())) {118result.setVmVersion(line);119} else if (line.matches(ownableSynchronizersPattern())) {120currentThreadStack.getLockOSList().add(parseLockInfo(line));121} else if (line.matches(jniGlobalRefInfoPattern())) {122result.setJniGlobalReferences(parseJNIGlobalRefs(line));123} else if (line.length() != 0) {124System.err.println("[Warning] Unknown string: " + line);125}126}127128scanner.close();129130} catch (NullPointerException e) {131e.printStackTrace();132throw new RuntimeException("Unexpected format in jstack output");133}134135return result;136}137138private MonitorInfo parseMonitorInfo(String line, String pattern) {139Scanner s = new Scanner(line);140s.findInLine(pattern);141MonitorInfo mi = new MonitorInfo();142MatchResult res = s.match();143144mi.setType(res.group(1));145mi.setMonitorAddress(res.group(2));146if (res.groupCount() > 2) {147mi.setMonitorClass(res.group(3));148}149return mi;150}151152protected String parseExtendedStatus(String line) {153Scanner s = new Scanner(line);154s.findInLine(extendedStatusPattern());155String result = s.match().group(1);156s.close();157return result;158}159160protected String parseJNIGlobalRefs(String line) {161Scanner s = new Scanner(line);162s.findInLine(jniGlobalRefInfoPattern());163String result = s.match().group(1);164s.close();165return result;166}167168protected ThreadStack parseThreadInfo(String threadInfo) {169Scanner s = new Scanner(threadInfo);170ThreadStack result = new ThreadStack();171172// parsing thread info173s.findInLine(threadInfoPattern());174MatchResult res = s.match();175176result.setThreadName(res.group(1));177178result.setType(res.group(3));179180result.setPriority(res.group(4));181result.setTid(res.group(7));182result.setNid(res.group(8));183result.setStatus(res.group(9));184185s.close();186return result;187}188189protected MethodInfo parseMethodInfo(String line) {190191MethodInfo result = new MethodInfo();192Scanner s = new Scanner(line);193194s.findInLine(methodInfoPattern());195MatchResult rexp = s.match();196if (rexp.group(4) != null && rexp.group(4).length() > 0) {197// line " at tmtools.jstack.share.utils.Utils.sleep(Utils.java:29)"198result.setName(rexp.group(1));199result.setCompilationUnit(rexp.group(2));200result.setLine(rexp.group(4));201202} else {203// line " at java.lang.Thread.sleep(Native Method)"204result.setName(rexp.group(1));205}206207s.close();208return result;209}210211public String dumpStackTraces() {212StringBuffer result = new StringBuffer();213Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();214215// adding data and vm version216result.append(Consts.UNKNOWN + "\n");217result.append(Consts.UNKNOWN + "\n\n");218219for (Thread t : stacks.keySet()) {220221result.append("\"" + t.getName() + "\"");222result.append(Consts.SEPARATOR);223224// status225if (t.isDaemon()) {226result.append("daemon");227result.append(Consts.SEPARATOR);228}229230// priority231result.append("prio=" + t.getPriority());232result.append(Consts.SEPARATOR);233234// tid235result.append("tid=" + Consts.UNKNOWN);236result.append(Consts.SEPARATOR);237238// nid239result.append("nid=" + Consts.UNKNOWN);240result.append(Consts.SEPARATOR);241242// status243result.append(Consts.UNKNOWN);244result.append(Consts.SEPARATOR);245246result.append("\n");247248// extended status249result.append(" java.lang.Thread.State: "250+ String.valueOf(Thread.currentThread().getState()));251result.append(Consts.SEPARATOR);252result.append("\n");253254for (StackTraceElement st : stacks.get(t)) {255result.append(" at " + st.toString() + "\n");256}257result.append("\n");258}259260result.append(Consts.JNI_GLOBAL_REF + Consts.UNKNOWN + "\n");261return result.toString();262}263264protected LockInfo parseLockInfo(String line) {265LockInfo res = new LockInfo();266267Scanner s = new Scanner(line);268s.findInLine(ownableSynchronizersPattern());269270MatchResult matchRes = s.match();271String lock = matchRes.group(1).equals("None") ? matchRes.group(1) : matchRes.group(2);272res.setLock(lock);273274return res;275}276277}278279280