2003-10-28 18:56:51 +00:00
|
|
|
/*===-- CommonProfiling.c - Profiling support library support -------------===*\
|
|
|
|
|*
|
|
|
|
|* The LLVM Compiler Infrastructure
|
|
|
|
|*
|
|
|
|
|* This file was developed by the LLVM research group and is distributed under
|
|
|
|
|* the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
|*
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|*
|
|
|
|
|* This file implements functions used by the various different types of
|
|
|
|
|* profiling implementations.
|
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "Profiling.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2003-10-28 22:45:25 +00:00
|
|
|
#include <stdlib.h>
|
2003-10-28 18:56:51 +00:00
|
|
|
|
2003-10-28 22:45:25 +00:00
|
|
|
static char *SavedArgs = 0;
|
|
|
|
static unsigned SavedArgsLength = 0;
|
2003-10-28 18:56:51 +00:00
|
|
|
|
2004-02-10 18:01:00 +00:00
|
|
|
static const char *OutputFilename = "llvmprof.out";
|
|
|
|
|
2003-10-28 18:56:51 +00:00
|
|
|
/* save_arguments - Save argc and argv as passed into the program for the file
|
|
|
|
* we output.
|
|
|
|
*/
|
2004-02-10 17:36:25 +00:00
|
|
|
int save_arguments(int argc, const char **argv) {
|
2003-10-28 22:45:25 +00:00
|
|
|
unsigned Length, i;
|
2004-02-10 17:36:25 +00:00
|
|
|
if (SavedArgs || !argv) return argc; /* This can be called multiple times */
|
2003-10-28 18:56:51 +00:00
|
|
|
|
2004-02-10 18:01:00 +00:00
|
|
|
/* Check to see if there are any arguments passed into the program for the
|
|
|
|
* profiler. If there are, strip them off and remember their settings.
|
|
|
|
*/
|
|
|
|
while (argc > 1 && !strncmp(argv[1], "-llvmprof-", 10)) {
|
|
|
|
/* Ok, we have an llvmprof argument. Remove it from the arg list and decide
|
|
|
|
* what to do with it.
|
|
|
|
*/
|
|
|
|
const char *Arg = argv[1];
|
|
|
|
memmove(&argv[1], &argv[2], (argc-2)*sizeof(char*));
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
if (!strcmp(Arg, "-llvmprof-output")) {
|
|
|
|
if (argc == 1)
|
|
|
|
puts("-llvmprof-output requires a filename argument!");
|
|
|
|
else {
|
|
|
|
OutputFilename = strdup(argv[1]);
|
|
|
|
memmove(&argv[1], &argv[2], (argc-2)*sizeof(char*));
|
|
|
|
--argc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("Unknown option to the profiler runtime: '%s' - ignored.\n", Arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-28 22:45:25 +00:00
|
|
|
for (Length = 0, i = 0; i != (unsigned)argc; ++i)
|
|
|
|
Length += strlen(argv[i])+1;
|
2003-10-28 18:56:51 +00:00
|
|
|
|
2003-10-28 22:45:25 +00:00
|
|
|
SavedArgs = (char*)malloc(Length);
|
|
|
|
for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
|
|
|
|
unsigned Len = strlen(argv[i]);
|
|
|
|
memcpy(SavedArgs+Length, argv[i], Len);
|
|
|
|
Length += Len;
|
|
|
|
SavedArgs[Length++] = ' ';
|
|
|
|
}
|
2003-10-28 18:56:51 +00:00
|
|
|
|
2003-10-28 22:45:25 +00:00
|
|
|
SavedArgsLength = Length;
|
2004-02-10 17:36:25 +00:00
|
|
|
|
|
|
|
return argc;
|
2003-10-28 22:45:25 +00:00
|
|
|
}
|
2003-10-28 18:56:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* write_profiling_data - Write a raw block of profiling counters out to the
|
|
|
|
* llvmprof.out file. Note that we allow programs to be instrumented with
|
|
|
|
* multiple different kinds of instrumentation. For this reason, this function
|
|
|
|
* may be called more than once.
|
|
|
|
*/
|
|
|
|
void write_profiling_data(enum ProfilingType PT, unsigned *Start,
|
|
|
|
unsigned NumElements) {
|
|
|
|
static int OutFile = -1;
|
|
|
|
int PTy;
|
|
|
|
|
|
|
|
/* If this is the first time this function is called, open the output file for
|
|
|
|
* appending, creating it if it does not already exist.
|
|
|
|
*/
|
|
|
|
if (OutFile == -1) {
|
|
|
|
off_t Offset;
|
2004-02-10 18:01:00 +00:00
|
|
|
OutFile = open(OutputFilename, O_CREAT | O_WRONLY | O_APPEND, 0666);
|
2003-10-28 18:56:51 +00:00
|
|
|
if (OutFile == -1) {
|
2004-02-10 18:01:00 +00:00
|
|
|
fprintf(stderr, "LLVM profiling runtime: while opening '%s': ",
|
|
|
|
OutputFilename);
|
|
|
|
perror("");
|
2003-10-28 18:56:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output the command line arguments to the file. */
|
|
|
|
{
|
|
|
|
int PTy = Arguments;
|
|
|
|
int Zeros = 0;
|
|
|
|
write(OutFile, &PTy, sizeof(int));
|
2003-10-28 22:45:25 +00:00
|
|
|
write(OutFile, &SavedArgsLength, sizeof(unsigned));
|
|
|
|
write(OutFile, SavedArgs, SavedArgsLength);
|
2003-10-28 18:56:51 +00:00
|
|
|
/* Pad out to a multiple of four bytes */
|
2003-10-28 22:45:25 +00:00
|
|
|
if (SavedArgsLength & 3)
|
|
|
|
write(OutFile, &Zeros, 4-(SavedArgsLength&3));
|
2003-10-28 18:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out this record! */
|
|
|
|
PTy = PT;
|
|
|
|
write(OutFile, &PTy, sizeof(int));
|
2003-10-28 19:35:56 +00:00
|
|
|
write(OutFile, &NumElements, sizeof(unsigned));
|
2003-10-28 18:56:51 +00:00
|
|
|
write(OutFile, Start, NumElements*sizeof(unsigned));
|
|
|
|
}
|