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