Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   (see the web pages for full authors list)
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/*
22
 * Copyright (C) 2000 Massimiliano Giorgi
23
 * Copyright (C) 2002 Paolo Gai
24
 * Copyright (C) 2002 Tomas Lenvall
25
 *
26
 * This program is free software; you can redistribute it and/or modify
27
 * it under the terms of the GNU General Public License as published by
28
 * the Free Software Foundation; either version 2 of the License, or
29
 * (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program; if not, write to the Free Software
38
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39
 *
40
 * CVS :        $Id: util.c,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $
41
 */
42
 
43
#include <stdio.h>
44
#include <string.h>
45
#include <fcntl.h>
46
#include <unistd.h>
47
#include <stdlib.h>
48
 
49
#include "types.h"
50
#include <trace.h>
51
#include <types.h>
52
 
53
static char *names[]={
54
  "reserved",
55
  "hoops",
56
  /*--*/
57
  "create",
58
  "activate",
59
  "schedule",
60
  "delay",
61
  "sleep",
62
  "endcycle",
63
  "destroy",
64
  "intactiv",
65
  "disable",
66
  /*--*/
67
  "user0",
68
  "user1",
69
  "user2",
70
  "user3",
71
  "user4",
72
  "user5",
73
  "user6",
74
  "user7",
75
  /*--*/    
76
  "wait",
77
  "waitnb",
78
  "signal"
79
};
80
 
81
int classtable[TRC_NUMCLASSES+1]={
82
  TRC_F_TRACER,
83
  TRC_F_SYSTEM,
84
  TRC_F_USER,
85
  TRC_F_SEM,
86
  TRC_F_LAST
87
};
88
 
89
int event_class(int ev)
90
{
91
  int i;
92
 
93
  if (ev < 0 || ev >= TRC_NUMEVENTS)
94
     return -1;
95
 
96
  for (i = 0; i < TRC_NUMCLASSES; i++)
97
      if (ev >= classtable[i] && ev < classtable[i+1])
98
         return i;
99
 
100
  return -1;
101
}
102
 
103
char *event_name(int ev)
104
{
105
  if (ev<0||ev>=TRC_NUMEVENTS) return "unknown";
106
  return names[ev];
107
}
108
 
109
char *event_hexdump(u_int8_t *ptr,int maxsize)
110
{
111
  static char buffer[256];
112
  int i;
113
  for (i=0;i<maxsize;i++) sprintf(buffer+i*2,"%02x",*(ptr+i));
114
  buffer[maxsize*2]='\0';
115
  return buffer;
116
}
117
 
118
char *event_strdump(u_int8_t *ptr, int maxsize)
119
{
120
  static char buffer[256];
121
  memcpy(buffer,ptr,maxsize);
122
  buffer[maxsize]='\0';
123
  return buffer;
124
}
125
 
126
 
127
char *format_time(long time)
128
{
129
  static char buffer[256];
130
 
131
  if (time<1000l) {
132
    sprintf(buffer,"%li",time);
133
    return buffer;
134
  }
135
  if (time<1000000l) {
136
    sprintf(buffer,"%li,%03li",time/1000l,time%1000l);
137
    return buffer;
138
  }
139
  sprintf(buffer,"%li,%03li,%03li",
140
          (time/1000000l),
141
          (time%1000000l)/1000l,
142
          time%1000l);
143
  return buffer;         
144
}
145
 
146
#ifndef O_BINARY
147
#define O_BINARY 0
148
#endif
149
 
150
int read_trace(char *filename,int (*func)(trc_event_t *))
151
{
152
  trc_event_t buffer;
153
  int fin;
154
  int res;
155
 
156
  fin=open(filename,O_RDONLY|O_BINARY);
157
  if (fin==-1) return -1;
158
  for (;;) {
159
    res=read(fin,&buffer,sizeof(trc_event_t));
160
    if (res!=sizeof(trc_event_t)) {
161
      close(fin);
162
      return res==0?0:-2;
163
    }
164
    res=(*func)(&buffer);
165
    if (res!=0) {
166
      close(fin);
167
      return res;
168
    }
169
  }
170
  close(fin);
171
  return 0;
172
}
173
 
174
 
175
/* reads trace events from a udp message */
176
int read_udp_trace(char *msg, int (*func)(trc_event_t *))
177
{
178
  short int events; // temporary storage of nr of events
179
  int res;
180
 
181
  /* message:
182
     |2 bytes=nr of events|12 bytes=event 0|12 bytes=event 1|...  
183
 
184
     Note: the size of an event depends on the extra informations that
185
     are put on the trc_event_t data structures. That size is
186
     currently 12 bytes, but it can change if additional fields are
187
     added to the trc_event_t structure. Including the
188
     include/trace/types.h header ensures that the size used in this
189
     application is coherent with the size of the compiled Shark
190
     applications...  
191
  */
192
  events = *((short int *)msg);
193
 
194
  msg += 2; // skip the 2 first bytes
195
 
196
  for (;
197
       events > 0;
198
       events--, msg += sizeof(trc_event_t))
199
    res = (*func)((trc_event_t *)msg);
200
 
201
  return 1;
202
}
203