llvm-6502/runtime/libprofile/CommonProfiling.c
2003-10-28 22:45:25 +00:00

89 lines
2.7 KiB
C

/*===-- 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>
#include <stdlib.h>
static char *SavedArgs = 0;
static unsigned SavedArgsLength = 0;
/* save_arguments - Save argc and argv as passed into the program for the file
* we output.
*/
void save_arguments(int argc, const char **argv) {
unsigned Length, i;
if (SavedArgs || !argv) return; /* This can be called multiple times */
for (Length = 0, i = 0; i != (unsigned)argc; ++i)
Length += strlen(argv[i])+1;
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++] = ' ';
}
SavedArgsLength = Length;
}
/* 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;
OutFile = open("llvmprof.out", O_CREAT | O_WRONLY | O_APPEND, 0666);
if (OutFile == -1) {
perror("LLVM profiling: while opening 'llvmprof.out'");
return;
}
/* Output the command line arguments to the file. */
{
int PTy = Arguments;
int Zeros = 0;
write(OutFile, &PTy, sizeof(int));
write(OutFile, &SavedArgsLength, sizeof(unsigned));
write(OutFile, SavedArgs, SavedArgsLength);
/* Pad out to a multiple of four bytes */
if (SavedArgsLength & 3)
write(OutFile, &Zeros, 4-(SavedArgsLength&3));
}
}
/* Write out this record! */
PTy = PT;
write(OutFile, &PTy, sizeof(int));
write(OutFile, &NumElements, sizeof(unsigned));
write(OutFile, Start, NumElements*sizeof(unsigned));
}