/// <summary> /// Creates threads dump and try to mimic JVM stack trace as much as possible to allow existing analytics tools to be used /// </summary> /// <param name="threadMxBean"> bean to use for thread dump </param> /// <param name="systemProperties"> dumped vm system properties </param> /// <returns> string that contains thread dump </returns> public static string ThreadDump(ThreadMXBean threadMxBean, Properties systemProperties) { ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(true, true); // Reproduce JVM stack trace as far as possible to allow existing analytics tools to be used string vmName = systemProperties.getProperty("java.vm.name"); string vmVersion = systemProperties.getProperty("java.vm.version"); string vmInfoString = systemProperties.getProperty("java.vm.info"); StringBuilder sb = new StringBuilder(); sb.Append(string.Format("Full thread dump {0} ({1} {2}):\n\n", vmName, vmVersion, vmInfoString)); foreach (ThreadInfo threadInfo in threadInfos) { sb.Append(string.Format("\"{0}\" #{1:D}\n", threadInfo.ThreadName, threadInfo.ThreadId)); sb.Append(" java.lang.Thread.State: ").Append(threadInfo.ThreadState).Append("\n"); StackTraceElement[] stackTrace = threadInfo.StackTrace; for (int i = 0; i < stackTrace.Length; i++) { StackTraceElement e = stackTrace[i]; sb.Append("\tat ").Append(e.ToString()).Append('\n'); // First stack element info can be found in the thread state if (i == 0 && threadInfo.LockInfo != null) { Thread.State ts = threadInfo.ThreadState; switch (ts) { case BLOCKED: sb.Append("\t- blocked on ").Append(threadInfo.LockInfo).Append('\n'); break; case WAITING: sb.Append("\t- waiting on ").Append(threadInfo.LockInfo).Append('\n'); break; case TIMED_WAITING: sb.Append("\t- waiting on ").Append(threadInfo.LockInfo).Append('\n'); break; default: break; } } foreach (MonitorInfo mi in threadInfo.LockedMonitors) { if (mi.LockedStackDepth == i) { sb.Append("\t- locked ").Append(mi).Append('\n'); } } } } return(sb.ToString()); }
} // MIssue /// <summary> /// Log Record Constructor /// </summary> /// <param name="record">record</param> public MIssue(LogRecord record) : this(Env.GetCtx(), 0, null) { String summary = record.message; SetSourceClassName(record.sourceClassName); SetSourceMethodName(record.sourceMethodName); SetLoggerName(record.GetLoggerName()); //Throwable t = record.getThrown(); //if (t != null) //{ //if (summary != null && summary.length() > 0) //summary = t.toString() + " " + summary; //if (summary == null || summary.length() == 0) //summary = t.toString(); // String e = record.message.ToString();// = record.message.ToString();// (Object)record.message; StringBuilder error = new StringBuilder(); StackTraceElement[] tes = null;// =(StackTraceElement[])e.StackTrace;// t.getStackTrace(); int count = 0; for (int i = 0; i < e.Length; i++) { StackTraceElement element = tes[i]; String s = element.ToString(); if (s.IndexOf("vienna") != -1) { error.Append(s).Append("\n"); if (count == 0) { String source = element.GetClassName() + "." + element.GetMethodName(); SetSourceClassName(source); SetLineNo(element.GetLineNumber()); } count++; } if (count > 5 || error.Length > 2000) { break; } } SetErrorTrace(error.ToString()); // Stack //CharArrayWriter cWriter = new CharArrayWriter(); //PrintWriter pWriter = new PrintWriter(cWriter); //t.printStackTrace(pWriter); //setStackTrace(cWriter.toString()); //if (summary == null || summary.length() == 0) //summary = "??"; //setIssueSummary(summary); SetRecord_ID(1); } // MIssue
private static void PrintThreadInfo(ThreadInfo ti, PrintWriter @out) { // print thread information PrintThread(ti, @out); // print stack trace with locks StackTraceElement[] stacktrace = ti.GetStackTrace(); MonitorInfo[] monitors = ti.GetLockedMonitors(); for (int i = 0; i < stacktrace.Length; i++) { StackTraceElement ste = stacktrace[i]; @out.WriteLine(Indent + "at " + ste.ToString()); foreach (MonitorInfo mi in monitors) { if (mi.GetLockedStackDepth() == i) { @out.WriteLine(Indent + " - locked " + mi); } } } @out.WriteLine(); }
/// <summary> /// <inheritDoc/> /// /// </summary> public override IList <Redwood.Record> Handle(Redwood.Record record) { StringBuilder b = new StringBuilder(1024); //--Special case for Exceptions string[] content; if (record.content is Exception) { //(vars) IList <string> lines = new List <string>(); //(root message) Exception exception = (Exception)record.content; lines.Add(record.content.ToString()); StackTraceElement[] trace = exception.GetStackTrace(); StackTraceElement topTraceElement = trace.Length > 0 ? trace[0] : null; foreach (StackTraceElement e in exception.GetStackTrace()) { lines.Add(tab + e.ToString()); } //(causes) while (exception.InnerException != null) { System.Console.Out.WriteLine("TOP ELEMENT: " + topTraceElement); //((variables)) exception = exception.InnerException; trace = exception.GetStackTrace(); lines.Add("Caused by: " + exception.GetType() + ": " + exception.Message); for (int i = 0; i < trace.Length; i++) { //((add trace element)) StackTraceElement e_1 = trace[i]; lines.Add(tab + e_1.ToString()); //((don't print redundant elements)) if (topTraceElement != null && e_1.GetClassName().Equals(topTraceElement.GetClassName()) && e_1.GetMethodName().Equals(topTraceElement.GetMethodName())) { lines.Add(tab + "..." + (trace.Length - i - 1) + " more"); break; } } //((update top element)) topTraceElement = trace.Length > 0 ? trace[0] : null; } //(set content array) content = new string[lines.Count]; content = Sharpen.Collections.ToArray(lines, content); } else { if (record.content == null) { content = new string[] { "null" }; } else { string toStr; if (record.content is ISupplier) { //noinspection unchecked toStr = ((ISupplier <object>)record.content).Get().ToString(); } else { toStr = record.content.ToString(); } if (toStr == null) { content = new string[] { "<null toString()>" }; } else { content = toStr.Split("\n"); } } } //would be nice to get rid of this 'split()' call at some point //--Handle Tracks UpdateTracks(record.depth); if (this.missingOpenBracket) { this.Style(b, "{\n", trackColor, trackStyle); this.missingOpenBracket = false; } //--Process Record //(variables) int cursorPos = 0; int contentLinesPrinted = 0; //(loop) Color color = Color.None; Edu.Stanford.Nlp.Util.Logging.Style style = Edu.Stanford.Nlp.Util.Logging.Style.None; //(get channels) List <object> printableChannels = new List <object>(); foreach (object chan in record.Channels()) { if (chan is Color) { color = (Color)chan; } else { if (chan is Edu.Stanford.Nlp.Util.Logging.Style) { style = (Edu.Stanford.Nlp.Util.Logging.Style)chan; } else { if (chan != Redwood.Force) { printableChannels.Add(chan); } } } } //--Write Channels if (leftMargin > 2) { //don't print if not enough space //((print channels) b.Append("["); cursorPos += 1; object lastChan = null; bool wasAnyChannelPrinted = false; for (int i = 0; i < printableChannels.Count; i++) { object chan_1 = printableChannels[i]; if (chan_1.Equals(lastChan)) { continue; } //skip duplicate channels lastChan = chan_1; //(get channel) string toPrint = chan_1.ToString(); if (toPrint.Length > leftMargin - 1) { toPrint = Sharpen.Runtime.Substring(toPrint, 0, leftMargin - 2); } if (cursorPos + toPrint.Length >= leftMargin) { //(case: doesn't fit) while (cursorPos < leftMargin) { b.Append(" "); cursorPos += 1; } if (contentLinesPrinted < content.Length) { WriteContent(record.depth, Style(new StringBuilder(), content[contentLinesPrinted], color, style).ToString(), b); contentLinesPrinted += 1; } b.Append("\n "); cursorPos = 1; } //(print flag) bool wasChannelPrinted = FormatChannel(b, toPrint, chan_1); wasAnyChannelPrinted = wasAnyChannelPrinted || wasChannelPrinted; if (wasChannelPrinted && i < printableChannels.Count - 1) { b.Append(channelSeparatorChar); cursorPos += 1; } cursorPos += toPrint.Length; } if (wasAnyChannelPrinted) { b.Append("]"); cursorPos += 1; } else { b.Length = b.Length - 1; // remove leading "[" cursorPos -= 1; } } //--Content //(write content) while (contentLinesPrinted < content.Length) { while (cursorPos < leftMargin) { b.Append(" "); cursorPos += 1; } WriteContent(record.depth, Style(new StringBuilder(), content[contentLinesPrinted], color, style).ToString(), b); contentLinesPrinted += 1; if (contentLinesPrinted < content.Length) { b.Append("\n"); cursorPos = 0; } } //(print) if (b.Length == 0 || b[b.Length - 1] != '\n') { b.Append("\n"); } Print(record.Channels(), b.ToString()); //--Continue if (info != null) { info.numElementsPrinted += 1; } List <Redwood.Record> rtn = new List <Redwood.Record>(); rtn.Add(record); return(rtn); }
public void ToStringOverride() { var e = new StackTraceElement("className", "methodName", "fileName", 42); Assert.That(e.ToString(), Is.EqualTo("at className.methodName(...) in fileName:42")); }
/// <summary> /// Returns a string representation of this thread info. /// The format of this string depends on the implementation. /// The returned string will typically include /// the <seealso cref="#getThreadName thread name"/>, /// the <seealso cref="#getThreadId thread ID"/>, /// its <seealso cref="#getThreadState state"/>, /// and a <seealso cref="#getStackTrace stack trace"/> if any. /// </summary> /// <returns> a string representation of this thread info. </returns> public override String ToString() { StringBuilder sb = new StringBuilder("\"" + ThreadName + "\"" + " Id=" + ThreadId + " " + ThreadState); if (LockName != null) { sb.Append(" on " + LockName); } if (LockOwnerName != null) { sb.Append(" owned by \"" + LockOwnerName + "\" Id=" + LockOwnerId); } if (Suspended) { sb.Append(" (suspended)"); } if (InNative) { sb.Append(" (in native)"); } sb.Append('\n'); int i = 0; for (; i < StackTrace_Renamed.Length && i < MAX_FRAMES; i++) { StackTraceElement ste = StackTrace_Renamed[i]; sb.Append("\tat " + ste.ToString()); sb.Append('\n'); if (i == 0 && LockInfo != null) { Thread.State ts = ThreadState; switch (ts.InnerEnumValue()) { case Thread.State.InnerEnum.BLOCKED: sb.Append("\t- blocked on " + LockInfo); sb.Append('\n'); break; case Thread.State.InnerEnum.WAITING: sb.Append("\t- waiting on " + LockInfo); sb.Append('\n'); break; case Thread.State.InnerEnum.TIMED_WAITING: sb.Append("\t- waiting on " + LockInfo); sb.Append('\n'); break; default: break; } } foreach (MonitorInfo mi in LockedMonitors_Renamed) { if (mi.LockedStackDepth == i) { sb.Append("\t- locked " + mi); sb.Append('\n'); } } } if (i < StackTrace_Renamed.Length) { sb.Append("\t..."); sb.Append('\n'); } LockInfo[] locks = LockedSynchronizers; if (locks.Length > 0) { sb.Append("\n\tNumber of locked synchronizers = " + locks.Length); sb.Append('\n'); foreach (LockInfo li in locks) { sb.Append("\t- " + li); sb.Append('\n'); } } sb.Append('\n'); return(sb.ToString()); }