1 // Throwable.java - Superclass for all exceptions.
3 /* Copyright (C) 1998, 1999 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java.io.PrintStream;
13 import java.io.PrintWriter;
14 import java.io.Serializable;
15 import java.io.OutputStreamWriter;
16 import java.io.OutputStream;
17 import java.io.FilterOutputStream;
18 import java.io.IOException;
21 * @author Tom Tromey <tromey@cygnus.com>
22 * @date October 30, 1998
25 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
26 * "The Java Language Specification", ISBN 0-201-63451-1
27 * Status: Sufficient for compiled code, but methods applicable to
28 * bytecode not implemented. JDK 1.1.
31 /* A CPlusPlusDemangler sits on top of a PrintWriter. All input is
32 * passed through the "c++filt" program (part of GNU binutils) which
33 * demangles internal symbols to their C++ source form.
35 * Closing a CPlusPlusDemangler doesn't close the underlying
36 * PrintWriter; it does, however close underlying process and flush
37 * all its buffers, so it's possible to guarantee that after a
38 * CPlusPlusDemangler has been closed no more will ever be written to
39 * the underlying PrintWriter.
41 * FIXME: This implictly converts data from the input stream, which is
42 * a stream of characters, to a stream of bytes. We need a way of
43 * handling Unicode characters in demangled identifiers. */
45 class CPlusPlusDemangler extends OutputStream
47 java.io.OutputStream procOut;
48 java.io.InputStream procIn;
49 java.lang.Process proc;
52 /* The number of bytes written to the underlying PrintWriter. This
53 provides a crude but fairly portable way to determine whether or
54 not the attempt to exec c++filt worked. */
55 public int written = 0;
57 CPlusPlusDemangler (PrintWriter writer) throws IOException
60 proc = Runtime.getRuntime ().exec ("c++filt");
61 procOut = proc.getOutputStream ();
62 procIn = proc.getInputStream ();
65 public void write (int b) throws IOException
68 while (procIn.available () != 0)
70 int c = procIn.read ();
81 public void close () throws IOException
85 while ((c = procIn.read ()) != -1)
95 catch (InterruptedException _)
101 public class Throwable implements Serializable
103 public native Throwable fillInStackTrace ();
105 public String getLocalizedMessage ()
107 return getMessage ();
110 public String getMessage ()
112 return detailMessage;
115 public void printStackTrace ()
117 printStackTrace (System.err);
120 public void printStackTrace (PrintStream ps)
122 PrintWriter writer = new PrintWriter (ps);
123 printStackTrace (writer);
126 public void printStackTrace (PrintWriter wr)
130 CPlusPlusDemangler cPlusPlusFilter = new CPlusPlusDemangler (wr);
131 PrintWriter writer = new PrintWriter (cPlusPlusFilter);
132 printRawStackTrace (writer);
134 if (cPlusPlusFilter.written == 0) // The demangler has failed...
135 printRawStackTrace (wr);
139 printRawStackTrace (wr);
145 detailMessage = null;
149 public Throwable (String message)
151 detailMessage = message;
155 public String toString ()
157 return ((detailMessage == null)
158 ? getClass().getName()
159 : getClass().getName() + ": " + getMessage ());
162 private native final void printRawStackTrace (PrintWriter wr);
164 // Name of this field comes from serialization spec.
165 private String detailMessage;
167 // Setting this flag to false prevents fillInStackTrace() from running.
168 static boolean trace_enabled = true;
169 private transient byte stackTrace[];
170 private static final long serialVersionUID = -3042686055658047285L;