]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gprofng/src/MachineModel.cc
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
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)
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. */
26 #include "DbeSession.h"
28 #include "Application.h"
29 #include "MemorySpace.h"
34 static const char *LIBNAME
= "../lib/analyzer/lib/machinemodels";
37 DbeSession::find_mach_model (char *name
)
39 // Read current working directory to see if it's there
42 // Absolute path given
43 char *path
= dbe_sprintf (NTXT ("%s.ermm"), name
);
44 if (access (path
, R_OK
| F_OK
) == 0)
47 // Don't try anywhere else
51 char *path
= dbe_sprintf (NTXT ("./%s.ermm"), name
);
52 if (access (path
, R_OK
| F_OK
) == 0)
57 // Read the user's home directory to see if it's there
58 char *home
= getenv (NTXT ("HOME"));
61 path
= dbe_sprintf (NTXT ("%s/%s.ermm"), home
, name
);
62 if (access (path
, R_OK
| F_OK
) == 0)
66 if (strchr (name
, (int) '/') != NULL
)
67 // name has a slash; don't look in system installation directory
70 // Read system installation directory to see if it's there
71 path
= dbe_sprintf ("%s/%s/%s.ermm", theApplication
->get_run_dir (),
73 if (access (path
, R_OK
| F_OK
) == 0)
79 // Handle the definitions from a machinemodel file
80 // Return value is NULL if it was OK, or an error message if not
82 DbeSession::load_mach_model (char *_name
)
85 int arg_count
, cparam
;
87 char *arglist
[MAXARGS
];
92 // Does name have .ermm suffix? If so, strip it away
93 char *name
= dbe_strdup (_name
);
94 size_t len
= strlen (name
);
95 if (len
> 5 && strcmp (name
+ len
- 5, ".ermm") == 0)
98 if ((mach_model_loaded
!= NULL
) && (strcmp (name
, mach_model_loaded
) == 0))
100 ret
= dbe_sprintf (GTXT ("Machine model %s is already loaded\n"), name
);
104 else if (mach_model_loaded
== NULL
&& len
== 0)
106 ret
= dbe_sprintf (GTXT ("No Machine model is loaded\n"));
113 // zero-length just means unload any previously loaded model; only look if non-zero
114 path
= find_mach_model (name
);
117 ret
= dbe_sprintf (GTXT ("Machine model %s not found\n"), name
);
121 fptr
= fopen (path
, NTXT ("r"));
124 ret
= dbe_sprintf (GTXT ("Open of Machine model %s, file %s failed\n"), name
, path
);
131 // We are now committed to make the new machine model the loaded one;
132 // Delete any MemoryObjects from any previously loaded machinemodel
133 if (dbeSession
->mach_model_loaded
!= NULL
)
135 Vector
<char *> *oldobjs
= MemorySpace::getMachineModelMemObjs
136 (dbeSession
->mach_model_loaded
);
137 for (int i
= 0; i
< oldobjs
->size (); i
++)
138 MemorySpace::mobj_delete (oldobjs
->fetch (i
));
140 free (mach_model_loaded
);
144 mach_model_loaded
= NULL
;
146 // and there's no "loading" to do; just return
150 mach_model_loaded
= name
;
157 char *script
= read_line (fptr
);
162 strtok (script
, NTXT ("\n"));
164 // extract the command
165 cmd
= strtok (script
, NTXT (" \t"));
166 if (cmd
== NULL
|| *cmd
== '#' || *cmd
== '\n')
172 char *remainder
= strtok (NULL
, NTXT ("\n"));
173 // now extract the arguments
177 if (nargs
>= MAXARGS
)
179 ret
= dbe_sprintf (GTXT ("Warning: more than %d arguments to %s command, line %d\n"),
180 MAXARGS
, cmd
, line_no
);
184 char *nextarg
= strtok (remainder
, NTXT ("\n"));
186 if (nextarg
== NULL
|| *nextarg
== '#')
187 // either the end of the line, or a comment indicator
189 arglist
[nargs
++] = parse_qstring (nextarg
, &end_cmd
);
191 if (remainder
== NULL
)
194 // skip any blanks or tabs to get to next argument
195 while ((*remainder
== ' ') || (*remainder
== '\t'))
199 cmd_type
= Command::get_command (cmd
, arg_count
, cparam
);
200 // check for extra arguments
201 if (cmd_type
!= UNKNOWN_CMD
&& cmd_type
!= INDXOBJDEF
&& nargs
> arg_count
)
202 ret
= dbe_sprintf (GTXT ("Warning: extra arguments to %s command, line %d\n"),
204 if (nargs
< arg_count
)
206 ret
= dbe_sprintf (GTXT ("Error: missing arguments to %s command, line %d\n"),
209 // ignore this command
218 char *err
= dbeSession
->indxobj_define (arglist
[0], NULL
,
219 arglist
[1], (nargs
>= 3) ? PTXT (arglist
[2]) : NULL
,
220 (nargs
>= 4) ? PTXT (arglist
[3]) : NULL
);
222 ret
= dbe_sprintf (GTXT (" %s: line %d `%s %s %s'\n"),
223 err
, line_no
, cmd
, arglist
[0], arglist
[1]);
231 // unexpected command in a machinemodel file
232 ret
= dbe_sprintf (GTXT ("Unexpected command in machinemodel file %s on line %d: `%.64s'\n"),
244 DbeSession::list_mach_models ()
246 Vector
<char*> *model_names
= new Vector
<char*>();
248 // Open the current directory to read the entries there
249 DIR *dir
= opendir (NTXT ("."));
252 struct dirent
*entry
= NULL
;
253 while ((entry
= readdir (dir
)) != NULL
)
255 size_t len
= strlen (entry
->d_name
);
256 if (len
< 5 || strcmp (entry
->d_name
+ len
- 5, ".ermm") != 0)
258 char *model
= dbe_strdup (entry
->d_name
);
260 // remove the .ermm suffix
264 model_names
->append (dbe_strdup (model
));
269 // Read the user's home directory to list the models there
270 char *home
= getenv ("HOME");
273 dir
= opendir (home
);
276 struct dirent
*entry
= NULL
;
277 while ((entry
= readdir (dir
)) != NULL
)
279 size_t len
= strlen (entry
->d_name
);
280 if (len
< 5 || strcmp (entry
->d_name
+ len
- 5, ".ermm") != 0)
282 char *model
= dbe_strdup (entry
->d_name
);
284 // remove the .ermm suffix
288 model_names
->append (dbe_strdup (model
));
294 // Read system installation directory to list the models there
295 char *sysdir
= dbe_sprintf ("%s/%s", theApplication
->get_run_dir (), LIBNAME
);
297 dir
= opendir (sysdir
);
300 struct dirent
*entry
= NULL
;
301 while ((entry
= readdir (dir
)) != NULL
)
303 size_t len
= strlen (entry
->d_name
);
304 if (len
< 5 || strcmp (entry
->d_name
+ len
- 5, ".ermm") != 0)
306 char *model
= dbe_strdup (entry
->d_name
);
308 // remove the .ermm suffix
312 model_names
->append (dbe_strdup (model
));