1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | #include "libtrace.h" |
---|
32 | #include "libtrace_int.h" |
---|
33 | |
---|
34 | #ifdef HAVE_INTTYPES_H |
---|
35 | # include <inttypes.h> |
---|
36 | #else |
---|
37 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
38 | #endif |
---|
39 | #include "format_helper.h" |
---|
40 | |
---|
41 | #include <sys/ioctl.h> |
---|
42 | /* |
---|
43 | int trace_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
44 | int numbytes; |
---|
45 | static short lctr = 0; |
---|
46 | int rlen; |
---|
47 | assert(libtrace); |
---|
48 | assert(len >= 0); |
---|
49 | |
---|
50 | if (buffer == 0) |
---|
51 | buffer = malloc(len); |
---|
52 | |
---|
53 | while(1) { |
---|
54 | switch(libtrace->sourcetype) { |
---|
55 | case DEVICE: |
---|
56 | if ((numbytes=read(INPUT.fd, |
---|
57 | buffer, |
---|
58 | len)) == -1) { |
---|
59 | perror("read"); |
---|
60 | return -1; |
---|
61 | } |
---|
62 | break; |
---|
63 | default: |
---|
64 | #if HAVE_ZLIB |
---|
65 | if ((numbytes=gzread(INPUT.file, |
---|
66 | buffer, |
---|
67 | len)) == -1) { |
---|
68 | perror("gzread"); |
---|
69 | return -1; |
---|
70 | } |
---|
71 | #else |
---|
72 | if ((numbytes=fread(buffer,len,1, |
---|
73 | INPUT.file)) == 0 ) { |
---|
74 | if(feof(INPUT.file)) { |
---|
75 | return 0; |
---|
76 | } |
---|
77 | if(ferror(INPUT.file)) { |
---|
78 | perror("fread"); |
---|
79 | return -1; |
---|
80 | } |
---|
81 | return 0; |
---|
82 | } |
---|
83 | #endif |
---|
84 | } |
---|
85 | break; |
---|
86 | } |
---|
87 | return numbytes; |
---|
88 | |
---|
89 | } |
---|
90 | */ |
---|
91 | |
---|
92 | struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
93 | struct libtrace_eventobj_t event; |
---|
94 | int data; |
---|
95 | |
---|
96 | if (packet->trace->format->get_fd) { |
---|
97 | event.fd = packet->trace->format->get_fd(packet); |
---|
98 | } else { |
---|
99 | event.fd = 0; |
---|
100 | } |
---|
101 | if (ioctl(event.fd,FIONREAD,&data)==-1) { |
---|
102 | perror("ioctl(FIONREAD)"); |
---|
103 | } |
---|
104 | if (data>0) { |
---|
105 | event.size = trace_read_packet(trace,packet); |
---|
106 | event.type = TRACE_EVENT_PACKET; |
---|
107 | return event; |
---|
108 | } |
---|
109 | event.type= TRACE_EVENT_IOWAIT; |
---|
110 | return event; |
---|
111 | } |
---|
112 | |
---|
113 | struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
114 | struct libtrace_eventobj_t event; |
---|
115 | double ts; |
---|
116 | double now; |
---|
117 | struct timeval stv; |
---|
118 | |
---|
119 | if (!trace->event.packet.buffer) { |
---|
120 | trace->event.packet.buffer = malloc(4096); |
---|
121 | trace->event.packet.size= |
---|
122 | trace_read_packet(trace,packet); |
---|
123 | event.size = trace->event.packet.size; |
---|
124 | if (trace->event.packet.size > 0 ) { |
---|
125 | memcpy(trace->event.packet.buffer, |
---|
126 | packet->buffer, |
---|
127 | trace->event.packet.size); |
---|
128 | } else { |
---|
129 | // return here, the test for |
---|
130 | // event.size will sort out the error |
---|
131 | event.type = TRACE_EVENT_PACKET; |
---|
132 | return event; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | ts=trace_get_seconds(packet); |
---|
137 | if (trace->event.tdelta!=0) { |
---|
138 | // Get the adjusted current time |
---|
139 | gettimeofday(&stv, NULL); |
---|
140 | now = stv.tv_sec + |
---|
141 | ((double)stv.tv_usec / 1000000.0); |
---|
142 | // adjust for trace delta |
---|
143 | now -= trace->event.tdelta; |
---|
144 | |
---|
145 | //if the trace timestamp is still in the |
---|
146 | //future, return a SLEEP event, |
---|
147 | //otherwise fire the packet |
---|
148 | if (ts > now) { |
---|
149 | event.seconds = ts - |
---|
150 | trace->event.trace_last_ts; |
---|
151 | event.type = TRACE_EVENT_SLEEP; |
---|
152 | return event; |
---|
153 | } |
---|
154 | } else { |
---|
155 | gettimeofday(&stv, NULL); |
---|
156 | // work out the difference between the |
---|
157 | // start of trace replay, and the first |
---|
158 | // packet in the trace |
---|
159 | trace->event.tdelta = stv.tv_sec + |
---|
160 | ((double)stv.tv_usec / 1000000.0); |
---|
161 | trace->event.tdelta -= ts; |
---|
162 | } |
---|
163 | |
---|
164 | // This is the first packet, so just fire away. |
---|
165 | packet->size = trace->event.packet.size; |
---|
166 | memcpy(packet->buffer, |
---|
167 | trace->event.packet.buffer, |
---|
168 | trace->event.packet.size); |
---|
169 | free(trace->event.packet.buffer); |
---|
170 | trace->event.packet.buffer = 0; |
---|
171 | event.type = TRACE_EVENT_PACKET; |
---|
172 | |
---|
173 | trace->event.trace_last_ts = ts; |
---|
174 | |
---|
175 | return event; |
---|
176 | |
---|
177 | } |
---|