summaryrefslogtreecommitdiff
path: root/gprofng/src/gprofng.cc
blob: 1bf567915653efc79e90bbabe4cbf1c431af34b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Copyright (C) 2021 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>

#include "Application.h"
#include "i18n.h"
#include "util.h"

static int verbose = 0;

class Gprofng : Application
{
public:
  Gprofng (int _argc, char *_argv[]);
  ~Gprofng ();
  void start();
  void usage();

private:
  void exec_cmd(char *tool_name, int argc, char **argv);
  int argc;
  char **argv;
};

int
main (int argc, char *argv[])
{
  Gprofng *gprofng = new Gprofng (argc, argv);
  gprofng->start();
  delete gprofng;
  return 0;
}

Gprofng::Gprofng (int _argc, char *_argv[]) : Application(_argc, _argv, NULL)
{
  argc = _argc;
  argv = _argv;
}

Gprofng::~Gprofng () { }

void
Gprofng::usage ()
{
  /*
   * Isolate the first line because it has an argument.
   * Otherwise it would be at the end of this long list.
   */
  printf ( GTXT (
    "Usage: %s [OPTION(S)] COMMAND [KEYWORD] [ARGUMENTS]\n"), whoami);

  printf ( GTXT (
    "\n"
    "This is the driver for the GPROFNG tools suite to gather and analyze performance data.\n"
    "\n"
    "Options:\n"
    "\n"
    " --version           print the version number and exit.\n"
    " --help              print usage information and exit.\n"
    " --check             verify if the hardware and software environment is supported.\n"
    " --verbose {on|off}  enable (on) or disable (off) verbose mode; the default is \"off\".\n"
    "\n"
    "Commands:\n"
    "\n"
    "The driver supports various commands. These are listed below.\n"
    "\n"
    "It is also possible to invoke the lower level commands directly, but since these \n"
    "are subject to change, in particular the options, we recommend to use the driver.\n"
    "\n"
    "The man pages for the commands below can be viewed using the command name with\n"
    "\"gprofng\" replaced by \"gp\" and the spaces replaced by a dash (\"-\"). For\n"
    "example the man page name for \"gprofng collect app\" is \"gp-collect-app\".\n"
    "\n"
    "The following combination of commands and keywords are supported:\n"
    "\n"
    "Collect performance data\n"
    "\n"
    " gprofng collect app     collect application performance data.\n"
    "\n"
    "Display the performance results\n"
    "\n"
    " gprofng display text    display the performance data in ASCII format.\n"
    " gprofng display html    generate an HTML file from one or more experiments.\n"
/*
    " gprofng display gui     invoke the GUI to graphically analyze the results.\n"
*/
    " gprofng display src     display source or disassembly with compiler annotations.\n"
    "\n"
    "Miscellaneous commands\n"
    "\n"
    " gprofng archive         include binaries and source code in an experiment directory.\n"
    "\n"
    "Environment:\n"
    "\n"
    "The following environment variables are supported:\n"
    "\n"
    " GPROFNG_MAX_CALL_STACK_DEPTH  set the depth of the call stack (default is 256).\n"
    "\n"
    " GPROFNG_USE_JAVA_OPTIONS      may be set when profiling a C/C++ application\n"
    "                               that uses dlopen() to execute Java code.\n"
    "\n"
    " GPROFNG_SSH_REMOTE_DISPLAY    use this variable to define the ssh command\n"
    "                               executed by the remote display tool.\n"
    "\n"
    " GPROFNG_SKIP_VALIDATION       set this variable to disable checking hardware,\n"
    "                               system, and Java versions.\n"
    "\n"
    " GPROFNG_ALLOW_CORE_DUMP       set this variable to allow a core file to be\n"
    "                               generated; otherwise an error report is created on /tmp.\n"
    "\n"
    " GPROFNG_ARCHIVE               use this variable to define the settings for automatic\n"
    "                               archiving upon experiment recording completion.\n"
    "\n"
    " GPROFNG_ARCHIVE_COMMON_DIR    set this variable to the location of the common archive.\n"
    "\n"
    " GPROFNG_JAVA_MAX_CALL_STACK_DEPTH  set the depth of the Java call stack; the default\n"
    "                                    is 256; set to 0 to disable capturing of call stacks.\n"
    "\n"
    " GPROFNG_JAVA_NATIVE_MAX_CALL_STACK_DEPTH  set the depth of the Java native call stack;\n"
    "                                           the default is 256; set to 0 to disable capturing\n"
    "                                           of call stacks (JNI and assembly call stacks\n"
    "                                           are not captured).\n"
    "\n"
    "Documentation:\n"
    "\n"
    "A getting started guide for gprofng is maintained as a Texinfo manual. If the info and\n"
    "gprofng programs are properly installed at your site, the command \"info gprofng\"\n"
    "should give you access to this document.\n"
    "\n"
    "See also:\n"
    "\n"
    "gp-archive(1), gp-collect-app(1), gp-display-html(1), gp-display-src(1), gp-display-text(1)\n"));

/*
  printf ( GTXT (
    "Usage: %s [--verbose] [--version] [--help] <tool-name> [<keyword>] <args>\n"
    "\n%s\n"
    "   archive         Archive binaries and sources\n"
    "   collect [app]   Collect performance data\n"
    "   display [text]  Print an ASCII report\n"
    "   display gui     Graphical tool for analyzing an experiment\n"
    "   display html    Generate an HTML file from an experiment\n"
    "   display src     Print source or dissasembly\n"),
    whoami, getenv ("_BUILDING_MANPAGE")
	   ? "*Available subcommands*"
	   : "Available Subcommands");
*/
}

void
Gprofng::exec_cmd (char *tool_name, int argc, char **argv)
{
  static const struct
  {
    const char *tool_name;
    const char *keyword;
    const char *app_name;
  } app_names [] = {
    { "archive", NULL, "gp-archive"},
    { "collect", "app", "gp-collect-app"},
    { "collect", "kernel", "gp-collect-kernel"},
    { "display", "text", "gp-display-text"},
    { "display", "gui", "gp-display-gui"},
    { "display", "html", "gp-display-html"},
    { "display", "src", "gp-display-src"},
    { NULL, NULL}
  };

  const char *keyword = argc > 1 ? argv[1] : "";
  int first = -1;
  int find_tool_name = -1;
  for (int i = 0; app_names[i].tool_name; i++)
    if (!strcmp (tool_name, app_names[i].tool_name))
      {
	if (app_names[i].keyword == NULL)
	  {
	    first = i;
	    break;
	  }
	if (!strcmp (keyword, app_names[i].keyword))
	  {
	    first = i;
	    argc--;
	    argv++;
	    break;
	  }
	if (find_tool_name == -1)
	  find_tool_name = i;
      }

  if (first == -1)
    {
      if (find_tool_name == -1)
	fprintf (stderr, GTXT ("%s: error: keyword '%s' is not supported\n"),
		 get_basename (get_name ()), tool_name);
      else if (*keyword == 0)
	fprintf (stderr, GTXT ("%s %s: error: no qualifier\n"),
		 get_basename (get_name ()), tool_name);
      else
	fprintf (stderr, GTXT ("%s %s: error: qualifier '%s' is not supported\n"),
		 get_basename (get_name ()), tool_name, keyword);
      exit (1);
    }

  const char *aname = app_names[first].app_name;;

  char **arr = (char **) malloc ((argc + 3) * sizeof (char *));
  int n = 0;
  char *pname = get_name ();
  arr[n++] = dbe_sprintf ("%.*s%s", (int) (get_basename (pname) - pname),
			    pname, aname);
  if (app_names[first].keyword)
    arr[n++] = dbe_sprintf ("--whoami=%s %s %s", whoami, tool_name,
			    app_names[first].keyword);
  else
    arr[n++] = dbe_sprintf ("--whoami=%s %s", whoami, tool_name);
  for (int i = 1; i < argc; i++)
    arr[n++] = argv[i];
  arr[n] = NULL;
  if (verbose)
    {
      printf ("gprofng::exec\n");
      for (int i = 0; arr[i]; i++)
	printf ("%5d: %s\n", i, arr[i]);
      printf("\n");
    }
  execv (arr[0], arr);

  // If execv returns, it must have failed.
  fprintf (stderr, GTXT ("%s failed: %s\n"), arr[0], STR (strerror (errno)));
  exit(1);
}

void
Gprofng::start ()
{
  if (argc == 1)
    {
      usage ();
      exit (0);
    }
  for (int i = 1; i < argc; i++)
    {
      char *s = argv[i];
      if (*s != '-')
	{
	  exec_cmd(s, argc - i, argv + i);
	  return;
	}
      else if (!strcmp (s, "--help"))
	{
	  usage ();
	  exit (0);
	}
      else if (!strcmp (s, "--version") || !strcmp (s, "-v"))
	{
	   Application::print_version_info ();
	   exit (0);
	}
      else if (!strcmp (s, "--verbose"))
	verbose = 1;
      else if (!strcmp (s, "--check"))
	{
	  fprintf (stderr, GTXT ("%s: error: --check is not implemented yet\n"),
		   get_basename (get_name ()));
	  exit (1);
	}
      else
	{
	  fprintf (stderr, GTXT ("%s: error: unknown option %s\n"),
		   get_basename (get_name ()), s);
	  exit(1);
	}
    }
  fprintf (stderr, GTXT ("%s: error: expected argument after options\n"),
	   get_basename (get_name ()));
}