source: lib/format_helper.c @ cab58c5

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since cab58c5 was 33d83d4, checked in by Perry Lorier <perry@…>, 15 years ago

Fixed up brazillions of warnings

  • Property mode set to 100644
File size: 7.2 KB
Line 
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 "config.h"
32#include <sys/types.h>
33#include <fcntl.h> /* for O_LARGEFILE */
34#include "libtrace.h"
35#include "libtrace_int.h"
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <errno.h>
41#include "format_helper.h"
42
43#include <assert.h>
44#include <stdarg.h>
45
46#ifdef WIN32
47#  include <io.h>
48#  include <share.h>
49#  define snprintf sprintf_s
50
51struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
52    struct libtrace_eventobj_t event = {0,0,0.0,0};
53
54    trace_set_err(trace,TRACE_ERR_OPTION_UNAVAIL, "trace_event() is not "
55            "supported on devices under windows in this version");
56
57    event.type = TRACE_EVENT_TERMINATE;
58    return event;
59}
60#else
61#  include <sys/ioctl.h>
62
63struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, 
64                                        struct libtrace_packet_t *packet) {
65        struct libtrace_eventobj_t event = {0,0,0.0,0};
66        int data;
67
68        assert(trace != NULL);
69        assert(packet != NULL);
70       
71        if (trace->format->get_fd) {
72                event.fd = trace->format->get_fd(trace);
73        } else {
74                event.fd = 0;
75        }
76        if (ioctl(event.fd,FIONREAD,&data)==-1) {
77                event.type = TRACE_EVENT_TERMINATE;
78                return event;
79        } 
80               
81        if (data>0) {
82                event.size = trace_read_packet(trace,packet);
83                event.type = TRACE_EVENT_PACKET;
84                return event;
85        }
86        event.type= TRACE_EVENT_IOWAIT;
87        return event;
88}
89#endif
90
91struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
92        struct libtrace_eventobj_t event = {0,0,0.0,0};
93        double ts;
94        double now;
95        struct timeval stv;
96
97        if (!trace->event.packet) {
98                trace->event.packet = trace_create_packet();
99                trace->event.psize=
100                        trace_read_packet(trace,trace->event.packet);
101                if (trace->event.psize<1) {
102                        /* return here, the test for
103                         * event.size will sort out the error
104                         */
105                        event.type = TRACE_EVENT_TERMINATE;
106                        return event;
107                }
108        }
109
110        ts=trace_get_seconds(trace->event.packet);
111        if (trace->event.tdelta!=0.0) {
112                /* Get the adjusted current time */
113                gettimeofday(&stv, NULL);
114                now = stv.tv_sec + 
115                        ((double)stv.tv_usec / 1000000.0);
116                /* adjust for trace delta */
117                now -= trace->event.tdelta; 
118
119                /* if the trace timestamp is still in the
120                 * future, return a SLEEP event,
121                 * otherwise fire the packet
122                 */
123                if (ts > now) {
124                        event.seconds = ts - 
125                                trace->event.trace_last_ts;
126                        event.type = TRACE_EVENT_SLEEP;
127                        return event;
128                }
129        } else {
130                gettimeofday(&stv, NULL);
131                /* work out the difference between the
132                 * start of trace replay, and the first
133                 * packet in the trace
134                 */
135                trace->event.tdelta = stv.tv_sec + 
136                        ((double)stv.tv_usec / 1000000.0);
137                trace->event.tdelta -= ts;
138        }
139
140        /* This is the first packet, so just fire away. */
141        /* TODO: finalise packet */
142        *packet = *trace->event.packet;
143        trace_destroy_packet(trace->event.packet);
144        trace->event.packet = NULL;
145
146        event.type = TRACE_EVENT_PACKET;
147
148        trace->event.trace_last_ts = ts;
149
150        return event;
151}
152
153/* Catch undefined O_LARGEFILE on *BSD etc */
154#ifndef O_LARGEFILE
155#  define O_LARGEFILE 0
156#endif
157
158/* Catching O_BINARY on all sane OS's */
159#ifndef O_BINARY
160#  define O_BINARY 0
161#endif
162
163/* open a file or stdin using gzip compression if necessary (and supported)
164 * @internal
165 */
166libtrace_io_t *trace_open_file(libtrace_t *trace)
167{
168        int fd;
169        libtrace_io_t *ret;
170
171
172        if (strcmp(trace->uridata,"-")==0) {
173                ret=libtrace_io_fdopen(fileno(stdin),"rb");
174                return ret;
175        }
176
177        /* We open the file with open(2), so we can provide O_LARGEFILE
178         * as zlib doesn't always do it itself
179         */
180        fd=open(trace->uridata,O_LARGEFILE|O_RDONLY|O_BINARY);
181        if (fd==-1) {
182                trace_set_err(trace,errno,"Unable to open %s",trace->uridata);
183                return 0;
184        }
185        ret=libtrace_io_fdopen(fd,"rb");
186        return ret;
187}
188
189/* Create a file or write to stdout using compression if requested
190 * @internal
191 */
192libtrace_io_t *trace_open_file_out(libtrace_out_t *trace,int level, int fileflag)
193{
194        int fd;
195        libtrace_io_t *ret;
196        char filemode[4]; /* wb9\0 */
197        assert(level<10);
198        assert(level>=0);
199#ifdef HAVE_LIBZ
200        snprintf(filemode,sizeof(filemode),"wb%d",level);
201#else
202        snprintf(filemode,sizeof(filemode),"wb");
203#endif
204
205        if (strcmp(trace->uridata,"-")==0) {
206                ret=libtrace_io_fdopen(fileno(stdout),filemode);
207                return ret;
208        }
209
210        /* We open the file with open(2), so we can provide O_LARGEFILE
211         * as zlib doesn't always do it itself
212         */
213        fd=open(trace->uridata,fileflag|O_LARGEFILE|O_BINARY,0666);
214        if (fd==-1) {
215                trace_set_err_out(trace,
216                                errno,"Unable to open %s",trace->uridata);
217                return 0;
218        }
219        ret=libtrace_io_fdopen(fd,filemode);
220        if (!ret) {
221                printf("%s\n",filemode);
222                trace_set_err_out(trace,
223                                TRACE_ERR_INIT_FAILED,"gz out of memory");
224        }
225        return ret;
226}
227
228
229/** Update the libtrace error
230 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
231 * @param msg a plaintext error message
232 * @internal
233 */
234void trace_set_err(libtrace_t *trace,int errcode,const char *msg,...)
235{
236        char buf[256];
237        va_list va;
238        va_start(va,msg);
239        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
240        trace->err.err_num=errcode;
241        if (errcode>0) {
242                vsnprintf(buf,sizeof(buf),msg,va);
243                snprintf(trace->err.problem,sizeof(trace->err.problem),
244                                "%s: %s",buf,strerror(errcode));
245        } else {
246                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
247                                msg,va);
248        }
249        va_end(va);
250}
251
252/** Update the libtrace for output traces error
253 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
254 * @param msg a plaintext error message
255 * @internal
256 */
257void trace_set_err_out(libtrace_out_t *trace,int errcode,const char *msg,...)
258{
259        char buf[256];
260        va_list va;
261        va_start(va,msg);
262        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
263        trace->err.err_num=errcode;
264        if (errcode>0) {
265                vsnprintf(buf,sizeof(buf),msg,va);
266                snprintf(trace->err.problem,sizeof(trace->err.problem),
267                                "%s: %s",buf,strerror(errno));
268        } else {
269                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
270                                msg,va);
271        }
272        va_end(va);
273}
274
275uint64_t byteswap64(uint64_t num)
276{
277        return (byteswap32((num&0xFFFFFFFF00000000ULL)>>32))
278              |((uint64_t)byteswap32(num&0x00000000FFFFFFFFULL)<<32);
279}
280
281uint32_t byteswap32(uint32_t num)
282{
283        return ((num&0x000000FFU)<<24)
284                | ((num&0x0000FF00U)<<8)
285                | ((num&0x00FF0000U)>>8)
286                | ((num&0xFF000000U)>>24);
287}
288
289uint16_t byteswap16(uint16_t num)
290{
291        return ((num<<8)&0xFF00)|((num>>8)&0x00FF);
292}
293
Note: See TracBrowser for help on using the repository browser.