Uh oh!
There was an error while loading. Please reload this page.
forked from smihir/trace-cmd
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbuffer-parse.c
More file actions
Latest commit
743 lines (631 loc) · 17.1 KB
/
Copy pathkbuffer-parse.c
File metadata and controls
743 lines (631 loc) · 17.1 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*
* Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program 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 (not later!)
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"kbuffer.h"
#defineMISSING_EVENTS (1 << 31)
#defineMISSING_STORED (1 << 30)
#defineCOMMIT_MASK ((1 << 27) - 1)
enum {
KBUFFER_FL_HOST_BIG_ENDIAN= (1<<0),
KBUFFER_FL_BIG_ENDIAN= (1<<1),
KBUFFER_FL_LONG_8= (1<<2),
KBUFFER_FL_OLD_FORMAT= (1<<3),
};
#defineENDIAN_MASK (KBUFFER_FL_HOST_BIG_ENDIAN | KBUFFER_FL_BIG_ENDIAN)
/** kbuffer
* @timestamp - timestamp of current event
* @lost_events - # of lost events between this subbuffer and previous
* @flags - special flags of the kbuffer
* @subbuffer - pointer to the sub-buffer page
* @data - pointer to the start of data on the sub-buffer page
* @index - index from @data to the @curr event data
* @curr - offset from @data to the start of current event
* (includes metadata)
* @next - offset from @data to the start of next event
* @size - The size of data on @data
* @start - The offset from @subbuffer where @data lives
*
* @read_4 - Function to read 4 raw bytes (may swap)
* @read_8 - Function to read 8 raw bytes (may swap)
* @read_long - Function to read a long word (4 or 8 bytes with needed swap)
*/
structkbuffer {
unsigned long longtimestamp;
long longlost_events;
unsigned longflags;
void*subbuffer;
void*data;
unsigned intindex;
unsigned intcurr;
unsigned intnext;
unsigned intsize;
unsigned intstart;
unsigned int (*read_4)(void*ptr);
unsigned long long (*read_8)(void*ptr);
unsigned long long (*read_long)(structkbuffer*kbuf, void*ptr);
int (*next_event)(structkbuffer*kbuf);
};
staticvoid*zmalloc(size_tsize)
{
returncalloc(1, size);
}
staticinthost_is_bigendian(void)
{
unsigned charstr[] = { 0x1, 0x2, 0x3, 0x4 };
unsigned int*ptr;
ptr= (unsigned int*)str;
return*ptr==0x01020304;
}
staticintdo_swap(structkbuffer*kbuf)
{
return ((kbuf->flags&KBUFFER_FL_HOST_BIG_ENDIAN) +kbuf->flags) &
ENDIAN_MASK;
}
staticunsigned long long__read_8(void*ptr)
{
unsigned long longdata=*(unsigned long long*)ptr;
returndata;
}
staticunsigned long long__read_8_sw(void*ptr)
{
unsigned long longdata=*(unsigned long long*)ptr;
unsigned long longswap;
swap= ((data&0xffULL) << 56) |
((data& (0xffULL << 8)) << 40) |
((data& (0xffULL << 16)) << 24) |
((data& (0xffULL << 24)) << 8) |
((data& (0xffULL << 32)) >> 8) |
((data& (0xffULL << 40)) >> 24) |
((data& (0xffULL << 48)) >> 40) |
((data& (0xffULL << 56)) >> 56);
returnswap;
}
staticunsigned int__read_4(void*ptr)
{
unsigned intdata=*(unsigned int*)ptr;
returndata;
}
staticunsigned int__read_4_sw(void*ptr)
{
unsigned intdata=*(unsigned int*)ptr;
unsigned intswap;
swap= ((data&0xffULL) << 24) |
((data& (0xffULL << 8)) << 8) |
((data& (0xffULL << 16)) >> 8) |
((data& (0xffULL << 24)) >> 24);
returnswap;
}
staticunsigned long longread_8(structkbuffer*kbuf, void*ptr)
{
returnkbuf->read_8(ptr);
}
staticunsigned intread_4(structkbuffer*kbuf, void*ptr)
{
returnkbuf->read_4(ptr);
}
staticunsigned long long__read_long_8(structkbuffer*kbuf, void*ptr)
{
returnkbuf->read_8(ptr);
}
staticunsigned long long__read_long_4(structkbuffer*kbuf, void*ptr)
{
returnkbuf->read_4(ptr);
}
staticunsigned long longread_long(structkbuffer*kbuf, void*ptr)
{
returnkbuf->read_long(kbuf, ptr);
}
staticintcalc_index(structkbuffer*kbuf, void*ptr)
{
return (unsigned long)ptr- (unsigned long)kbuf->data;
}
staticint__next_event(structkbuffer*kbuf);
/**
* kbuffer_alloc - allocat a new kbuffer
* @size; enum to denote size of word
* @endian: enum to denote endianness
*
* Allocates and returns a new kbuffer.
*/
structkbuffer*
kbuffer_alloc(enumkbuffer_long_sizesize, enumkbuffer_endianendian)
{
structkbuffer*kbuf;
intflags=0;
switch (size) {
caseKBUFFER_LSIZE_4:
break;
caseKBUFFER_LSIZE_8:
flags |= KBUFFER_FL_LONG_8;
break;
default:
returnNULL;
}
switch (endian) {
caseKBUFFER_ENDIAN_LITTLE:
break;
caseKBUFFER_ENDIAN_BIG:
flags |= KBUFFER_FL_BIG_ENDIAN;
break;
default:
returnNULL;
}
kbuf=zmalloc(sizeof(*kbuf));
if (!kbuf)
returnNULL;
kbuf->flags=flags;
if (host_is_bigendian())
kbuf->flags |= KBUFFER_FL_HOST_BIG_ENDIAN;
if (do_swap(kbuf)) {
kbuf->read_8=__read_8_sw;
kbuf->read_4=__read_4_sw;
} else {
kbuf->read_8=__read_8;
kbuf->read_4=__read_4;
}
if (kbuf->flags&KBUFFER_FL_LONG_8)
kbuf->read_long=__read_long_8;
else
kbuf->read_long=__read_long_4;
/* May be changed by kbuffer_set_old_format() */
kbuf->next_event=__next_event;
returnkbuf;
}
/** kbuffer_free - free an allocated kbuffer
* @kbuf: The kbuffer to free
*
* Can take NULL as a parameter.
*/
voidkbuffer_free(structkbuffer*kbuf)
{
free(kbuf);
}
staticunsigned inttype4host(structkbuffer*kbuf,
unsigned inttype_len_ts)
{
if (kbuf->flags&KBUFFER_FL_BIG_ENDIAN)
return (type_len_ts >> 29) &3;
else
returntype_len_ts&3;
}
staticunsigned intlen4host(structkbuffer*kbuf,
unsigned inttype_len_ts)
{
if (kbuf->flags&KBUFFER_FL_BIG_ENDIAN)
return (type_len_ts >> 27) &7;
else
return (type_len_ts >> 2) &7;
}
staticunsigned inttype_len4host(structkbuffer*kbuf,
unsigned inttype_len_ts)
{
if (kbuf->flags&KBUFFER_FL_BIG_ENDIAN)
return (type_len_ts >> 27) & ((1 << 5) -1);
else
returntype_len_ts& ((1 << 5) -1);
}
staticunsigned intts4host(structkbuffer*kbuf,
unsigned inttype_len_ts)
{
if (kbuf->flags&KBUFFER_FL_BIG_ENDIAN)
returntype_len_ts& ((1 << 27) -1);
else
returntype_len_ts >> 5;
}
/*
* Linux 2.6.30 and earlier (not much ealier) had a different
* ring buffer format. It should be obsolete, but we handle it anyway.
*/
enumold_ring_buffer_type {
OLD_RINGBUF_TYPE_PADDING,
OLD_RINGBUF_TYPE_TIME_EXTEND,
OLD_RINGBUF_TYPE_TIME_STAMP,
OLD_RINGBUF_TYPE_DATA,
};
staticunsigned intold_update_pointers(structkbuffer*kbuf)
{
unsigned long longextend;
unsigned inttype_len_ts;
unsigned inttype;
unsigned intlen;
unsigned intdelta;
unsigned intlength;
void*ptr=kbuf->data+kbuf->curr;
type_len_ts=read_4(kbuf, ptr);
ptr+=4;
type=type4host(kbuf, type_len_ts);
len=len4host(kbuf, type_len_ts);
delta=ts4host(kbuf, type_len_ts);
switch (type) {
caseOLD_RINGBUF_TYPE_PADDING:
kbuf->next=kbuf->size;
return0;
caseOLD_RINGBUF_TYPE_TIME_EXTEND:
extend=read_4(kbuf, ptr);
extend <<= TS_SHIFT;
extend+=delta;
delta=extend;
ptr+=4;
break;
caseOLD_RINGBUF_TYPE_TIME_STAMP:
/* should never happen! */
kbuf->curr=kbuf->size;
kbuf->next=kbuf->size;
kbuf->index=kbuf->size;
return-1;
default:
if (len)
length=len*4;
else {
length=read_4(kbuf, ptr);
length-=4;
ptr+=4;
}
break;
}
kbuf->timestamp+=delta;
kbuf->index=calc_index(kbuf, ptr);
kbuf->next=kbuf->index+length;
returntype;
}
staticint__old_next_event(structkbuffer*kbuf)
{
inttype;
do {
kbuf->curr=kbuf->next;
if (kbuf->next >= kbuf->size)
return-1;
type=old_update_pointers(kbuf);
} while (type==OLD_RINGBUF_TYPE_TIME_EXTEND||type==OLD_RINGBUF_TYPE_PADDING);
return0;
}
staticunsigned int
translate_data(structkbuffer*kbuf, void*data, void**rptr,
unsigned long long*delta, int*length)
{
unsigned long longextend;
unsigned inttype_len_ts;
unsigned inttype_len;
type_len_ts=read_4(kbuf, data);
data+=4;
type_len=type_len4host(kbuf, type_len_ts);
*delta=ts4host(kbuf, type_len_ts);
switch (type_len) {
caseKBUFFER_TYPE_PADDING:
*length=read_4(kbuf, data);
data+=*length;
break;
caseKBUFFER_TYPE_TIME_EXTEND:
extend=read_4(kbuf, data);
data+=4;
extend <<= TS_SHIFT;
extend+=*delta;
*delta=extend;
*length=0;
break;
caseKBUFFER_TYPE_TIME_STAMP:
data+=12;
*length=0;
break;
case0:
*length=read_4(kbuf, data) -4;
*length= (*length+3) & ~3;
data+=4;
break;
default:
*length=type_len*4;
break;
}
*rptr=data;
returntype_len;
}
staticunsigned intupdate_pointers(structkbuffer*kbuf)
{
unsigned long longdelta;
unsigned inttype_len;
intlength;
void*ptr=kbuf->data+kbuf->curr;
type_len=translate_data(kbuf, ptr, &ptr, &delta, &length);
kbuf->timestamp+=delta;
kbuf->index=calc_index(kbuf, ptr);
kbuf->next=kbuf->index+length;
returntype_len;
}
/**
* kbuffer_translate_data - read raw data to get a record
* @swap: Set to 1 if bytes in words need to be swapped when read
* @data: The raw data to read
* @size: Address to store the size of the event data.
*
* Returns a pointer to the event data. To determine the entire
* record size (record metadata + data) just add the difference between
* @data and the returned value to @size.
*/
void*kbuffer_translate_data(intswap, void*data, unsigned int*size)
{
unsigned long longdelta;
structkbufferkbuf;
inttype_len;
intlength;
void*ptr;
if (swap) {
kbuf.read_8=__read_8_sw;
kbuf.read_4=__read_4_sw;
kbuf.flags=host_is_bigendian() ? 0 : KBUFFER_FL_BIG_ENDIAN;
} else {
kbuf.read_8=__read_8;
kbuf.read_4=__read_4;
kbuf.flags=host_is_bigendian() ? KBUFFER_FL_BIG_ENDIAN: 0;
}
type_len=translate_data(&kbuf, data, &ptr, &delta, &length);
switch (type_len) {
caseKBUFFER_TYPE_PADDING:
caseKBUFFER_TYPE_TIME_EXTEND:
caseKBUFFER_TYPE_TIME_STAMP:
returnNULL;
};
*size=length;
returnptr;
}
staticint__next_event(structkbuffer*kbuf)
{
inttype;
do {
kbuf->curr=kbuf->next;
if (kbuf->next >= kbuf->size)
return-1;
type=update_pointers(kbuf);
} while (type==KBUFFER_TYPE_TIME_EXTEND||type==KBUFFER_TYPE_PADDING);
return0;
}
staticintnext_event(structkbuffer*kbuf)
{
returnkbuf->next_event(kbuf);
}
/**
* kbuffer_next_event - increment the current pointer
* @kbuf: The kbuffer to read
* @ts: Address to store the next record's timestamp (may be NULL to ignore)
*
* Increments the pointers into the subbuffer of the kbuffer to point to the
* next event so that the next kbuffer_read_event() will return a
* new event.
*
* Returns the data of the next event if a new event exists on the subbuffer,
* NULL otherwise.
*/
void*kbuffer_next_event(structkbuffer*kbuf, unsigned long long*ts)
{
intret;
if (!kbuf|| !kbuf->subbuffer)
returnNULL;
ret=next_event(kbuf);
if (ret<0)
returnNULL;
if (ts)
*ts=kbuf->timestamp;
returnkbuf->data+kbuf->index;
}
/**
* kbuffer_load_subbuffer - load a new subbuffer into the kbuffer
* @kbuf: The kbuffer to load
* @subbuffer: The subbuffer to load into @kbuf.
*
* Load a new subbuffer (page) into @kbuf. This will reset all
* the pointers and update the @kbuf timestamp. The next read will
* return the first event on @subbuffer.
*
* Returns 0 on succes, -1 otherwise.
*/
intkbuffer_load_subbuffer(structkbuffer*kbuf, void*subbuffer)
{
unsigned long longflags;
void*ptr=subbuffer;
if (!kbuf|| !subbuffer)
return-1;
kbuf->subbuffer=subbuffer;
kbuf->timestamp=read_8(kbuf, ptr);
ptr+=8;
kbuf->curr=0;
if (kbuf->flags&KBUFFER_FL_LONG_8)
kbuf->start=16;
else
kbuf->start=12;
kbuf->data=subbuffer+kbuf->start;
flags=read_long(kbuf, ptr);
kbuf->size= (unsigned int)flags&COMMIT_MASK;
if (flags&MISSING_EVENTS) {
if (flags&MISSING_STORED) {
ptr=kbuf->data+kbuf->size;
kbuf->lost_events=read_long(kbuf, ptr);
} else
kbuf->lost_events=-1;
} else
kbuf->lost_events=0;
kbuf->index=0;
kbuf->next=0;
next_event(kbuf);
return0;
}
/**
* kbuffer_read_event - read the next event in the kbuffer subbuffer
* @kbuf: The kbuffer to read from
* @ts: The address to store the timestamp of the event (may be NULL to ignore)
*
* Returns a pointer to the data part of the current event.
* NULL if no event is left on the subbuffer.
*/
void*kbuffer_read_event(structkbuffer*kbuf, unsigned long long*ts)
{
if (!kbuf|| !kbuf->subbuffer)
returnNULL;
if (kbuf->curr >= kbuf->size)
returnNULL;
if (ts)
*ts=kbuf->timestamp;
returnkbuf->data+kbuf->index;
}
/**
* kbuffer_timestamp - Return the timestamp of the current event
* @kbuf: The kbuffer to read from
*
* Returns the timestamp of the current (next) event.
*/
unsigned long longkbuffer_timestamp(structkbuffer*kbuf)
{
returnkbuf->timestamp;
}
/**
* kbuffer_read_at_offset - read the event that is at offset
* @kbuf: The kbuffer to read from
* @offset: The offset into the subbuffer
* @ts: The address to store the timestamp of the event (may be NULL to ignore)
*
* The @offset must be an index from the @kbuf subbuffer beginning.
* If @offset is bigger than the stored subbuffer, NULL will be returned.
*
* Returns the data of the record that is at @offset. Note, @offset does
* not need to be the start of the record, the offset just needs to be
* in the record (or beginning of it).
*
* Note, the kbuf timestamp and pointers are updated to the
* returned record. That is, kbuffer_read_event() will return the same
* data and timestamp, and kbuffer_next_event() will increment from
* this record.
*/
void*kbuffer_read_at_offset(structkbuffer*kbuf, intoffset,
unsigned long long*ts)
{
void*data;
if (offset<kbuf->start)
offset=0;
else
offset-=kbuf->start;
/* Reset the buffer */
kbuffer_load_subbuffer(kbuf, kbuf->subbuffer);
while (kbuf->curr<offset) {
data=kbuffer_next_event(kbuf, ts);
if (!data)
break;
}
returndata;
}
/**
* kbuffer_subbuffer_size - the size of the loaded subbuffer
* @kbuf: The kbuffer to read from
*
* Returns the size of the subbuffer. Note, this size is
* where the last event resides. The stored subbuffer may actually be
* bigger due to padding and such.
*/
intkbuffer_subbuffer_size(structkbuffer*kbuf)
{
returnkbuf->size;
}
/**
* kbuffer_curr_index - Return the index of the record
* @kbuf: The kbuffer to read from
*
* Returns the index from the start of the data part of
* the subbuffer to the current location. Note this is not
* from the start of the subbuffer. An index of zero will
* point to the first record. Use kbuffer_curr_offset() for
* the actually offset (that can be used by kbuffer_read_at_offset())
*/
intkbuffer_curr_index(structkbuffer*kbuf)
{
returnkbuf->curr;
}
/**
* kbuffer_curr_offset - Return the offset of the record
* @kbuf: The kbuffer to read from
*
* Returns the offset from the start of the subbuffer to the
* current location.
*/
intkbuffer_curr_offset(structkbuffer*kbuf)
{
returnkbuf->curr+kbuf->start;
}
/**
* kbuffer_event_size - return the size of the event data
* @kbuf: The kbuffer to read
*
* Returns the size of the event data (the payload not counting
* the meta data of the record) of the current event.
*/
intkbuffer_event_size(structkbuffer*kbuf)
{
returnkbuf->next-kbuf->index;
}
/**
* kbuffer_curr_size - return the size of the entire record
* @kbuf: The kbuffer to read
*
* Returns the size of the entire record (meta data and payload)
* of the current event.
*/
intkbuffer_curr_size(structkbuffer*kbuf)
{
returnkbuf->next-kbuf->curr;
}
/**
* kbuffer_missed_events - return the # of missed events from last event.
* @kbuf: The kbuffer to read from
*
* Returns the # of missed events (if recorded) before the current
* event. Note, only events on the beginning of a subbuffer can
* have missed events, all other events within the buffer will be
* zero.
*/
intkbuffer_missed_events(structkbuffer*kbuf)
{
/* Only the first event can have missed events */
if (kbuf->curr)
return0;
returnkbuf->lost_events;
}
/**
* kbuffer_set_old_forma - set the kbuffer to use the old format parsing
* @kbuf: The kbuffer to set
*
* This is obsolete (or should be). The first kernels to use the
* new ring buffer had a slightly different ring buffer format
* (2.6.30 and earlier). It is still somewhat supported by kbuffer,
* but should not be counted on in the future.
*/
voidkbuffer_set_old_format(structkbuffer*kbuf)
{
kbuf->flags |= KBUFFER_FL_OLD_FORMAT;
kbuf->next_event=__old_next_event;
}
/**
* kbuffer_start_of_data - return offset of where data starts on subbuffer
* @kbuf: The kbuffer
*
* Returns the location on the subbuffer where the data starts.
*/
intkbuffer_start_of_data(structkbuffer*kbuf)
{
returnkbuf->start;
}