Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2000 Massimiliano Giorgi
 * Copyright (C) 2002 Paolo Gai
 * Copyright (C) 2002 Tomas Lenvall
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * CVS :        $Id: util.c,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $
 */


#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "types.h"
#include <trace.h>
#include <types.h>

static char *names[]={
  "reserved",
  "hoops",
  /*--*/
  "create",
  "activate",
  "schedule",
  "delay",
  "sleep",
  "endcycle",
  "destroy",
  "intactiv",
  "disable",
  /*--*/
  "user0",
  "user1",
  "user2",
  "user3",
  "user4",
  "user5",
  "user6",
  "user7",
  /*--*/    
  "wait",
  "waitnb",
  "signal"
};

int classtable[TRC_NUMCLASSES+1]={
  TRC_F_TRACER,
  TRC_F_SYSTEM,
  TRC_F_USER,
  TRC_F_SEM,
  TRC_F_LAST
};

int event_class(int ev)
{
  int i;

  if (ev < 0 || ev >= TRC_NUMEVENTS)
     return -1;

  for (i = 0; i < TRC_NUMCLASSES; i++)
      if (ev >= classtable[i] && ev < classtable[i+1])
         return i;

  return -1;
}

char *event_name(int ev)
{
  if (ev<0||ev>=TRC_NUMEVENTS) return "unknown";
  return names[ev];
}

char *event_hexdump(u_int8_t *ptr,int maxsize)
{
  static char buffer[256];
  int i;
  for (i=0;i<maxsize;i++) sprintf(buffer+i*2,"%02x",*(ptr+i));
  buffer[maxsize*2]='\0';
  return buffer;
}

char *event_strdump(u_int8_t *ptr, int maxsize)
{
  static char buffer[256];
  memcpy(buffer,ptr,maxsize);
  buffer[maxsize]='\0';
  return buffer;
}


char *format_time(long time)
{
  static char buffer[256];
 
  if (time<1000l) {
    sprintf(buffer,"%li",time);
    return buffer;
  }
  if (time<1000000l) {
    sprintf(buffer,"%li,%03li",time/1000l,time%1000l);
    return buffer;
  }
  sprintf(buffer,"%li,%03li,%03li",
          (time/1000000l),
          (time%1000000l)/1000l,
          time%1000l);
  return buffer;         
}

#ifndef O_BINARY
#define O_BINARY 0
#endif

int read_trace(char *filename,int (*func)(trc_event_t *))
{
  trc_event_t buffer;
  int fin;
  int res;
 
  fin=open(filename,O_RDONLY|O_BINARY);
  if (fin==-1) return -1;
  for (;;) {
    res=read(fin,&buffer,sizeof(trc_event_t));
    if (res!=sizeof(trc_event_t)) {
      close(fin);
      return res==0?0:-2;
    }
    res=(*func)(&buffer);
    if (res!=0) {
      close(fin);
      return res;
    }
  }
  close(fin);
  return 0;
}


/* reads trace events from a udp message */
int read_udp_trace(char *msg, int (*func)(trc_event_t *))
{
  short int events; // temporary storage of nr of events
  int res;

  /* message:
     |2 bytes=nr of events|12 bytes=event 0|12 bytes=event 1|...  

     Note: the size of an event depends on the extra informations that
     are put on the trc_event_t data structures. That size is
     currently 12 bytes, but it can change if additional fields are
     added to the trc_event_t structure. Including the
     include/trace/types.h header ensures that the size used in this
     application is coherent with the size of the compiled Shark
     applications...  
  */

  events = *((short int *)msg);

  msg += 2; // skip the 2 first bytes

  for (;
       events > 0;
       events--, msg += sizeof(trc_event_t))
    res = (*func)((trc_event_t *)msg);

  return 1;
}