00001 /* Copyright 1994, LongView Technologies L.L.C. $Revision: 1.13 $ */ 00002 /* Copyright (c) 2006, Sun Microsystems, Inc. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 00006 following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 00010 disclaimer in the documentation and/or other materials provided with the distribution. 00011 * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 00012 from this software without specific prior written permission. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00020 00021 00022 */ 00023 00024 // This file contains the interface to the vm pretty printer. 00025 // prettyPrintStream describes the output media. 00026 // prettyPrinter defined the interface for printing. 00027 00028 // WARNING WARNING WARNING WARNING WARNING WARNING 00029 // The implementation is NOT production quality due 00030 // to time constraints but since it is only intended 00031 // for vm debugging I hope we can live with it!!!!! 00032 // -- Lars 3/24-95 00033 00034 class prettyPrintStream : public PrintableResourceObj { 00035 protected: 00036 int indentation; 00037 int pos; 00038 bool in_hl; 00039 void set_highlight(bool value) { in_hl = value; } 00040 public: 00041 bool in_highlight() { return in_hl; } 00042 00043 // Creation 00044 prettyPrintStream() { 00045 indentation = pos = 0; 00046 set_highlight(false); 00047 } 00048 00049 // Indentation 00050 virtual void indent() = 0; 00051 void inc_indent() { indentation++; } 00052 void dec_indent() { indentation--; } 00053 int position () { return pos; } 00054 00055 // Printing 00056 virtual void print(char* str) = 0; 00057 virtual void print_char(char c) = 0; 00058 virtual void newline() = 0; 00059 void dec_newline() { dec_indent(); newline(); } 00060 void inc_newline() { inc_indent(); newline(); } 00061 virtual void space() = 0; 00062 virtual void begin_highlight() = 0; 00063 virtual void end_highlight() = 0; 00064 00065 // Layout 00066 virtual int width() = 0; 00067 virtual int remaining() = 0; 00068 virtual int width_of_string(char* str) = 0; 00069 virtual int width_of_char(char c) = 0; 00070 virtual int width_of_space() = 0; 00071 virtual int infinity() = 0; 00072 00073 // VM printing 00074 void print(); 00075 }; 00076 00077 // Default pretty-printer stream 00078 class defaultPrettyPrintStream : public prettyPrintStream { 00079 public: 00080 defaultPrettyPrintStream() : prettyPrintStream() {} 00081 void indent(); 00082 void print(char* str); 00083 void print_char(char c); 00084 int width_of_string(char* str); 00085 void space(); 00086 void newline(); 00087 void begin_highlight(); 00088 void end_highlight(); 00089 00090 int width() { return 100; } 00091 int remaining() { return width() - pos; } 00092 int width_of_char(char c) { return 1; } 00093 int width_of_space() { return 1; } 00094 int infinity() { return width(); } 00095 }; 00096 00097 class byteArrayPrettyPrintStream: public defaultPrettyPrintStream { 00098 private: 00099 GrowableArray<int>* _buffer; 00100 00101 public: 00102 byteArrayPrettyPrintStream(); 00103 void newline(); 00104 void print_char(char c); 00105 byteArrayOop asByteArray(); 00106 }; 00107 00108 // Pretty printing is done by first constructing a pseudo abstract syntax tree 00109 // based on the byte codes of the method and then printing the AST. 00110 // A simple ad-hoc strategy is used for splitting! 00111 00112 class prettyPrinter : AllStatic { 00113 public: 00114 // Pretty prints the method with the bci highlighted. 00115 // If output is NULL a default stream is used. 00116 00117 static void print(int index, 00118 deltaVFrame* fr, 00119 prettyPrintStream* output = NULL); 00120 00121 static void print_body( 00122 deltaVFrame* fr, 00123 prettyPrintStream* output = NULL); 00124 00125 static void print(methodOop method, 00126 klassOop klass = NULL, 00127 int bci = -1, 00128 prettyPrintStream* output = NULL); 00129 00130 // Pretty prints the method with the bci highlighted into a byteArray. 00131 static byteArrayOop print_in_byteArray(methodOop method, 00132 klassOop klass = NULL, 00133 int bci = -1); 00134 };