File: UserTrace.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 (381 lines) | stat: -rw-r--r-- 11,326 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
   UserTrace.c : User event tracing libary.
   Copyright (C) 2001 Karim Yaghmour.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation;
    version 2.1 of the License.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   History :
     K.Y., 01/12/2001, Initial typing.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>

#include <LTTTypes.h>
#include <DevCommand.h>
#include <UserTrace.h>

#define USER_TRACE_DEV_CLOSED    -1

 /* Global variables */
int gTraceDev = USER_TRACE_DEV_CLOSED;    /* File descriptor to tracing driver */

/******************************************************************
 * Function :
 *    trace_attach()
 * Description :
 *    Attach to trace device.
 * Parameters :
 *    NONE.
 * Return values :
 *    0, if everything went OK.
 *    Error code from "open" operation.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_attach(void)
{
  int lRetValue = 0;     /* Function's return value */

  /* Open the tracing device */
  if((gTraceDev = open("/dev/tracerU", O_RDONLY, 0)) < 0)
    {
    lRetValue = gTraceDev;
    gTraceDev = USER_TRACE_DEV_CLOSED;
    }

  /* Everything is OK */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_detach()
 * Description :
 *    Detach from trace device.
 * Parameters :
 *    NONE.
 * Return values :
 *    0, if everything went OK.
 *    -1, Device wasn't attached to begin with.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_detach(void)
{
  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Device has now been closed */
  gTraceDev = USER_TRACE_DEV_CLOSED;

  /* Close trace device */
  close(gTraceDev);

  /* Everything is OK */
  return 0;
}

/******************************************************************
 * Function :
 *    trace_create_event()
 * Description :
 *    Create a new event.
 * Parameters :
 *    pmEventType, string describing event type
 *    pmEventDesc, string used for standard formatting
 *    pmFormatType, type of formatting used to log event
 *                  data
 *    pmFormatData, data specific to format
 *    pmEventID, the ID of the event created.
 * Return values :
 *    Event ID on success,
 *    Device error code otherwise (< 0).
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_create_event(char*            pmEventType,
		       char*            pmEventDesc,
		       int              pmFormatType,
		       char*            pmFormatData)
{
  int                         lRetValue = 0;     /* Function's return value */
  tracer_create_user_event    lCreateUserEvent;  /* Create user event information */

  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Set basic event properties */
  if(pmEventType != NULL)
    strncpy(lCreateUserEvent.type, pmEventType, CUSTOM_EVENT_TYPE_STR_LEN);
  if(pmEventDesc != NULL)
    strncpy(lCreateUserEvent.desc, pmEventDesc, CUSTOM_EVENT_DESC_STR_LEN);
  if(pmFormatData != NULL)
    strncpy(lCreateUserEvent.form, pmFormatData, CUSTOM_EVENT_FORM_STR_LEN);

  /* Ensure that strings are bound */
  lCreateUserEvent.type[CUSTOM_EVENT_TYPE_STR_LEN - 1] = '\0';
  lCreateUserEvent.desc[CUSTOM_EVENT_DESC_STR_LEN - 1] = '\0';
  lCreateUserEvent.form[CUSTOM_EVENT_FORM_STR_LEN - 1] = '\0';

  /* Set format type */
  lCreateUserEvent.format_type = pmFormatType;

  /* Send the command to the device */
  lRetValue = ioctl(gTraceDev, TRACER_CREATE_USER_EVENT, &lCreateUserEvent);

  /* Set the event ID if operation was successful*/
  if(lRetValue == 0)
    return lCreateUserEvent.id;

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_destroy_event()
 * Description :
 *    Destroy an event.
 * Parameters :
 *    pmEventID, the Id returned by trace_create_event()
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_destroy_event(int pmEventID)
{
  int lRetValue = 0;     /* Function's return value */

  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Tell the device to destroy the event */
  lRetValue = ioctl(gTraceDev, TRACER_DESTROY_USER_EVENT, pmEventID);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_user_event()
 * Description :
 *    Trace a user event.
 * Parameters :
 *    pmEventID, the event ID provided upon creation.
 *    pmEventSize, the size of the data provided.
 *    pmEventData, data buffer describing event.
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_user_event(int pmEventID, int pmEventSize, void* pmEventData)
{
  int                       lRetValue = 0; /* Function's return value */
  tracer_trace_user_event   lUserEvent;    /* The user's event */

  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Set custom event Id */
  lUserEvent.id = pmEventID;

  /* Set the data size */
  if(pmEventSize <= CUSTOM_EVENT_MAX_SIZE)
    lUserEvent.data_size = (uint32_t) pmEventSize;
  else
    lUserEvent.data_size = (uint32_t) CUSTOM_EVENT_MAX_SIZE;

  /* Set the pointer to the event data */
  lUserEvent.data = pmEventData;
  
  /* Send the command to the device */
  lRetValue = ioctl(gTraceDev, TRACER_TRACE_USER_EVENT, &lUserEvent);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_set_event_mask()
 * Description :
 *    Set the event mask.
 * Parameters :
 *    pmEventMask, Event mask to be set.
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_set_event_mask(trace_event_mask pmEventMask)
{
  int lRetValue = 0;     /* Function's return value */

  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Send the command to the device */
  lRetValue = ioctl(gTraceDev, TRACER_SET_EVENT_MASK, &pmEventMask);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_get_event_mask()
 * Description :
 *    Get the event mask.
 * Parameters :
 *    pmEventMask, Pointer to variable receiving the event mask.
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_get_event_mask(trace_event_mask* pmEventMask)
{
  int lRetValue = 0;     /* Function's return value */

  /* Is the trace device open? */
  if(gTraceDev == USER_TRACE_DEV_CLOSED)
    return -1;

  /* Send the command to the device */
  lRetValue = ioctl(gTraceDev, TRACER_GET_EVENT_MASK, pmEventMask);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_enable_event_trace()
 * Description :
 *    Enable the tracing of a certain event.
 * Parameters :
 *    pmEventID, Event ID who's tracing is to be enabled.
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_enable_event_trace(int pmEventID)
{
  int               lRetValue = 0;     /* Function's return value */
  trace_event_mask  lEventMask;        /* The event trace mask */

  /* Get the current event mask */
  if(trace_get_event_mask(&lEventMask) < 0)
    return -1;

  /* Set the event's bit to enable tracing */
  ltt_set_bit(pmEventID, &lEventMask);

  /* Set the event mask */
  lRetValue = trace_set_event_mask(lEventMask);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_disable_event_trace()
 * Description :
 *    Disable the tracing of a certain event.
 * Parameters :
 *    pmEventID, Event ID who's tracing is to be disabled.
 * Return values :
 *    0, if everything went OK.
 *    Device error code otherwise.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_disable_event_trace(int pmEventID)
{
  int               lRetValue = 0;     /* Function's return value */
  trace_event_mask  lEventMask;        /* The event trace mask */

  /* Get the current event mask */
  if(trace_get_event_mask(&lEventMask) < 0)
    return -1;

  /* Set the event's bit to enable tracing */
  ltt_clear_bit(pmEventID, &lEventMask);

  /* Set the event mask */
  lRetValue = trace_set_event_mask(lEventMask);

  /* Tell the caller about his operation's status */
  return lRetValue;
}

/******************************************************************
 * Function :
 *    trace_is_event_traced()
 * Description :
 *    Check if a certain event is being traced.
 * Parameters :
 *    pmEventID, Event ID to be checked for tracing.
 * Return values :
 *    1, if event is being traced.
 *    0, if event is not being traced.
 * History :
 *    K.Y., 04/12/2001. Initial typing.
 * Note :
 ******************************************************************/
int trace_is_event_traced(int pmEventID)
{
  int               lRetValue = 0;     /* Function's return value */
  trace_event_mask  lEventMask;        /* The event trace mask */

  /* Get the current event mask */
  if(trace_get_event_mask(&lEventMask) < 0)
    return -1;

  /* Set the event's bit to enable tracing */
  lRetValue = ltt_test_bit(pmEventID, &lEventMask);

  /* Tell the caller about his operation's status */
  return lRetValue;
}