1 /* Copyright (C) 2021 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. */
28 #include <sys/types.h>
34 #include <StringBuilder.h>
37 /* get_count_data -- format exec of bit to do the real work */
39 collect::get_count_data ()
45 // reserve space for original args, plus 30 arguments to bit
46 nargs
= origargc
+ 30;
47 char **narglist
= (char **) calloc (nargs
, sizeof (char *));
50 // construct the command for bit
51 snprintf (command
, sizeof (command
), NTXT ("%s"), run_dir
);
52 s
= strstr_r (command
, NTXT ("/bin"));
55 // build command line for launching it
56 snprintf (s
, sizeof (command
) - (s
- command
), NTXT ("/lib/compilers/bit"));
57 if (stat (command
, &statbuf
) == -1)
59 // if bit command does not exist there
60 char *first_look
= strdup (command
);
61 snprintf (command
, sizeof (command
), NTXT ("%s"), run_dir
);
62 s
= strstr (command
, NTXT ("/bin"));
63 snprintf (s
, sizeof (command
) - (s
- command
), NTXT ("/prod/bin/bit"));
64 if (stat (command
, &statbuf
) == -1)
66 // if bit command does not exist
67 dbe_write (2, GTXT ("bit is not installed as `%s' or `%s'\nNo experiment is possible\n"), first_look
, command
);
72 *arglist
++ = strdup (command
);
76 dbe_write (2, GTXT ("collect can't find install bin directory\n"));
80 // Tell it to collect data
81 *arglist
++ = NTXT ("collect");
83 // add the flag for real-data vs. static data
84 switch (cc
->get_count ())
87 *arglist
++ = NTXT ("-i");
88 *arglist
++ = NTXT ("static");
89 *arglist
++ = NTXT ("-M");
92 *arglist
++ = NTXT ("-M");
93 *arglist
++ = NTXT ("-u");
99 // tell bit to produce an experiment
100 *arglist
++ = NTXT ("-e");
102 // now copy an edited list of collect options to the arglist
103 char **oargv
= origargv
;
105 // skip the "collect"
108 while (argc
!= targ_index
)
113 // pass these arguments along, with parameter
121 *arglist
++ = *oargv
++;
122 *arglist
++ = *oargv
++;
126 *arglist
++ = *oargv
++; // set the -I flag
127 *arglist
++ = *oargv
; // and the directory name
128 *arglist
++ = NTXT ("-d"); // and the -d flag
129 *arglist
++ = *oargv
++; // to the same directory name
134 // pass these arguments along as is
135 *arglist
++ = *oargv
++;
159 // skip two arguments
167 // these should never get this far
168 dbe_write (2, GTXT ("unexpected argument %s\n"), p
);
173 // now copy the target and its arguments
174 if (access (prog_name
, X_OK
) != 0) // not found
175 *arglist
++ = *oargv
++;
179 *arglist
++ = prog_name
;
181 while (*oargv
!= NULL
)
182 *arglist
++ = *oargv
++;
184 /* now we have the full argument list composed; if verbose, print it */
185 if ((verbose
== 1) || (disabled
))
187 /* describe the experiment */
188 char *ccret
= cc
->show (0);
194 ccret
= cc
->show_expt ();
197 /* write this to stdout */
201 /* print the arguments to bit */
204 sb
.append (NTXT ("Exec argv[] = "));
205 for (int ret
= 0; ret
< nargs
; ret
++)
207 if (narglist
[ret
] == NULL
)
210 sb
.append (NTXT (" "));
211 sb
.append (narglist
[ret
]);
213 sb
.append (NTXT ("\n\n"));
214 write (2, sb
.toString (), sb
.length ());
217 /* check for dry run */
221 /* ensure original outputs restored for target */
224 /* now exec the bit to instrument and run the target ... */
225 // (void) execve( *narglist, narglist, origenvp);
226 (void) execvp (*narglist
, narglist
);
228 /* exec failed; no experiment to delete */
229 /* restore output for collector */
231 char *em
= strerror (errno
);
233 dbe_write (2, GTXT ("execve of %s failed: errno = %d\n"), narglist
[0], errno
);
235 dbe_write (2, GTXT ("execve of %s failed: %s\n"), narglist
[0], em
);