]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gprofng/src/gprofng.cc
gprofng: a new GNU profiler
[thirdparty/binutils-gdb.git] / gprofng / src / gprofng.cc
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 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <getopt.h>
28
29 #include "Application.h"
30 #include "i18n.h"
31 #include "util.h"
32
33 static int verbose = 0;
34
35 class Gprofng : Application
36 {
37 public:
38 Gprofng (int _argc, char *_argv[]);
39 ~Gprofng ();
40 void start();
41 void usage();
42
43 private:
44 void exec_cmd(char *tool_name, int argc, char **argv);
45 int argc;
46 char **argv;
47 };
48
49 int
50 main (int argc, char *argv[])
51 {
52 Gprofng *gprofng = new Gprofng (argc, argv);
53 gprofng->start();
54 delete gprofng;
55 return 0;
56 }
57
58 Gprofng::Gprofng (int _argc, char *_argv[]) : Application(_argc, _argv, NULL)
59 {
60 argc = _argc;
61 argv = _argv;
62 }
63
64 Gprofng::~Gprofng () { }
65
66 void
67 Gprofng::usage ()
68 {
69 /*
70 * Isolate the first line because it has an argument.
71 * Otherwise it would be at the end of this long list.
72 */
73 printf ( GTXT (
74 "Usage: %s [OPTION(S)] COMMAND [KEYWORD] [ARGUMENTS]\n"), whoami);
75
76 printf ( GTXT (
77 "\n"
78 "This is the driver for the GPROFNG tools suite to gather and analyze performance data.\n"
79 "\n"
80 "Options:\n"
81 "\n"
82 " --version print the version number and exit.\n"
83 " --help print usage information and exit.\n"
84 " --check verify if the hardware and software environment is supported.\n"
85 " --verbose {on|off} enable (on) or disable (off) verbose mode; the default is \"off\".\n"
86 "\n"
87 "Commands:\n"
88 "\n"
89 "The driver supports various commands. These are listed below.\n"
90 "\n"
91 "It is also possible to invoke the lower level commands directly, but since these \n"
92 "are subject to change, in particular the options, we recommend to use the driver.\n"
93 "\n"
94 "The man pages for the commands below can be viewed using the command name with\n"
95 "\"gprofng\" replaced by \"gp\" and the spaces replaced by a dash (\"-\"). For\n"
96 "example the man page name for \"gprofng collect app\" is \"gp-collect-app\".\n"
97 "\n"
98 "The following combination of commands and keywords are supported:\n"
99 "\n"
100 "Collect performance data\n"
101 "\n"
102 " gprofng collect app collect application performance data.\n"
103 "\n"
104 "Display the performance results\n"
105 "\n"
106 " gprofng display text display the performance data in ASCII format.\n"
107 " gprofng display html generate an HTML file from one or more experiments.\n"
108 /*
109 " gprofng display gui invoke the GUI to graphically analyze the results.\n"
110 */
111 " gprofng display src display source or disassembly with compiler annotations.\n"
112 "\n"
113 "Miscellaneous commands\n"
114 "\n"
115 " gprofng archive include binaries and source code in an experiment directory.\n"
116 "\n"
117 "Environment:\n"
118 "\n"
119 "The following environment variables are supported:\n"
120 "\n"
121 " GPROFNG_MAX_CALL_STACK_DEPTH set the depth of the call stack (default is 256).\n"
122 "\n"
123 " GPROFNG_USE_JAVA_OPTIONS may be set when profiling a C/C++ application\n"
124 " that uses dlopen() to execute Java code.\n"
125 "\n"
126 " GPROFNG_SSH_REMOTE_DISPLAY use this variable to define the ssh command\n"
127 " executed by the remote display tool.\n"
128 "\n"
129 " GPROFNG_SKIP_VALIDATION set this variable to disable checking hardware,\n"
130 " system, and Java versions.\n"
131 "\n"
132 " GPROFNG_ALLOW_CORE_DUMP set this variable to allow a core file to be\n"
133 " generated; otherwise an error report is created on /tmp.\n"
134 "\n"
135 " GPROFNG_ARCHIVE use this variable to define the settings for automatic\n"
136 " archiving upon experiment recording completion.\n"
137 "\n"
138 " GPROFNG_ARCHIVE_COMMON_DIR set this variable to the location of the common archive.\n"
139 "\n"
140 " GPROFNG_JAVA_MAX_CALL_STACK_DEPTH set the depth of the Java call stack; the default\n"
141 " is 256; set to 0 to disable capturing of call stacks.\n"
142 "\n"
143 " GPROFNG_JAVA_NATIVE_MAX_CALL_STACK_DEPTH set the depth of the Java native call stack;\n"
144 " the default is 256; set to 0 to disable capturing\n"
145 " of call stacks (JNI and assembly call stacks\n"
146 " are not captured).\n"
147 "\n"
148 "Documentation:\n"
149 "\n"
150 "A getting started guide for gprofng is maintained as a Texinfo manual. If the info and\n"
151 "gprofng programs are properly installed at your site, the command \"info gprofng\"\n"
152 "should give you access to this document.\n"
153 "\n"
154 "See also:\n"
155 "\n"
156 "gp-archive(1), gp-collect-app(1), gp-display-html(1), gp-display-src(1), gp-display-text(1)\n"));
157
158 /*
159 printf ( GTXT (
160 "Usage: %s [--verbose] [--version] [--help] <tool-name> [<keyword>] <args>\n"
161 "\n%s\n"
162 " archive Archive binaries and sources\n"
163 " collect [app] Collect performance data\n"
164 " display [text] Print an ASCII report\n"
165 " display gui Graphical tool for analyzing an experiment\n"
166 " display html Generate an HTML file from an experiment\n"
167 " display src Print source or dissasembly\n"),
168 whoami, getenv ("_BUILDING_MANPAGE")
169 ? "*Available subcommands*"
170 : "Available Subcommands");
171 */
172 }
173
174 void
175 Gprofng::exec_cmd (char *tool_name, int argc, char **argv)
176 {
177 static const struct
178 {
179 const char *tool_name;
180 const char *keyword;
181 const char *app_name;
182 } app_names [] = {
183 { "archive", NULL, "gp-archive"},
184 { "collect", "app", "gp-collect-app"},
185 { "collect", "kernel", "gp-collect-kernel"},
186 { "display", "text", "gp-display-text"},
187 { "display", "gui", "gp-display-gui"},
188 { "display", "html", "gp-display-html"},
189 { "display", "src", "gp-display-src"},
190 { NULL, NULL}
191 };
192
193 const char *keyword = argc > 1 ? argv[1] : "";
194 int first = -1;
195 int find_tool_name = -1;
196 for (int i = 0; app_names[i].tool_name; i++)
197 if (!strcmp (tool_name, app_names[i].tool_name))
198 {
199 if (app_names[i].keyword == NULL)
200 {
201 first = i;
202 break;
203 }
204 if (!strcmp (keyword, app_names[i].keyword))
205 {
206 first = i;
207 argc--;
208 argv++;
209 break;
210 }
211 if (find_tool_name == -1)
212 find_tool_name = i;
213 }
214
215 if (first == -1)
216 {
217 if (find_tool_name == -1)
218 fprintf (stderr, GTXT ("%s: error: keyword '%s' is not supported\n"),
219 get_basename (get_name ()), tool_name);
220 else if (*keyword == 0)
221 fprintf (stderr, GTXT ("%s %s: error: no qualifier\n"),
222 get_basename (get_name ()), tool_name);
223 else
224 fprintf (stderr, GTXT ("%s %s: error: qualifier '%s' is not supported\n"),
225 get_basename (get_name ()), tool_name, keyword);
226 exit (1);
227 }
228
229 const char *aname = app_names[first].app_name;;
230
231 char **arr = (char **) malloc ((argc + 3) * sizeof (char *));
232 int n = 0;
233 char *pname = get_name ();
234 arr[n++] = dbe_sprintf ("%.*s%s", (int) (get_basename (pname) - pname),
235 pname, aname);
236 if (app_names[first].keyword)
237 arr[n++] = dbe_sprintf ("--whoami=%s %s %s", whoami, tool_name,
238 app_names[first].keyword);
239 else
240 arr[n++] = dbe_sprintf ("--whoami=%s %s", whoami, tool_name);
241 for (int i = 1; i < argc; i++)
242 arr[n++] = argv[i];
243 arr[n] = NULL;
244 if (verbose)
245 {
246 printf ("gprofng::exec\n");
247 for (int i = 0; arr[i]; i++)
248 printf ("%5d: %s\n", i, arr[i]);
249 printf("\n");
250 }
251 execv (arr[0], arr);
252
253 // If execv returns, it must have failed.
254 fprintf (stderr, GTXT ("%s failed: %s\n"), arr[0], STR (strerror (errno)));
255 exit(1);
256 }
257
258 void
259 Gprofng::start ()
260 {
261 if (argc == 1)
262 {
263 usage ();
264 exit (0);
265 }
266 for (int i = 1; i < argc; i++)
267 {
268 char *s = argv[i];
269 if (*s != '-')
270 {
271 exec_cmd(s, argc - i, argv + i);
272 return;
273 }
274 else if (!strcmp (s, "--help"))
275 {
276 usage ();
277 exit (0);
278 }
279 else if (!strcmp (s, "--version") || !strcmp (s, "-v"))
280 {
281 Application::print_version_info ();
282 exit (0);
283 }
284 else if (!strcmp (s, "--verbose"))
285 verbose = 1;
286 else if (!strcmp (s, "--check"))
287 {
288 fprintf (stderr, GTXT ("%s: error: --check is not implemented yet\n"),
289 get_basename (get_name ()));
290 exit (1);
291 }
292 else
293 {
294 fprintf (stderr, GTXT ("%s: error: unknown option %s\n"),
295 get_basename (get_name ()), s);
296 exit(1);
297 }
298 }
299 fprintf (stderr, GTXT ("%s: error: expected argument after options\n"),
300 get_basename (get_name ()));
301 }