From 4f48a490f9af51f22946af72d908777af1c1f148 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 20 Jun 2014 14:24:18 +0000 Subject: indent --- instrument.c | 45 ++++------ stack.c | 48 +++++------ stack.h | 21 ++--- symbols.c | 276 +++++++++++++++++++++++++---------------------------------- symbols.h | 23 +++-- trace.c | 92 +++++++++----------- 6 files changed, 215 insertions(+), 290 deletions(-) diff --git a/instrument.c b/instrument.c index 4c9dfa0..99630fa 100644 --- a/instrument.c +++ b/instrument.c @@ -13,43 +13,34 @@ #include /* Function prototypes with attributes */ -void main_constructor( void ) - __attribute__ ((no_instrument_function, constructor)); - -void main_destructor( void ) - __attribute__ ((no_instrument_function, destructor)); - -void __cyg_profile_func_enter( void *, void * ) - __attribute__ ((no_instrument_function)); - -void __cyg_profile_func_exit( void *, void * ) - __attribute__ ((no_instrument_function)); - - +void main_constructor(void) __attribute__((no_instrument_function, constructor)); +void main_destructor(void) __attribute__((no_instrument_function, destructor)); +void __cyg_profile_func_enter(void *, void *) __attribute__((no_instrument_function)); +void __cyg_profile_func_exit(void *, void *) __attribute__((no_instrument_function)); static FILE *fp; - -void main_constructor( void ) +void +main_constructor(void) { - fp = fopen( "trace.txt", "w" ); - if (fp == NULL) exit(-1); + fp = fopen("trace.txt", "w"); + if (fp == NULL) + exit(-1); } - -void main_deconstructor( void ) +void +main_deconstructor(void) { - fclose( fp ); + fclose(fp); } - -void __cyg_profile_func_enter( void *this, void *callsite ) +void +__cyg_profile_func_enter(void *this, void *callsite) { - fprintf(fp, "E%p\n", (int *)this); + fprintf(fp, "E%p\n", (int *) this); } - -void __cyg_profile_func_exit( void *this, void *callsite ) +void +__cyg_profile_func_exit(void *this, void *callsite) { - fprintf(fp, "X%p\n", (int *)this); + fprintf(fp, "X%p\n", (int *) this); } - diff --git a/stack.c b/stack.c index 254283a..fe15590 100644 --- a/stack.c +++ b/stack.c @@ -15,48 +15,44 @@ static int stack[MAX_ELEMENTS]; static int index; -void stackInit( void ) +void +stackInit(void) { - index = 0; - - return; + index = 0; } - -int stackNumElems( void ) +int +stackNumElems(void) { - return index; + return index; } - -unsigned int stackTop( void ) +unsigned int +stackTop(void) { - assert( index > 0 ); + assert(index > 0); - return (stack[index-1]); + return (stack[index - 1]); } - -void stackPush( unsigned int value ) +void +stackPush(unsigned int value) { - assert ( index < MAX_ELEMENTS ); + assert(index < MAX_ELEMENTS); - stack[index] = value; - index++; - - return; + stack[index] = value; + index++; } - -unsigned int stackPop( void ) +unsigned int +stackPop(void) { - unsigned int value; + unsigned int value; - assert( index > 0 ); + assert(index > 0); - index--; - value = stack[index]; + index--; + value = stack[index]; - return value; + return value; } - diff --git a/stack.h b/stack.h index ec7418a..cd95b72 100644 --- a/stack.h +++ b/stack.h @@ -11,15 +11,12 @@ #ifndef __STACK_H #define __STACK_H -void stackInit( void ); - -int stackNumElems( void ); - -unsigned int stackTop( void ); - -void stackPush( unsigned int value ); - -unsigned int stackPop( void ); - -#endif /* __STACK_H */ - +__BEGIN_DECLS +void stackInit(void); +int stackNumElems(void); +unsigned int stackTop(void); +void stackPush(unsigned int value); +unsigned int stackPop(void); +__END_DECLS + +#endif diff --git a/symbols.c b/symbols.c index e4e58a3..62dc73b 100644 --- a/symbols.c +++ b/symbols.c @@ -3,7 +3,7 @@ * File: symbols.c * * Symbols functions. This file has functions for symbols mgmt - * (such as translating addresses to function names with + * (such as translating addresses to function names with * addr2line) and also connectivity matrix functions to keep * the function call trace counts. * @@ -12,197 +12,155 @@ */ #include -#include #include #include #include "stack.h" #include "symbols.h" -func_t functions[MAX_FUNCTIONS]; +func_t functions[MAX_FUNCTIONS]; unsigned int totals[MAX_FUNCTIONS]; unsigned int calls[MAX_FUNCTIONS][MAX_FUNCTIONS]; - char imageName[50]; -void initSymbol( char *image ) +void +initSymbol(char *image) { - int from, to; - - strlcpy( imageName, image, sizeof(imageName) ); - - for ( from = 0 ; from < MAX_FUNCTIONS ; from++ ) { - - functions[from].address = 0; - functions[from].funcName[0] = 0; - totals[from] = 0; + int from, to; - for ( to = 0 ; to < MAX_FUNCTIONS ; to++ ) { + strlcpy(imageName, image, sizeof(imageName)); - calls[from][to] = 0; + for (from = 0; from < MAX_FUNCTIONS; from++) { + functions[from].address = 0; + functions[from].funcName[0] = 0; + totals[from] = 0; - } - - } - - return; + for (to = 0; to < MAX_FUNCTIONS; to++) + calls[from][to] = 0; + } } - -int lookupSymbol( unsigned int address ) +int +lookupSymbol(unsigned int address) { - int index; - - for (index = 0 ; index < MAX_FUNCTIONS ; index++) { + int index; - if (functions[index].address == 0) break; + for (index = 0; index < MAX_FUNCTIONS; index++) { + if (functions[index].address == 0) + break; - if (functions[index].address == address) return index; + if (functions[index].address == address) + return index; + } - } - - assert(0); - - return 0; + return 0; } - -int translateFunctionFromSymbol( unsigned int address, char *func ) +int +translateFunctionFromSymbol(unsigned int address, char *func) { - FILE *p; - char line[100]; - int len, i; - - snprintf( line, sizeof(line), "addr2line -e %s -f -s 0x%x", imageName, address ); - - p = popen( line, "r" ); - - if (p == NULL) return 0; - else { - - len = fread( line, 99, 1, p ); - - i = 0; - while ( i < strlen(line) ) { - - if ((line[i] == 0x0d) || (line[i] == 0x0a)) { - func[i] = 0; - break; - } else { - func[i] = line[i]; - } - - i++; - - } - - pclose(p); - - } - - return 1; + FILE *p; + char line[100]; + int len, i; + + snprintf(line, sizeof(line), "addr2line -e %s -f -s 0x%x", imageName, address); + + p = popen(line, "r"); + + if (p == NULL) + return 0; + else { + len = fread(line, 99, 1, p); + + for (i = 0; i < strlen(line); i++) { + if ((line[i] == 0x0d) || (line[i] == 0x0a)) { + func[i] = 0; + break; + } else { + func[i] = line[i]; + } + } + pclose(p); + } + + return 1; } - -void addSymbol( unsigned int address ) +void +addSymbol(unsigned int address) { - int index; - - for (index = 0 ; index < MAX_FUNCTIONS ; index++) { - - if (functions[index].address == address) return; - - if (functions[index].address == 0) break; - - } - - if (index < MAX_FUNCTIONS) { - - functions[index].address = address; - - translateFunctionFromSymbol( address, functions[index].funcName ); + int index; - } else { + for (index = 0; index < MAX_FUNCTIONS; index++) { + if (functions[index].address == address) + return; - assert( 0 ); + if (functions[index].address == 0) + break; + } - } - - return; + if (index < MAX_FUNCTIONS) { + functions[index].address = address; + translateFunctionFromSymbol(address, functions[index].funcName); + } } - -void addCallTrace( unsigned int address ) +void +addCallTrace(unsigned int address) { - if (stackNumElems()) { - calls[lookupSymbol(stackTop())][lookupSymbol(address)]++; - } - - return; + if (stackNumElems()) + calls[lookupSymbol(stackTop())][lookupSymbol(address)]++; } - -void emitSymbols( void ) +void +emitSymbols(void) { - int from, to; - FILE *fp; - - fp = fopen("graph.dot", "w"); - if (fp == NULL) { - printf("Couldn't open graph.dot\n"); - exit(0); - } - - fprintf(fp, "digraph %s {\n\n", imageName ); - - /* Identify node shapes */ - for (from = 0 ; from < MAX_FUNCTIONS ; from++) { - - if (functions[from].address == 0) break; - - for (to = 0 ; to < MAX_FUNCTIONS ; to++) { - - if (functions[to].address == 0) break; - - if (calls[from][to]) totals[from]++; - - } - - if (totals[from]) { - - fprintf( fp, " %s [shape=rectangle]\n", functions[from].funcName ); - - } else { - - fprintf( fp, " %s [shape=ellipse]\n", functions[from].funcName ); - - } - - } - - /* Emit call graph */ - for (from = 0 ; from < MAX_FUNCTIONS ; from++) { - - if (functions[from].address == 0) break; - - for (to = 0 ; to < MAX_FUNCTIONS ; to++) { - - if (calls[from][to]) { - fprintf( fp, " %s -> %s [label=\"%d call%s\" fontsize=\"10\"]\n", - functions[from].funcName, functions[to].funcName, - calls[from][to], calls[from][to] > 1 ? "s" : "" ); - } - - if (functions[to].address == 0) break; - - } - - } - - fprintf( fp, "\n}\n" ); - - fclose(fp); - - return; + int from, to; + FILE *fp; + + fp = fopen("graph.dot", "w"); + if (fp == NULL) { + printf("Couldn't open graph.dot\n"); + exit(0); + } + fprintf(fp, "digraph %s {\n\n", imageName); + + /* Identify node shapes */ + for (from = 0; from < MAX_FUNCTIONS; from++) { + if (functions[from].address == 0) + break; + + for (to = 0; to < MAX_FUNCTIONS; to++) { + if (functions[to].address == 0) + break; + + if (calls[from][to]) + totals[from]++; + } + + if (totals[from]) + fprintf(fp, " %s [shape=rectangle]\n", functions[from].funcName); + else + fprintf(fp, " %s [shape=ellipse]\n", functions[from].funcName); + } + + /* Emit call graph */ + for (from = 0; from < MAX_FUNCTIONS; from++) { + if (functions[from].address == 0) + break; + + for (to = 0; to < MAX_FUNCTIONS; to++) { + if (calls[from][to]) { + fprintf(fp, " %s -> %s [label=\"%d call%s\" fontsize=\"10\"]\n", + functions[from].funcName, functions[to].funcName, + calls[from][to], calls[from][to] > 1 ? "s" : ""); + } + if (functions[to].address == 0) + break; + } + } + + fprintf(fp, "\n}\n"); + + fclose(fp); } - diff --git a/symbols.h b/symbols.h index bf450ba..83b07a1 100644 --- a/symbols.h +++ b/symbols.h @@ -15,19 +15,16 @@ #define MAX_FUNCTION_NAME 50 typedef struct { - unsigned int address; - char funcName[MAX_FUNCTION_NAME+1]; + unsigned int address; + char funcName[MAX_FUNCTION_NAME + 1]; } func_t; +__BEGIN_DECLS +void initSymbol(char *imageName); +int lookupSymbol(unsigned int address); +void addSymbol(unsigned int address); +void addCallTrace(unsigned int address); +void emitSymbols(void); +__END_DECLS -void initSymbol( char *imageName ); - -int lookupSymbol( unsigned int address ); - -void addSymbol( unsigned int address ); - -void addCallTrace( unsigned int address ); - -void emitSymbols( void ); - -#endif /* __SYMBOLS_H */ +#endif diff --git a/trace.c b/trace.c index d5975a4..31d01f7 100644 --- a/trace.c +++ b/trace.c @@ -15,58 +15,44 @@ #include "symbols.h" #include "stack.h" - -int main( int argc, char *argv[] ) +int +main(int argc, char *argv[]) { - FILE *tracef; - char type; - unsigned int address; - - if (argc != 2) { - - printf("Usage: pvtrace \n\n"); - exit(-1); - - } - - initSymbol( argv[1] ); - stackInit(); - - tracef = fopen("trace.txt", "r"); - - if (tracef == NULL) { - printf("Can't open trace.txt\n"); - exit(-1); - } - - while (!feof(tracef)) { - - fscanf( tracef, "%c0x%x\n", &type, &address ); - - if (type == 'E') { - - /* Function Entry */ - - addSymbol( address ); - - addCallTrace( address ); - - stackPush( address ); - - } else if (type == 'X') { - - /* Function Exit */ - - (void) stackPop(); - - } - - } - - emitSymbols(); - - fclose( tracef ); - - return 0; + FILE *tracef; + char type; + unsigned int address; + + if (argc != 2) { + printf("Usage: pvtrace \n\n"); + exit(-1); + } + initSymbol(argv[1]); + stackInit(); + + tracef = fopen("trace.txt", "r"); + + if (tracef == NULL) { + printf("Can't open trace.txt\n"); + exit(-1); + } + while (!feof(tracef)) { + fscanf(tracef, "%c0x%x\n", &type, &address); + switch (type) { + case 'E': + /* Function Entry */ + addSymbol(address); + addCallTrace(address); + stackPush(address); + break; + case 'X': + /* Function Exit */ + (void) stackPop(); + break; + } + } + + emitSymbols(); + fclose(tracef); + + return 0; } - -- cgit v1.2.3