Classes | Macros | Typedefs | Enumerations | Functions | Variables
trace.h File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <rms/util/trace_intr.h>
#include <stdlib.h>
#include <alloca.h>

Go to the source code of this file.

Classes

struct  s_tracePrint
 
struct  s_traceControl
 
union  u_traceIoctl
 

Macros

#define __TRACE_H_REV   "\$RCSfile: trace.h,v $\$Revision: 1.1.1.1 $\$Date: 2009/10/09 20:03:00 $"
 
#define __USE_POSIX
 
#define TRACE(lvl, msg, params...)
 
#define TRACE_MAXIMUM_NUMBER_OF_FUNCTIONS   (sizeof(int)*8)
 
#define TRACE_NAME   ""
 
#define traceControl_TimeDiff(sooner, later)
 
#define TRACE_COMPILER_DOES_DYNAMIC_ARRAYS   1
 
#define TRACE_NUM_PARAMETER_INTS   6
 
#define TRACE_INIT_CHECK
 
#define TRACE_TID   traceTID
 
#define TRACE_FUNCTION(tv_adr, tid, lvl, msg, params...)
 
#define TRACE_FUNCTIONS   {0,0,trace_printf}
 
#define TRACE_USER_FUNCTION(tv_adr, tid, lvl, msg, params...)
 
#define traceControl_ReInit(name)
 
#define traceControl_Mode(new_mode)
 
#define traceControl_ModeGet()
 
#define traceControl_ModeSet(mask)
 
#define traceControl_ModeClr(mask)
 
#define traceControl_Reset()
 
#define traceControl_PMCGet(which)
 
#define traceControl_PMCRead(which, cpuidOut_ptr)
 
#define traceControl_LevelSet(func, __mask__)
 
#define traceControl_LevelGet(func)
 

Typedefs

typedef void(* func_ptr_t) (struct timeval *, int, int, char *,...)
 

Enumerations

enum  e_traceIoctl {
  traceIoctl_e_init, traceIoctl_e_mapControl, traceIoctl_e_mapLevel, traceIoctl_e_reset,
  traceIoctl_e_versionGet, traceIoctl_e_cpuGet, traceIoctl_e_modeSet, traceIoctl_e_levelSet,
  traceIoctl_e_PMCGet
}
 

Functions

static void trace_printf (struct timeval *tv_sp, int tid, int lvl, char *msg,...)
 
static int traceControl_Init (char *name)
 

Variables

static struct s_traceControltraceControl_sp =0
 
static inttraceLevel_ip
 
static int traceTID =0
 
static func_ptr_t trace_user_functions [] =TRACE_FUNCTIONS
 
static int traceControl_fd =0
 
static int disabled_level [4] ={0}
 
static struct s_traceControl disabled_control ={{0}}
 

Detailed Description

Contains all the userspace and kernel space macros for running TRACE. It also contains some of the data structures, and the userspace initialization routine.

Definition in file trace.h.

Macro Definition Documentation

#define __TRACE_H_REV   "\$RCSfile: trace.h,v $\$Revision: 1.1.1.1 $\$Date: 2009/10/09 20:03:00 $"

Definition at line 15 of file trace.h.

#define __USE_POSIX

Definition at line 34 of file trace.h.

#define TRACE (   lvl,
  msg,
  params... 
)
Value:
({ int lidx;\
struct timeval lclTime={0,0};/*an indication that no trace occurred*/\
/* first function activated sets the reference time */\
TRACE_INIT_CHECK;\
if ((traceControl_sp->mode&(1<<0)) && (traceLevel_ip[lidx+0]&(1<<lvl)))\
{ /* need to provide a way to return time (only when in user space\
though?maybe?) */\
TRACE_FUNCTION( &lclTime, TRACE_TID, lvl, msg , ## params );\
}\
TRACE_USER_FUNCTION( &lclTime, TRACE_TID, lvl, msg , ## params );\
lclTime;\
})
static int * traceLevel_ip
Definition: trace.h:346
int numberOfFunctions
Definition: trace.h:151
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
#define TRACE_TID
Definition: trace.h:366
#define TRACE_FUNCTION(tv_adr, tid, lvl, msg, params...)
Definition: trace.h:379

The TRACE macro

The macro first checks to see if TRACE is installed on the system. This is accomplished by the TRACE_INIT_CHECK macro. If TRACE is in fact installed we move on and verify that logging with TRACE is enabled, and that logging as the appropriate level is enabled. If all that is true, we call the TRACE_FUNCTION. The TRACE macro will return the time at which it was called. If no TRACE took place, the time will be 0.

Bug:
The TRACE_USER_FUNCTION macro should be taken out. The TRACE_FUNCTION macro should handle printing to all the TRACE functions.
Parameters
lvlThe TRACE level
msgThe TRACE format message
paramsParameters for the TRACE format message
Returns
The time that the macro was called if TRACE is setup on the system, 0 otherwise.

Definition at line 67 of file trace.h.

#define TRACE_COMPILER_DOES_DYNAMIC_ARRAYS   1

Definition at line 118 of file trace.h.

#define TRACE_FUNCTION (   tv_adr,
  tid,
  lvl,
  msg,
  params... 
)
Value:
trace_function( tv_adr, tid\
,lvl, msg , ## params )
static void trace_function(struct timeval *time, int tid,...)
Definition: trace_intr.h:93

The TRACE_FUNCTION sets the appropriate registers and then jumps into kernel space. There is a seperate TRACE_FUNCTION for each architecture that TRACE supports.

Parameters
tv_adrPointer to a timeval struct that will contain the time at which the TRACE was made.
tidThe TRACE ID, usually 0 in kernel space
msgThe TRACE format string
paramsThe integer parameters to the format string

Definition at line 379 of file trace.h.

#define TRACE_FUNCTIONS   {0,0,trace_printf}

An array of function pointers to the various userspace TRACE functions. They can be used as follows:

extern void trace_print( struct timeval, int, int, char*, ...); #define TRACE_FUNCTIONS {trace_print} #define TRACE_NAME "simple"

Definition at line 392 of file trace.h.

#define TRACE_INIT_CHECK
Value:
do\
} while (0)
static int traceControl_Init(char *name)
Definition: trace.h:486
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Check to see if the kernel has TRACE support, and if so map the traceControl data strcture into our address space so we can do stuff with it.

Definition at line 359 of file trace.h.

#define TRACE_MAXIMUM_NUMBER_OF_FUNCTIONS   (sizeof(int)*8)

The maximum number of TRACE message logging functions that can be used. The TRACE mode is a bitmask for enabling the different TRACE functions. Because the TRACE mode is an integer, we are limited to however many bit are in an integer.

Definition at line 88 of file trace.h.

#define TRACE_NAME   ""

The default name for a TRACE source

Definition at line 94 of file trace.h.

Referenced by traceControl_Init().

#define TRACE_NUM_PARAMETER_INTS   6

The maximum number of integer parameters that can be passed to the TRACE macro.

Definition at line 125 of file trace.h.

#define TRACE_TID   traceTID

The numerical TRACE ID. This is procured from the TRACE_NAME.

Definition at line 366 of file trace.h.

#define TRACE_USER_FUNCTION (   tv_adr,
  tid,
  lvl,
  msg,
  params... 
)
Value:
/* NOTE THE FUNCTION ARRAY IDX 0 LINES UP WITH MODE/LEVEL IDX 1 */\
do \
{ int _ii_, lidx;\
for (_ii_=1; _ii_<traceControl_sp->numberOfFunctions; _ii_++)\
{ if ((traceControl_sp->mode&(1<<_ii_)) && (traceLevel_ip[lidx+_ii_]&(1<<lvl)))\
{ if (trace_user_functions[_ii_-1])\
{ if ((tv_adr)->tv_sec == 0) gettimeofday( tv_adr, 0 );\
trace_user_functions[_ii_-1](tv_adr,tid,lvl, msg , ## params );\
}\
}\
}\
} while (0)
static int * traceLevel_ip
Definition: trace.h:346
int numberOfFunctions
Definition: trace.h:151
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
#define TRACE_TID
Definition: trace.h:366
static func_ptr_t trace_user_functions[]
Definition: trace.h:431

The TRACE_USER_FUNCTION macro loops through all the user space TRACE functions and executes them.

Parameters
tv_adrPointer to a timeval struct that will contain the time at which the TRACE was made.
tidThe TRACE ID, usually 0 in kernel space
msgThe TRACE format string
paramsThe integer parameters to the format string

Definition at line 444 of file trace.h.

#define traceControl_LevelGet (   func)
Value:
({ int lidx;\
lidx = TRACE_TID * traceControl_sp->numberOfFunctions;\
})
static int * traceLevel_ip
Definition: trace.h:346
static int traceControl_Init(char *name)
Definition: trace.h:486
double func(double x, double y)
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
#define TRACE_TID
Definition: trace.h:366

Get the current TRACE level for a specific TRACE function.

Parameters
funcThe index of the function who's level we want to get.
Returns
The TRACE level for the TRACE function with index func.

Definition at line 754 of file trace.h.

#define traceControl_LevelSet (   func,
  __mask__ 
)
Value:
({ union u_traceIoctl ioctl_data;\
{ ioctl_data.generic.num = traceTID;\
ioctl_data.generic.ptr = (void*)func;\
ioctl_data.generic.mask = __mask__;\
ioctl( traceControl_fd, traceIoctl_e_levelSet, &ioctl_data );\
}\
else if (func < (sizeof(disabled_level)/sizeof(int)))\
{ traceLevel_ip[func] = __mask__;\
}\
})
static int * traceLevel_ip
Definition: trace.h:346
static int disabled_level[4]
Definition: trace.h:472
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
double func(double x, double y)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
static int traceTID
Definition: trace.h:352

Set the TRACE level for the current TRACE ID for a particular TRACE function.

Bug:

Does this really want the function number?

mask isn't a very good name.

Parameters
funcThe index of the function who's level we want to set
<strong>mask</strong>The new level mask.

Definition at line 734 of file trace.h.

#define traceControl_Mode (   new_mode)
Value:
({ int old_mode;\
old_mode = traceControl_sp->mode;\
{ ioctl( traceControl_fd, traceIoctl_e_modeSet, new_mode );\
}\
old_mode;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Set the TRACE mode. Currently, there are two modes. Mode "0" is TRACE turned off, while mode "1" is TRACE turned on.

Parameters
new_modeThe mode that TRACE will switch to.

Definition at line 618 of file trace.h.

#define traceControl_ModeClr (   mask)
Value:
({ int old_mode, new_mode;\
old_mode = traceControl_sp->mode;\
new_mode = old_mode & ~mask;\
{ ioctl( traceControl_fd, traceIoctl_e_modeSet, new_mode );\
}\
else\
{ traceControl_sp->mode = new_mode;\
}\
old_mode;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Apply a mask (bitwise AND) to the TRACE mode.

Parameters
maskThe mask that will be ANDed to the TRACE mode.

Definition at line 662 of file trace.h.

#define traceControl_ModeGet ( )
Value:
traceControl_sp->mode;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Get the current TRACE mode.

Returns
The current TRACE mode.

Definition at line 633 of file trace.h.

#define traceControl_ModeSet (   mask)
Value:
({ int old_mode, new_mode;\
old_mode = traceControl_sp->mode;\
new_mode = old_mode | mask;\
{ ioctl( traceControl_fd, traceIoctl_e_modeSet, new_mode );\
}\
else\
{ traceControl_sp->mode = new_mode;\
}\
old_mode;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Apply a mask (bitwise OR) to the TRACE mode.

Parameters
maskThe mask that will be ORed with the TRACE mode.

Definition at line 643 of file trace.h.

#define traceControl_PMCGet (   which)
Value:
({ union u_traceIoctl ioctl_data;\
{ ioctl_data.pmc.whichIn_cpuidOut = which;\
ioctl( traceControl_fd, traceIoctl_e_PMCGet, &ioctl_data );\
}\
ioctl_data.pmc.pmc_data_val;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Get the data from the Performance Monitoring Counter on a specific CPU.

Parameters
whichWhich CPU to get the data from
Returns
The value from the PMC on the specific CPU.

Definition at line 693 of file trace.h.

#define traceControl_PMCRead (   which,
  cpuidOut_ptr 
)
Value:
({ union u_traceIoctl ioctl_data;\
{ ioctl_data.pmc.whichIn_cpuidOut = which;\
ioctl( traceControl_fd, traceIoctl_e_PMCGet, &ioctl_data );\
}\
*(cpuidOut_ptr) = ioctl_data.pmc.whichIn_cpuidOut;\
ioctl_data.pmc.pmc_data_val;\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
if(dump)
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Practically the same as traceControl_PMCGet, except that this will place the cpuID in the area pointed to by cpuidOut_ptr.

Bug:
I really don't know what this will do.
Parameters
whichThe processor to retreive PMC data from
cpuidOut_ptrA pointer to an int where the processes ID will be stored.

Definition at line 713 of file trace.h.

#define traceControl_ReInit (   name)
Value:
({ int __sts__=0;\
}\
else if (traceControl_fd > 0)\
{ /* get a traceTID via ioctl */\
union u_traceIoctl ioctl_data;\
ioctl_data.generic.ptr = name;\
__sts__ = ioctl( traceControl_fd, traceIoctl_e_init, &ioctl_data );\
traceTID = ioctl_data.generic.num;\
}\
__sts__;\
})
const XML_Char * name
Definition: expat.h:151
static int traceControl_Init(char *name)
Definition: trace.h:486
void * ptr
Definition: trace.h:207
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
static int traceTID
Definition: trace.h:352
struct u_traceIoctl::@50 generic

Get a new TRACE ID and name for a process. This can be useful for forked processes that want a different name.

Parameters
nameThe new TRACE name

Definition at line 597 of file trace.h.

#define traceControl_Reset ( )
Value:
if (traceControl_fd > 0)\
}\
})
static int traceControl_Init(char *name)
Definition: trace.h:486
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338

Reset TRACE, which clears the TRACE circular buffer.

Definition at line 679 of file trace.h.

#define traceControl_TimeDiff (   sooner,
  later 
)
Value:
({ int secs, usecs;\
secs = later.tv_sec - sooner.tv_sec;\
usecs = later.tv_usec - sooner.tv_usec;\
((secs*1000000) + usecs);\
})

Determine the difference in microseconds between two timeval structures.

Parameters
soonerThe earlier timeval struct
laterThe later timeval struct
Returns
The difference in microseconds between the two timeval structures.

Definition at line 107 of file trace.h.

Typedef Documentation

typedef void(* func_ptr_t) (struct timeval *, int, int, char *,...)

Definition at line 430 of file trace.h.

Enumeration Type Documentation

Enumerations for the various things you can have TRACE do through the ioctl() interface.

Enumerator
traceIoctl_e_init 
traceIoctl_e_mapControl 
traceIoctl_e_mapLevel 
traceIoctl_e_reset 
traceIoctl_e_versionGet 
traceIoctl_e_cpuGet 
traceIoctl_e_modeSet 
traceIoctl_e_levelSet 
traceIoctl_e_PMCGet 

Definition at line 188 of file trace.h.

Function Documentation

static void trace_printf ( struct timeval *  tv_sp,
int  tid,
int  lvl,
char *  msg,
  ... 
)
static

A user space TRACE print function. This one prints TRACE messages using printf.

Parameters
tv_adrPointer to a timeval struct that will contain the time at which the TRACE was made.
tidThe TRACE ID, usually 0 in kernel space
msgThe TRACE format string
paramsThe integer parameters to the format string

Definition at line 405 of file trace.h.

406 { va_list ap;
407  char timbuf[100];
408  int timlen;
409  char *newbuf;
410  struct tm tm_s;
411 
412  localtime_r( (time_t *)&tv_sp->tv_sec, &tm_s );
413  timlen = strftime( timbuf, sizeof(timbuf), "%a %H:%M:%S", &tm_s );
414 
415  sprintf( &timbuf[timlen], ".%06d: ", (int)tv_sp->tv_usec );
416  newbuf = (char *)alloca( timlen+strlen(&timbuf[timlen])+strlen(msg)+2/*room for "\n\0"*/ );
417 
418  /* now accomplish the prepending of the time and the appending of the newline */
419  sprintf( newbuf, "%s%s\n", timbuf, msg );
420 
421  va_start( ap, msg );
422  vprintf( newbuf, ap );
423  va_end( ap );
424 
425  return;
426 }
static int traceControl_Init ( char *  name)
static

Called if there is TRACE support in the kernel, this function will initialize the traceControl_sp data structures.

Parameters
nameThe TRACE name that will be used for all logging.

Definition at line 486 of file trace.h.

References close(), disabled_control, disabled_level, u_traceIoctl::generic, u_traceIoctl::num, s_traceControl::numberOfFunctions, s_traceControl::numberOfTID, open(), u_traceIoctl::ptr, make_template_knob_config::stderr, TRACE_NAME, and traceIoctl_e_init.

487 {
488  int fd_map;
489  int levelArraySize;
490  int levelOffset;
491  int sts;
492  union u_traceIoctl ioctl_data;
493 
494  /* This should assure we do init processing once. Use ReInit to
495  change name.
496  Note: first call may return -1 while successive calls will
497  return 0. */
498  if (traceControl_sp) return (0);
499 
500  /* incase anything bad happens... */
501  traceTID = 0;
505  /sizeof(disabled_level[0]));
506 
507  traceControl_fd = open( "/proc/trace/control", O_RDONLY );
508  if (traceControl_fd == -1)
509  { perror( "open /proc/trace/control" );
510  fprintf( stderr, "continueing with disabled tracing\n" );
511  return (-1);
512  }
513 
514  /* get a traceTID via ioctl */
515  if (name[0] == '\0')
516  { /* try TRACE_NAME (which may actually be the same thing [""] */
517  name = TRACE_NAME;
518  }
519  ioctl_data.generic.ptr = name;
520  sts = ioctl( traceControl_fd, traceIoctl_e_init, &ioctl_data );
521  traceTID = ioctl_data.generic.num;
522 
523 
524  fd_map = open( "/proc/trace/map", O_RDONLY );
525  if (fd_map == -1)
526  { perror( "open /proc/trace/map" );
527  fprintf( stderr, "continueing with disabled tracing\n" );
528  traceTID = 0;
529  traceControl_fd = -1;
533  /sizeof(disabled_level[0]));
534  return (-1);
535  }
536 
537  traceControl_sp = (struct s_traceControl *)mmap( 0 /* hint address */
538  , sizeof(struct s_traceControl)
539  , PROT_READ
540  , MAP_SHARED
541  , fd_map
542  , 0 /* offset */ );
543  if (traceControl_sp == (void *)-1/*MAP_FAILED*/)
544  { perror( "mmap /proc/trace/map" );
545  fprintf( stderr, "continueing with disabled tracing\n" );
546  traceTID = 0;
547  traceControl_fd = -1;
551  /sizeof(disabled_level[0]));
552  return (-1);
553  }
554 
555  levelArraySize = ( traceControl_sp->numberOfTID
557 
558  /* NEED TO REORGANIZE STRUCTURE TO REMOVE HARD CODED NUMBER
559  ref. trace_proc.c:trace_proc_map_mmap() */
560  levelOffset = 4096;
561 
562  traceLevel_ip = (int *)mmap( 0 /* hint address */
563  , levelArraySize * sizeof(int)
564  , PROT_READ
565  , MAP_SHARED
566  , fd_map
567  , levelOffset );
568 
569  if (traceLevel_ip == (void *)-1/*MAP_FAILED*/)
570  { perror( "mmap level" );
571  fprintf( stderr, "continueing with disabled tracing\n" );
572  traceTID = 0;
573  traceControl_fd = -1;
577  /sizeof(disabled_level[0]));
578  return (-1);
579  }
580 
581  /* skip past initial
582  NOTE: it does not work to add this to the offset as mmap page
583  aligns the returned pointer */
585 
586  close( fd_map );
587 
588  return (0);
589 } /* traceControl_Init */
const XML_Char * name
Definition: expat.h:151
static int * traceLevel_ip
Definition: trace.h:346
static int disabled_level[4]
Definition: trace.h:472
int numberOfFunctions
Definition: trace.h:151
#define TRACE_NAME
Definition: trace.h:94
int numberOfTID
Definition: trace.h:149
procfile open("FD_BRL_v0.txt")
static struct s_traceControl disabled_control
Definition: trace.h:478
static int traceControl_fd
Definition: trace.h:464
static struct s_traceControl * traceControl_sp
Definition: trace.h:338
static int traceTID
Definition: trace.h:352
procfile close()

Variable Documentation

struct s_traceControl disabled_control ={{0}}
static

This is a copy of the control structure that has been disabled and is used when there is no TRACE support in the kernel.

Definition at line 478 of file trace.h.

Referenced by traceControl_Init().

int disabled_level[4] ={0}
static

This is the level mask that is used if TRACE is disabled.

Bug:
Should this be 4, or should it be the number of TRACE functions?

Definition at line 472 of file trace.h.

Referenced by traceControl_Init().

func_ptr_t trace_user_functions[] =TRACE_FUNCTIONS
static

Definition at line 431 of file trace.h.

int traceControl_fd =0
static

traceControl_fd is point at /proc/trace/control and is used when making ioctl() calls for TRACE.

Definition at line 464 of file trace.h.

struct s_traceControl* traceControl_sp =0
static

Global pointer to the TRACE control structure.

Definition at line 338 of file trace.h.

int* traceLevel_ip
static

Pointer to an array that contains the information about the levels for all the TRACE functions.

Bug:
Why is this not in the traceControl data structure?

Definition at line 346 of file trace.h.

int traceTID =0
static

Global value to keep track of the TRACE ID of the current process.

Definition at line 352 of file trace.h.