summaryrefslogtreecommitdiff
path: root/gprofng/src/MachineModel.cc
blob: 15f493aeaf690c875f74d1ca7565b7dbb1fe7d37 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* 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 <string.h>
#include <unistd.h>
#include <dirent.h>

#include "DbeSession.h"
#include "Command.h"
#include "Application.h"
#include "MemorySpace.h"
#include "i18n.h"

#define MAXARGS     20

static const char *LIBNAME = "../lib/analyzer/lib/machinemodels";

char *
DbeSession::find_mach_model (char *name)
{
  // Read current working directory to see if it's there
  if (name[0] == '/')
    {
      // Absolute path given
      char *path = dbe_sprintf (NTXT ("%s.ermm"), name);
      if (access (path, R_OK | F_OK) == 0)
	return path;
      free (path);
      // Don't try anywhere else
      return NULL;
    }

  char *path = dbe_sprintf (NTXT ("./%s.ermm"), name);
  if (access (path, R_OK | F_OK) == 0)
    return path;
  free (path);


  // Read the user's home directory to see if it's there
  char *home = getenv (NTXT ("HOME"));
  if (home != NULL)
    {
      path = dbe_sprintf (NTXT ("%s/%s.ermm"), home, name);
      if (access (path, R_OK | F_OK) == 0)
	return path;
      free (path);
    }
  if (strchr (name, (int) '/') != NULL)
    // name has a slash; don't look in system installation directory
    return NULL;

  // Read system installation directory to see if it's there
  path = dbe_sprintf ("%s/%s/%s.ermm", theApplication->get_run_dir (),
		      LIBNAME, name);
  if (access (path, R_OK | F_OK) == 0)
    return path;
  free (path);
  return NULL;
}

//  Handle the definitions from a machinemodel file
//  Return value is NULL if it was OK, or an error message if not
char *
DbeSession::load_mach_model (char *_name)
{
  CmdType cmd_type;
  int arg_count, cparam;
  char *cmd, *end_cmd;
  char *arglist[MAXARGS];
  char *ret = NULL;
  char *path = NULL;
  FILE *fptr = NULL;

  // Does name have .ermm suffix?  If so, strip it away
  char *name = dbe_strdup (_name);
  size_t len = strlen (name);
  if (len > 5 && strcmp (name + len - 5, ".ermm") == 0)
    name[len - 5] = 0;

  if ((mach_model_loaded != NULL) && (strcmp (name, mach_model_loaded) == 0))
    {
      ret = dbe_sprintf (GTXT ("Machine model %s is already loaded\n"), name);
      free (name);
      return ret;
    }
  else if (mach_model_loaded == NULL && len == 0)
    {
      ret = dbe_sprintf (GTXT ("No Machine model is loaded\n"));
      free (name);
      return ret;
    }

  if (len != 0)
    {
      // zero-length just means unload any previously loaded model; only look if non-zero
      path = find_mach_model (name);
      if (path == NULL)
	{
	  ret = dbe_sprintf (GTXT ("Machine model %s not found\n"), name);
	  free (name);
	  return ret;
	}
      fptr = fopen (path, NTXT ("r"));
      if (fptr == NULL)
	{
	  ret = dbe_sprintf (GTXT ("Open of Machine model %s, file %s failed\n"), name, path);
	  free (path);
	  free (name);
	  return ret;
	}
    }

  // We are now committed to make the new machine model the loaded one;
  //   Delete any MemoryObjects from any previously loaded machinemodel
  if (dbeSession->mach_model_loaded != NULL)
    {
      Vector <char *> *oldobjs = MemorySpace::getMachineModelMemObjs
	      (dbeSession->mach_model_loaded);
      for (int i = 0; i < oldobjs->size (); i++)
	MemorySpace::mobj_delete (oldobjs->fetch (i));
      delete oldobjs;
      free (mach_model_loaded);
    }
  if (len == 0)
    {
      mach_model_loaded = NULL;
      free (name);
      // and there's no "loading" to do; just return
      return NULL;
    }
  else
    mach_model_loaded = name;

  int line_no = 0;
  end_cmd = NULL;

  while (!feof (fptr))
    {
      char *script = read_line (fptr);
      if (script == NULL)
	continue;

      line_no++;
      strtok (script, NTXT ("\n"));

      // extract the command
      cmd = strtok (script, NTXT (" \t"));
      if (cmd == NULL || *cmd == '#' || *cmd == '\n')
	{
	  free (script);
	  continue;
	}

      char *remainder = strtok (NULL, NTXT ("\n"));
      // now extract the arguments
      int nargs = 0;
      for (;;)
	{
	  if (nargs >= MAXARGS)
	    {
	      ret = dbe_sprintf (GTXT ("Warning: more than %d arguments to %s command, line %d\n"),
				 MAXARGS, cmd, line_no);
	      continue;
	    }

	  char *nextarg = strtok (remainder, NTXT ("\n"));

	  if (nextarg == NULL || *nextarg == '#')
	      // either the end of the line, or a comment indicator
	      break;
	  arglist[nargs++] = parse_qstring (nextarg, &end_cmd);
	  remainder = end_cmd;
	  if (remainder == NULL)
	    break;

	  // skip any blanks or tabs to get to next argument
	  while ((*remainder == ' ') || (*remainder == '\t'))
	    remainder++;
	}

      cmd_type = Command::get_command (cmd, arg_count, cparam);
      // check for extra arguments
      if (cmd_type != UNKNOWN_CMD && cmd_type != INDXOBJDEF && nargs > arg_count)
	ret = dbe_sprintf (GTXT ("Warning: extra arguments to %s command, line %d\n"),
			   cmd, line_no);
      if (nargs < arg_count)
	{
	  ret = dbe_sprintf (GTXT ("Error: missing arguments to %s command, line %d\n"),
			     cmd, line_no);

	  // ignore this command
	  free (script);
	  continue;
	}

      switch (cmd_type)
	{
	case INDXOBJDEF:
	  {
	    char *err = dbeSession->indxobj_define (arglist[0], NULL,
			    arglist[1], (nargs >= 3) ? PTXT (arglist[2]) : NULL,
			    (nargs >= 4) ? PTXT (arglist[3]) : NULL);
	    if (err != NULL)
	      ret = dbe_sprintf (GTXT ("   %s: line %d `%s %s %s'\n"),
				 err, line_no, cmd, arglist[0], arglist[1]);
	    break;
	  }
	case COMMENT:
	  // ignore the line
	  break;
	default:
	  {
	    // unexpected command in a machinemodel file
	    ret = dbe_sprintf (GTXT ("Unexpected command in machinemodel file %s on line %d: `%.64s'\n"),
			       path, line_no, cmd);
	    break;
	  }
	}
      free (script);
    }
  fclose (fptr);
  return ret;
}

Vector<char*> *
DbeSession::list_mach_models ()
{
  Vector<char*> *model_names = new Vector<char*>();

  // Open the current directory to read the entries there
  DIR *dir = opendir (NTXT ("."));
  if (dir != NULL)
    {
      struct dirent *entry = NULL;
      while ((entry = readdir (dir)) != NULL)
	{
	  size_t len = strlen (entry->d_name);
	  if (len < 5 || strcmp (entry->d_name + len - 5, ".ermm") != 0)
	    continue;
	  char *model = dbe_strdup (entry->d_name);

	  // remove the .ermm suffix
	  model[len - 5] = 0;

	  // add to vector
	  model_names->append (dbe_strdup (model));
	}
      closedir (dir);
    }

  // Read the user's home directory to list the models there
  char *home = getenv ("HOME");
  if (home != NULL)
    {
      dir = opendir (home);
      if (dir != NULL)
	{
	  struct dirent *entry = NULL;
	  while ((entry = readdir (dir)) != NULL)
	    {
	      size_t len = strlen (entry->d_name);
	      if (len < 5 || strcmp (entry->d_name + len - 5, ".ermm") != 0)
		continue;
	      char *model = dbe_strdup (entry->d_name);

	      // remove the .ermm suffix
	      model[len - 5] = 0;

	      // add to vector
	      model_names->append (dbe_strdup (model));
	    }
	  closedir (dir);
	}
    }

  // Read system installation directory to list the models there
  char *sysdir = dbe_sprintf ("%s/%s", theApplication->get_run_dir (), LIBNAME);

  dir = opendir (sysdir);
  if (dir != NULL)
    {
      struct dirent *entry = NULL;
      while ((entry = readdir (dir)) != NULL)
	{
	  size_t len = strlen (entry->d_name);
	  if (len < 5 || strcmp (entry->d_name + len - 5, ".ermm") != 0)
	    continue;
	  char *model = dbe_strdup (entry->d_name);

	  // remove the .ermm suffix
	  model[len - 5] = 0;

	  // add to vector
	  model_names->append (dbe_strdup (model));
	}
      closedir (dir);
    }
  return model_names;
}