1 /* Copyright (C) 2021 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #ifndef _PRINT_H
22 #define _PRINT_H
23 
24 
25 // Include files
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "dbe_types.h"
29 #include "Metric.h"
30 #include "Hist_data.h"
31 #include "Ovw_data.h"
32 #include "Stats_data.h"
33 #include "Emsg.h"
34 #include "Exp_Layout.h"
35 #include "DefaultMap.h"
36 #include "FileData.h"
37 #include "HeapData.h"
38 #include "HashMap.h"
39 
40 const char nl[] = "\n";
41 const char tab[] = "\t";
42 
43 // Printing options.
44 enum Print_destination
45 {
46   DEST_PRINTER      = 0,
47   DEST_FILE         = 1,
48   DEST_OPEN_FILE    = 2
49 };
50 
51 enum Print_mode
52 {
53   MODE_LIST,
54   MODE_DETAIL,
55   MODE_GPROF,
56   MODE_ANNOTATED
57 };
58 
59 struct Print_params
60 {
61   Print_destination dest;   // printer or file
62   char *name;               // of printer or file
63   int ncopies;              // # of copies
64   bool header;              // print header first
65   FILE *openfile;           // if destination is DEST_OPEN_FILE
66 };
67 
68 class Experiment;
69 class MetricList;
70 class DbeView;
71 class Stack_coverage;
72 class Function;
73 class LoadObject;
74 
75 //  Class Definitions
76 class er_print_common_display
77 {
78 public:
er_print_common_display()79   er_print_common_display ()
80   {
81     out_file = NULL;
82     pr_params.header = false;
83   }
84 
~er_print_common_display()85   virtual ~er_print_common_display () { }
86 
87   //  Open the file/printer to write to
88   int open (Print_params *);
89 
90   void
set_out_file(FILE * o)91   set_out_file (FILE *o)
92   {
93     out_file = o;
94     pr_params.dest = DEST_FILE;
95   }
96 
97   //  Print the final output data.  This function calls
98   //  data_dump() to actually do the dumping of data.
99   bool print_output ();
100 
101   //  Print the output in the appropriate format.
102   virtual void data_dump () = 0;
103 
104   void header_dump (int exp_idx);
105 
106   // Return the report. If the report size is greater than max, return truncated report
107   // Allocates memory, so the caller should free this memory.
108   char *get_output (int max);
109 
110 protected:
111   DbeView *dbev;
112   FILE *out_file;
113   Print_params pr_params;
114   char *tmp_file;
115   int exp_idx1, exp_idx2;
116   bool load;
117   bool header;
118 };
119 
120 class er_print_histogram : public er_print_common_display
121 {
122 public:
123   er_print_histogram (DbeView *dbv, Hist_data *data, MetricList *metrics_list,
124 		      Print_mode disp_type, int limit, char *sort_name,
125 		      Histable *sobj, bool show_load, bool show_header);
126   void data_dump ();
127 
128 private:
129   void dump_list (int limit);
130   void dump_detail (int limit);
131   void get_gprof_width (Metric::HistMetric *hist_metric, int limit);
132   void dump_gprof (int limit);
133   void dump_annotated_dataobjects (Vector<int> *marks, int threshold);
134   void dump_annotated ();
135 
136   Stack_coverage *stack_cov;
137   Hist_data *hist_data;
138   MetricList *mlist;
139   Print_mode type;
140   int number_entries;
141   char *sort_metric;
142   Histable *sel_obj;
143 };
144 
145 class er_print_ctree : public er_print_common_display
146 {
147 public:
148   er_print_ctree (DbeView *dbv, Vector<Histable*> *cstack, Histable *sobj,
149 		  int limit);
150   void data_dump ();
151   void print_children (Hist_data *data, int index, Histable *obj, char *prefix,
152 		       Hist_data::HistItem *total);
153 
154 private:
155   Vector<Histable*> *cstack;
156   Histable *sobj;
157   MetricList *mlist;
158   Metric::HistMetric *hist_metric;
159   int limit;
160   int print_row;
161 };
162 
163 class er_print_gprof : public er_print_common_display
164 {
165 public:
166   er_print_gprof (DbeView *dbv, Vector<Histable*> *cstack);
167   void data_dump ();
168 private:
169   Vector<Histable*> *cstack;
170 };
171 
172 class er_print_leaklist : public er_print_common_display
173 {
174 public:
175   er_print_leaklist (DbeView *dbv, bool show_leak,
176 		     bool show_alloca, int limit);
177   void data_dump ();
178 
179 private:
180   bool leak;
181   bool alloca;
182   int limit;
183 };
184 
185 class er_print_heapactivity : public er_print_common_display
186 {
187 public:
188   er_print_heapactivity (DbeView *_dbev, Histable::Type _type,
189 			 bool _printStat, int _limit);
190   void data_dump ();
191 
192 private:
193   void printStatistics (Hist_data *hist_data);
194   void printCallStacks (Hist_data *hist_data);
195 
196   Histable::Type type;
197   bool printStat;
198   int limit;
199 };
200 
201 class er_print_ioactivity : public er_print_common_display
202 {
203 public:
204   er_print_ioactivity (DbeView *_dbev, Histable::Type _type,
205 		       bool _printStat, int _limit);
206   void data_dump ();
207 
208 private:
209   void printStatistics (Hist_data *hist_data);
210   void printCallStacks (Hist_data *hist_data);
211 
212   Histable::Type type;
213   bool printStat;
214   int limit;
215 };
216 
217 class er_print_experiment : public er_print_common_display
218 {
219 public:
220   er_print_experiment (DbeView *me, int bgn_idx, int end_idx, bool show_load,
221 	   bool show_header, bool show_stat, bool show_over, bool show_odetail);
222   void data_dump ();
223 
224 private:
225   int max_len1, max_len2, max_len3;
226   void overview_sum (int &maxlen);
227   void overview_dump (int exp_idx, int &maxlen);
228   void overview_summary (Ovw_data *ovw_data, int &maxlen);
229   void overview_item (Ovw_data::Ovw_item *ovw_item,
230 		      Ovw_data::Ovw_item *ovw_item_labels);
231   void overview_value (Value *value, ValueTag value_tag, double total_value);
232   void statistics_sum (int &maxlen);
233   void statistics_dump (int exp_idx, int &maxlen);
234   void statistics_item (Stats_data *stats_data);
235 
236   bool stat;
237   bool over;
238   bool odetail;
239 };
240 
241 //  Print the header.  Experiment name and the sample
242 //  selection, along with the percentage.
243 char *pr_load_objects (Vector<LoadObject*> *loadobjects, char *lead);
244 char *pr_samples (Experiment *exp);
245 char *pr_mesgs (Emsg *msg, const char *null_str, const char *lead);
246 void print_load_object (FILE *out_file);
247 void print_header (Experiment *exp, FILE *out_file);
248 
249 // Print Function metrics
250 int print_label (FILE *out_file, MetricList *metrics_list,
251 		 Metric::HistMetric *hist_metric, int space);
252 void print_anno_file (char *name, const char *sel, const char *srcFile,
253 		      bool isDisasm, FILE *dis_file, FILE *inp_file,
254 		      FILE *out_file, DbeView *dbev, bool xdefault);
255 void print_html_title (FILE *out_file, char *title);
256 void print_html_label (FILE *out_file, MetricList *metrics_list);
257 void print_html_content (FILE *out_file, Hist_data *d, MetricList *metrics_list,
258 			 int limit, Histable::NameFormat nfmt);
259 void print_html_one (FILE *out_file, Hist_data *data, Hist_data::HistItem *item,
260 		     MetricList *metrics_list, Histable::NameFormat nfmt);
261 void print_html_trailer (FILE* out_file);
262 char *html_ize_name (char *name);
263 void print_delim_label (FILE *out_file, MetricList *metrics_list, char delim);
264 void print_delim_content (FILE *out_file, Hist_data *data,
265 			  MetricList *metrics_list, int limit,
266 			  Histable::NameFormat nfmt, char delim);
267 void print_delim_one (FILE *out_file, Hist_data *data, Hist_data::HistItem *item,
268 	       MetricList *metrics_list, Histable::NameFormat nfmt, char delim);
269 void print_delim_trailer (FILE* out_file, char delim);
270 char *csv_ize_name (char *name, char delim);
271 char *split_metric_name (char *name);
272 
273 #endif
274