arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / gprofng / src / checks.cc
blobf6d9b0e08ca35f3d130387c54e1ed1bae02aa8a5
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 <ctype.h>
23 #include <unistd.h>
24 #include <sys/utsname.h>
25 #include <sys/param.h>
27 #include "gp-defs.h"
28 #include "Elf.h"
29 #include "collctrl.h"
30 #include "i18n.h"
31 #include "util.h"
32 #include "collect.h"
34 void
35 collect::check_target (int argc, char **argv)
37 char *next;
38 char *last = 0;
39 char *a;
40 char *ccret;
41 char **lasts = &last;
42 int tindex = targ_index;
43 int ret;
44 char *basename;
45 is_64 = false;
47 /* now check the executable */
48 nargs = argc - targ_index;
49 Exec_status rv = check_executable (argv[targ_index]);
50 switch (rv)
52 case EXEC_OK:
53 njargs = cc->get_java_arg_cnt ();
54 arglist = (char **) xcalloc (nargs + 5 + njargs, sizeof (char *));
55 jargs = cc->get_java_args ();
57 // store the first argument -- target name
58 ret = 0;
59 arglist[ret++] = argv[tindex++];
60 if (cc->get_java_mode () == 1)
62 // add any user-specified -J (Java) arguments
63 int length = (int) strlen (argv[targ_index]);
64 int is_java = 0;
65 if ((length >= 6) && strcmp (&argv[targ_index][length - 5], NTXT ("/java")) == 0)
66 is_java = 1;
67 else if ((length == 4) && strcmp (&argv[targ_index][0], NTXT ("java")) == 0)
68 is_java = 1;
69 if (njargs != 0 && is_java)
71 next = strtok_r (jargs, NTXT (" \t"), lasts);
72 arglist[ret++] = next;
73 for (;;)
75 next = strtok_r (NULL, NTXT (" \t"), lasts);
76 if (next == NULL)
77 break;
78 arglist[ret++] = next;
83 // copy the rest of the arguments
84 for (int i = 1; i < nargs; i++)
85 arglist[ret++] = argv[tindex++];
86 nargs = ret;
87 break;
88 case EXEC_IS_JAR:
89 // Preface the user-supplied argument list with
90 // the path to the java, the collector invocation,
91 // any -J java arguments provided, and "-jar".
92 ccret = cc->set_java_mode (NTXT ("on"));
93 if (ccret != NULL)
95 writeStr (2, ccret);
96 exit (1);
98 njargs = cc->get_java_arg_cnt ();
99 arglist = (char **) xcalloc (nargs + 5 + njargs, sizeof (char *));
100 jargs = cc->get_java_args ();
102 a = find_java ();
103 if (a == NULL)
104 exit (1); // message was written
105 ret = 0;
106 arglist[ret++] = a;
107 // add any user-specified Java arguments
108 if (njargs != 0)
110 next = strtok_r (jargs, NTXT (" \t"), lasts);
111 arglist[ret++] = next;
112 for (;;)
114 next = strtok_r (NULL, NTXT (" \t"), lasts);
115 if (next == NULL)
116 break;
117 arglist[ret++] = next;
120 arglist[ret++] = NTXT ("-jar");
121 for (int i = 0; i < nargs; i++)
122 arglist[ret++] = argv[tindex++];
123 nargs = ret;
124 break;
125 case EXEC_IS_CLASSCLASS:
126 // remove the .class from the name
127 ret = (int) strlen (argv[targ_index]);
128 argv[targ_index][ret - 6] = 0;
130 // now fall through to the EXEC_IS_CLASS case
131 case EXEC_IS_CLASS:
132 // Preface the user-supplied argument list with
133 // the path to the java, the collector invocation,
134 // and any -J java arguments provided.
135 ccret = cc->set_java_mode (NTXT ("on"));
136 if (ccret != NULL)
138 writeStr (2, ccret);
139 exit (1);
141 jargs = cc->get_java_args ();
142 njargs = cc->get_java_arg_cnt ();
143 arglist = (char **) xcalloc (nargs + 4 + njargs, sizeof (char *));
145 a = find_java ();
146 if (a == NULL)
147 exit (1); // message was written
148 ret = 0;
149 arglist[ret++] = a;
150 // add any user-specified Java arguments
151 if (njargs != 0)
153 next = strtok_r (jargs, NTXT (" \t"), lasts);
154 arglist[ret++] = next;
155 for (;;)
157 next = strtok_r (NULL, NTXT (" \t"), lasts);
158 if (next == NULL)
159 break;
160 arglist[ret++] = next;
164 // copy the remaining arguments to the new list
165 for (int i = 0; i < nargs; i++)
166 arglist[ret++] = argv[tindex++];
167 nargs = ret;
168 break;
169 case EXEC_ELF_NOSHARE:
170 case EXEC_OPEN_FAIL:
171 case EXEC_ELF_LIB:
172 case EXEC_ELF_HEADER:
173 case EXEC_ELF_ARCH:
174 case EXEC_ISDIR:
175 case EXEC_NOT_EXEC:
176 case EXEC_NOT_FOUND:
177 default:
178 /* something wrong; write a message */
179 char *errstr = status_str (rv, argv[targ_index]);
180 if (errstr)
182 dbe_write (2, "%s", errstr);
183 free (errstr);
185 exit (1);
187 cc->set_target (arglist[0]);
189 /* check the experiment */
190 char *ccwarn;
191 ccret = cc->check_expt (&ccwarn);
192 if (ccwarn != NULL)
194 writeStr (2, ccwarn);
195 free (ccwarn);
197 if (ccret != NULL)
199 writeStr (2, ccret);
200 exit (1);
202 /* check if java, to see if -j flag was given */
203 if ((basename = strrchr (arglist[0], '/')) == NULL)
204 basename = arglist[0];
205 else
206 basename++;
207 if (strcmp (basename, NTXT ("java")) == 0)
209 /* the target's name is java; was java flag set? */
210 if ((jseen_global == 0) && (cc->get_java_mode () == 0))
212 char *cret = cc->set_java_mode (NTXT ("on"));
213 if (cret != NULL)
215 writeStr (2, cret);
216 exit (1);
222 collect::Exec_status
223 collect::check_executable (char *target_name)
225 char target_path[MAXPATHLEN];
226 dbe_stat_t statbuf;
227 if (target_name == NULL) // not set, but assume caller knows what it's doing
228 return EXEC_OK;
229 if (getenv ("GPROFNG_SKIP_VALIDATION")) // don't check target
230 return EXEC_OK;
232 // see if target exists and is not a directory
233 if ((dbe_stat (target_name, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFDIR))
235 // target is found, check for access as executable
236 if (access (target_name, X_OK) != 0)
238 // not an executable, check for jar or class file
239 int i = (int) strlen (target_name);
240 if ((i >= 5) && strcmp (&target_name[i - 4], NTXT (".jar")) == 0)
242 // could be a jar file
243 // XXXX -- need better check for real jar file
244 cc->set_java_mode ("on");
245 return EXEC_IS_JAR;
247 if ((i >= 7) && strcmp (&target_name[i - 6], NTXT (".class")) == 0)
249 // could be a class file
250 // XXXX -- need better check for real class file
251 cc->set_java_mode (NTXT ("on"));
252 return EXEC_IS_CLASSCLASS;
254 // not a jar or class file, return not an executable
255 return EXEC_NOT_EXEC;
257 else // found, and it is executable. set the path to it
258 snprintf (target_path, sizeof (target_path), NTXT ("%s"), target_name);
260 else
262 // not found, look on path
263 char *exe_name = get_realpath (target_name);
264 if (access (exe_name, X_OK) != 0)
266 // target can't be located
267 // one last attempt: append .class to name, and see if we can find it
268 snprintf (target_path, sizeof (target_path), NTXT ("%s.class"), target_name);
269 if (dbe_stat (target_path, &statbuf) == 0)
271 // the file exists
272 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
274 // this is a directory; that won't do.
275 return EXEC_ISDIR;
277 // say it's a class file
278 cc->set_java_mode (NTXT ("on"));
279 return EXEC_IS_CLASS;
281 return EXEC_NOT_FOUND;
283 snprintf (target_path, sizeof (target_path), NTXT ("%s"), exe_name);
284 delete exe_name;
287 // target_path is now the purported executable
288 // check for ELF library out of date
289 if (Elf::elf_version (EV_CURRENT) == EV_NONE)
290 return EXEC_ELF_LIB;
291 Elf *elf = Elf::elf_begin (target_path);
292 if (elf == NULL)
293 return EXEC_OK;
294 // do not by pass checking architectural match
295 collect::Exec_status exec_stat = check_executable_arch (elf);
296 delete elf;
297 return exec_stat;
300 collect::Exec_status
301 collect::check_executable_arch (Elf *elf)
303 Elf_Internal_Ehdr *ehdrp = elf->elf_getehdr ();
304 if (ehdrp == NULL)
305 return EXEC_ELF_HEADER;
306 unsigned short machine = ehdrp->e_machine;
308 switch (machine)
310 #if ARCH(SPARC)
311 case EM_SPARC:
312 case EM_SPARC32PLUS:
313 break;
314 case EM_SPARCV9:
315 is_64 = true;
316 break;
317 #elif ARCH(Intel)
318 case EM_X86_64:
320 is_64 = true;
321 // now figure out if the platform can run it
322 struct utsname unbuf;
323 int r = uname (&unbuf);
324 if (r == 0 && strstr (unbuf.machine, "_64") == NULL)
325 // machine can not run 64 bits, but this code is 64-bit
326 return EXEC_ELF_ARCH;
328 break;
329 case EM_386:
330 break;
331 #elif ARCH(Aarch64)
332 case EM_AARCH64:
333 is_64 = true;
334 break;
335 #elif ARCH(RISCV)
336 case EM_RISCV:
337 is_64 = true;
338 break;
339 #endif
340 default:
341 return EXEC_ELF_ARCH;
344 // now check if target was built with shared libraries
345 int dynamic = 0;
346 for (unsigned cnt = 0; cnt < ehdrp->e_phnum; cnt++)
348 Elf_Internal_Phdr *phdrp = elf->get_phdr (cnt);
349 if (phdrp && phdrp->p_type == PT_DYNAMIC)
351 dynamic = 1;
352 break;
355 if (dynamic == 0)
357 // target is not a dynamic executable or shared object;
358 // can't record data
359 return EXEC_ELF_NOSHARE;
361 return EXEC_OK;
364 char *
365 collect::status_str (Exec_status rv, char *target_name)
367 switch (rv)
369 case EXEC_OK:
370 case EXEC_IS_JAR:
371 case EXEC_IS_CLASS:
372 case EXEC_IS_CLASSCLASS:
373 // supported flavors -- no error message
374 return NULL;
375 case EXEC_ELF_NOSHARE:
376 return dbe_sprintf (GTXT ("Target executable `%s' must be built with shared libraries\n"), target_name);
377 case EXEC_OPEN_FAIL:
378 return dbe_sprintf (GTXT ("Can't open target executable `%s'\n"), target_name);
379 case EXEC_ELF_LIB:
380 return xstrdup (GTXT ("Internal error: Not a working version of ELF library\n"));
381 case EXEC_ELF_HEADER:
382 return dbe_sprintf (GTXT ("Target `%s' is not a valid ELF executable\n"), target_name);
383 case EXEC_ELF_ARCH:
384 return dbe_sprintf (GTXT ("Target architecture of executable `%s' is not supported on this machine\n"), target_name);
385 case EXEC_ISDIR:
386 return dbe_sprintf (GTXT ("Target `%s' is a directory, not an executable\n"), target_name);
387 case EXEC_NOT_EXEC:
388 return dbe_sprintf (GTXT ("Target `%s' is not executable\n"), target_name);
389 case EXEC_NOT_FOUND:
390 return dbe_sprintf (GTXT ("Target `%s' not found\n"), target_name);
392 return NULL;
395 char *
396 collect::find_java (void)
398 char buf[MAXPATHLEN];
399 char *var = NULL;
400 Exec_status rv = EXEC_OK;
402 // first see if the user entered a -j argument
403 var = cc->get_java_path ();
404 if (var != NULL)
406 snprintf (buf, sizeof (buf), NTXT ("%s/bin/java"), var);
407 java_how = NTXT ("-j");
408 rv = check_executable (buf);
410 // then try JDK_HOME
411 if (java_how == NULL)
413 var = getenv (NTXT ("JDK_HOME"));
414 if ((var != NULL) && (strlen (var) > 0))
416 snprintf (buf, sizeof (buf), NTXT ("%s/bin/java"), var);
417 java_how = NTXT ("JDK_HOME");
418 rv = check_executable (buf);
421 // then try JAVA_PATH
422 if (java_how == NULL)
424 var = getenv (NTXT ("JAVA_PATH"));
425 if ((var != NULL) && (strlen (var) > 0))
427 snprintf (buf, sizeof (buf), NTXT ("%s/bin/java"), var);
428 java_how = NTXT ("JAVA_PATH");
429 rv = check_executable (buf);
432 // try the user's path
433 if (java_how == NULL)
435 snprintf (buf, sizeof (buf), NTXT ("java"));
436 rv = check_executable (buf);
437 if (rv == EXEC_OK)
438 java_how = NTXT ("PATH");
440 // finally, just try /usr/java -- system default
441 if (java_how == NULL)
443 snprintf (buf, sizeof (buf), NTXT ("/usr/java/bin/java"));
444 rv = check_executable (buf);
445 java_how = NTXT ("/usr/java/bin/java");
448 // we now have a nominal path to java, and how we chose it
449 // and we have rv set to the check_executable return
450 switch (rv)
452 case EXEC_OK:
453 java_path = xstrdup (buf);
454 if (verbose == 1)
455 dbe_write (2, GTXT ("Path to `%s' (set from %s) used for Java profiling\n"),
456 java_path, java_how);
457 return xstrdup (buf);
458 default:
459 dbe_write (2, GTXT ("Path to `%s' (set from %s) does not point to a JVM executable\n"),
460 buf, java_how);
461 break;
463 return NULL;
466 void
467 collect::validate_config (int how)
469 if (getenv (NTXT ("GPROFNG_SKIP_VALIDATION")) != NULL)
470 return;
471 char *cmd = dbe_sprintf (NTXT ("%s/perftools_validate"), run_dir);
472 if (access (cmd, X_OK) != 0)
474 if (how)
475 dbe_write (2, GTXT ("WARNING: Unable to validate system: `%s' could not be executed\n"), cmd);
476 return;
478 char *quiet = how == 0 ? NTXT ("") : NTXT ("-q"); // check collection, verbosely
479 char *buf;
480 if (cc->get_java_default () == 0 && java_path)
481 buf = dbe_sprintf (NTXT ("%s -c -j %s -H \"%s\" %s"), cmd, java_path, java_how, quiet);
482 else // not java mode -- don't check the java version
483 buf = dbe_sprintf (NTXT ("%s -c %s"), cmd, quiet);
484 free (cmd);
486 /* now run the command */
487 int ret = system (buf);
488 int status = WEXITSTATUS (ret);
489 if ((status & 0x1) != 0)
490 dbe_write (2, GTXT ("WARNING: Data collection may fail: system is not properly configured or is unsupported.\n"));
491 if ((status & 0x2) != 0)
492 dbe_write (2, GTXT ("WARNING: Java data collection may fail: J2SE[tm] version is unsupported.\n"));
493 free (buf);
496 void
497 collect::validate_java (const char *jvm, const char *jhow, int q)
499 char *cmd = dbe_sprintf (NTXT ("%s/perftools_ckjava"), run_dir);
500 if (access (cmd, X_OK) != 0)
502 dbe_write (2, GTXT ("WARNING: Unable to validate Java: `%s' could not be executed\n"), cmd);
503 return;
505 char *buf = dbe_sprintf (NTXT ("%s -j %s -H \"%s\" %s"), cmd, jvm, jhow,
506 (q == 1 ? "-q" : ""));
507 free (cmd);
509 /* now run the command */
510 int ret = system (buf);
511 int status = WEXITSTATUS (ret);
512 if (status != 0)
513 dbe_write (2, GTXT ("WARNING: Java data collection may fail: J2SE[tm] version is unsupported.\n"));
514 free (buf);