File: TraceReader.c

package info (click to toggle)
ltt 0.9.5pre6-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,256 kB
  • ctags: 1,630
  • sloc: ansic: 17,284; sh: 8,010; makefile: 252
file content (322 lines) | stat: -rw-r--r-- 10,461 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
   TraceReader.c : Main file for alternate trace reader.
   Copyright (C) 2001 Karim Yaghmour (karym@opersys.com).
 
   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; version 2 of the License.

   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

   History : 
     K.Y., 07/09/1999, Initial typing (largely inspired by TraceVisualizer.c).
*/

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#include <Tables.h>
#include <EventDB.h>
#include <RTAIDB.h>
#include <EventOptions.h>

/******************************************************************
 * Function : 
 *    CustomOperations()
 * Description :
 *    Function containing custom operations done on trace.
 * Parameters :
 *    pmSystem, Pointer to system description
 *    pmTraceDB, Pointer to trace database
 *    pmOptions, User-provided options
 * Return values :
 *    NONE
 * History :
 *    K.Y.   07/09/2001, Initial typing.
 * Note :
 ******************************************************************/
void CustomOperations(systemInfo* pmSystem, db* pmTraceDB, options* pmOptions)
{
  int                 lFormatType;       /* Formatting type */
  char                lEventTime[80];    /* String to print event time */
  char*               lFormatStr;        /* Event formatting string */
  event               lEvent;            /* Generic event */
  uint8_t             lEventID;          /* Event ID */
  eventDescription    lEventDesc;        /* Event description */
  customEventDesc*    pCustomDesc;       /* Custom event description */

  /* Position at first event in list */
  lEvent = pmTraceDB->FirstEvent;

  /* Go through event database */
  do
    {
    /* Get the event's ID */
    lEventID = DBEventID(pmTraceDB, &lEvent);

    /* Depending on the type of event */
    switch(lEventID)
      {
      /* Event creation */
      case TRACE_NEW_EVENT:
	/* Get the event's description */
	DBEventDescription(pmTraceDB, &lEvent, TRUE, &lEventDesc);

	/* Format event's time */
	DBFormatTimeInReadableString(lEventTime, lEventDesc.Time.tv_sec, lEventDesc.Time.tv_usec);

	/* Print out some info on this event */
	printf("New event created at: %s; Event type created: %s; Custom event ID: %d \n",
	       lEventTime,
	       NEW_EVENT(lEventDesc.Struct)->type,
	       RFT32(pmTraceDB, NEW_EVENT(lEventDesc.Struct)->id));
	break;

      /* Custom event */
      case TRACE_CUSTOM:
	/* Get the event's description */
	DBEventDescription(pmTraceDB, &lEvent, FALSE, &lEventDesc);

	/* Get the event's custom description */
	pCustomDesc = DBEventGetCustomDescription(pmTraceDB, &lEvent);

	/* Print out info on event */
	printf("Custom Event: %s; Custom event ID: %d; Format type: %d\n",
	       pCustomDesc->Event.type,
	       pCustomDesc->ID,
	       RFT32(pmTraceDB, pCustomDesc->Event.format_type));
	printf("\t Formatting string: %s \n",
	       pCustomDesc->Event.form);	       
	break;

      /* All other events */
      default:
	break;
      }
    } while(DBEventNext(pmTraceDB, &lEvent) == TRUE); /* Continue until there's no more "next" event */

  printf("\n\n");

  /* Find the event fitting the description "omega" */
  /* WARNING: READ note about the following function in LibLTT/EventDB.c before using it */
  if((lFormatStr = DBEventGetFormatByCustomType(pmTraceDB, "Omega", &lFormatType)) != NULL)
    printf("Formatting string for event of type \"Omega\": %s \n", lFormatStr);

  /* Is this event formatted in XML */
  if(lFormatType == CUSTOM_EVENT_FORMAT_TYPE_XML)
    printf("Event formatted in XML \n");

  /* Change the formatting string for the event type "omega" */
  if(DBEventSetFormatByCustomType(pmTraceDB,
				  "Omega",
				  lFormatType,
				  "<event name=\"Omega\" size=\"0\"><var name=\"a_char\" type=\"char\"/></event>") == TRUE)
    {
    printf("Format string for \"Omega\" should have changed now \n");

    /* Read the description again to make sure it changed */
    if((lFormatStr = DBEventGetFormatByCustomType(pmTraceDB, "Omega", &lFormatType)) != NULL)
      printf("Formatting string for event of type \"Gamma\": %s \n", lFormatStr);
    }

  printf("\n\n");

  /* Find the event of custom event ID 31 */
  if((lFormatStr = DBEventGetFormatByCustomID(pmTraceDB, 31, &lFormatType)) != NULL)
    printf("Formatting string for event of custom ID 31: %s \n", lFormatStr);

  /* Is this event formatted in XML */
  if(lFormatType == CUSTOM_EVENT_FORMAT_TYPE_XML)
    printf("Event formatted in XML \n");


  /* Change the formatting string for custom event ID 31 */
  if(DBEventSetFormatByCustomID(pmTraceDB,
				31,
				lFormatType,
				"<event name=\"Delta\" size=\"0\"><var name=\"a_struct\" type=\"u40\"/></event>") == TRUE)
    {
    printf("Format string for custom event ID 31 should have changed now \n");

    /* Read the description again to make sure it changed */
    if((lFormatStr = DBEventGetFormatByCustomID(pmTraceDB, 31, &lFormatType)) != NULL)
      printf("Formatting string for event of custom ID 31: %s \n", lFormatStr);
    }  
}

/******************************************************************
 * Function : 
 *    main()
 * Description :
 *    Main entry point into data decoder.
 * Parameters :
 *    argc, The number of command line arguments
 *    argv, Array of strings containing the command line arguments
 * Return values :
 *    NONE
 * History :
 *    K.Y.   07/09/2001, Initial typing.
 * Note :
 ******************************************************************/
int main(int argc, char** argv)
{
  db*               pTraceDB;             /* The trace database */
  int               lReadResult;          /* Result of reading the trace file */
  int               lDecodedDataFile = 0; /* File containing the human readable data */
  int               lTraceDataFile = 0;   /* The file to which tracing data WAS dumped */
  systemInfo*       pSystem;              /* Full description of system during trace */
  options*          pOptions;             /* Parsed command line options */
  FILE*             lProcDataFile = NULL; /* File containing /proc information */

  /* Initialize variables used */
  pTraceDB = NULL;
  pSystem  = NULL;
  pOptions = NULL;

  /* Allocate space for options */
  pOptions = CreateOptions();

  /* How many parameters do we have? (reader tracefile procfile dumpfile) */
  if((argc < 3) || (argc > 4))
    {
    printf("TraceReader: Wrong number of command-line arguments \n");
    printf("TraceReader: Usage => tracereader tracefile procfile [dumpfile] \n");
    exit(1);
    }

  /* Copy the trace file name */
  pOptions->InputFileName = (char*) malloc(strlen(argv[1]) + 1);
  strcpy(pOptions->InputFileName, argv[1]);

  /* Copy the proc file name */
  pOptions->ProcFileName = (char*) malloc(strlen(argv[2]) + 1);
  strcpy(pOptions->ProcFileName, argv[2]);

  /* Are we dumping to an output file? */
  if(argc == 4)
    {
    /* Copy the output file name */
    pOptions->OutputFileName = (char*) malloc(strlen(argv[3]) + 1);
    strcpy(pOptions->OutputFileName, argv[3]);
    }

  /* Open the input file */
  if((lTraceDataFile = open(pOptions->InputFileName, O_RDONLY, 0)) < 0)
    {
    /* File ain't good */
    printf("Unable to open input data file \n");
    exit(1);
    }
  
  /* Open the /proc information file */
  if((lProcDataFile = fopen(pOptions->ProcFileName, "r")) == NULL)
    {
    /* File ain't good */
    printf("Unable to open /proc information file \n");
    close(lTraceDataFile);
    exit(1);
    }

  /* Are we dumping to an output file? */
  if(argc == 4)
    {
    /* Open the output file */
    if((lDecodedDataFile = creat(pOptions->OutputFileName, S_IRUSR| S_IWUSR | S_IRGRP | S_IROTH)) < 0)
      {
      /* File ain't good */
      printf("Unable to open output file \n");
      close(lTraceDataFile);
      fclose(lProcDataFile);
      exit(1);
      }
    }

  /* Create database and system structures */
  pTraceDB = DBCreateDB();
  pSystem  = DBCreateSystem();

  /* Do the read proper */
  lReadResult = DBReadTrace(lTraceDataFile, pTraceDB);

  /* Was there any problem reading the trace */
  if(lReadResult != 0)
    {
    /* Depending on the type of error */
    switch(lReadResult)
      {
      case 1:
      default:
	/* Something's wrong with the input file */
	printf("\nInput file isn't a valid trace file \n");
	break;

      case 2:
	/* Trace file format version not supported */
	printf("\nTrace file format version %d.%d not supported by visualizer \n",
	       pTraceDB->MajorVersion,
	       pTraceDB->MinorVersion);
	break;
      }

    /* Stop here */
    exit(1);
    }

  /* Process the information in the /proc information file */
  DBProcessProcInfo(lProcDataFile, pSystem);

  /* Close the /proc info file */
  fclose(lProcDataFile);

  /* Process the database according to selected options */
  DBProcessTrace(pSystem, pTraceDB, pOptions);

#if 1 /* Toggle this binary flag to enable or disable custom code */
  /* Call custom operations routine */
  CustomOperations(pSystem, pTraceDB, pOptions);
#endif

  /* Are we dumping to an output file? */
  if(argc == 4)
    {
    /* Depending on the type of system */
    switch(pTraceDB->SystemType)
      {
      /* Plain Linux */
      case TRACE_SYS_TYPE_VANILLA_LINUX:
	/* Print out the data-base and add details requested at the command-line */
	DBPrintTrace(lDecodedDataFile, pSystem, pTraceDB, pOptions);
	break;

#if SUPP_RTAI
      /* RTAI/Linux system */
      case TRACE_SYS_TYPE_RTAI_LINUX:
	/* Print out the data-base and add details requested at the command-line */
	RTAIDBPrintTrace(lDecodedDataFile, pSystem, pTraceDB, pOptions);
#endif /* SUPP_RTAI */
      }

    /* Close the output */
    close(lDecodedDataFile);
    printf("Output has been written to %s \n", pOptions->OutputFileName);
    }

  /* Close the input file */
  if(pOptions->InputFileName != NULL)
    close(lTraceDataFile);

  /* We're outa here */
  exit(0);
}