kbuffer-parse.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "kbuffer.h"
  10. #define MISSING_EVENTS (1UL << 31)
  11. #define MISSING_STORED (1UL << 30)
  12. #define COMMIT_MASK ((1 << 27) - 1)
  13. enum {
  14. KBUFFER_FL_HOST_BIG_ENDIAN = (1<<0),
  15. KBUFFER_FL_BIG_ENDIAN = (1<<1),
  16. KBUFFER_FL_LONG_8 = (1<<2),
  17. KBUFFER_FL_OLD_FORMAT = (1<<3),
  18. };
  19. #define ENDIAN_MASK (KBUFFER_FL_HOST_BIG_ENDIAN | KBUFFER_FL_BIG_ENDIAN)
  20. /** kbuffer
  21. * @timestamp - timestamp of current event
  22. * @lost_events - # of lost events between this subbuffer and previous
  23. * @flags - special flags of the kbuffer
  24. * @subbuffer - pointer to the sub-buffer page
  25. * @data - pointer to the start of data on the sub-buffer page
  26. * @index - index from @data to the @curr event data
  27. * @curr - offset from @data to the start of current event
  28. * (includes metadata)
  29. * @next - offset from @data to the start of next event
  30. * @size - The size of data on @data
  31. * @start - The offset from @subbuffer where @data lives
  32. *
  33. * @read_4 - Function to read 4 raw bytes (may swap)
  34. * @read_8 - Function to read 8 raw bytes (may swap)
  35. * @read_long - Function to read a long word (4 or 8 bytes with needed swap)
  36. */
  37. struct kbuffer {
  38. unsigned long long timestamp;
  39. long long lost_events;
  40. unsigned long flags;
  41. void *subbuffer;
  42. void *data;
  43. unsigned int index;
  44. unsigned int curr;
  45. unsigned int next;
  46. unsigned int size;
  47. unsigned int start;
  48. unsigned int (*read_4)(void *ptr);
  49. unsigned long long (*read_8)(void *ptr);
  50. unsigned long long (*read_long)(struct kbuffer *kbuf, void *ptr);
  51. int (*next_event)(struct kbuffer *kbuf);
  52. };
  53. static void *zmalloc(size_t size)
  54. {
  55. return calloc(1, size);
  56. }
  57. static int host_is_bigendian(void)
  58. {
  59. unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 };
  60. unsigned int *ptr;
  61. ptr = (unsigned int *)str;
  62. return *ptr == 0x01020304;
  63. }
  64. static int do_swap(struct kbuffer *kbuf)
  65. {
  66. return ((kbuf->flags & KBUFFER_FL_HOST_BIG_ENDIAN) + kbuf->flags) &
  67. ENDIAN_MASK;
  68. }
  69. static unsigned long long __read_8(void *ptr)
  70. {
  71. unsigned long long data = *(unsigned long long *)ptr;
  72. return data;
  73. }
  74. static unsigned long long __read_8_sw(void *ptr)
  75. {
  76. unsigned long long data = *(unsigned long long *)ptr;
  77. unsigned long long swap;
  78. swap = ((data & 0xffULL) << 56) |
  79. ((data & (0xffULL << 8)) << 40) |
  80. ((data & (0xffULL << 16)) << 24) |
  81. ((data & (0xffULL << 24)) << 8) |
  82. ((data & (0xffULL << 32)) >> 8) |
  83. ((data & (0xffULL << 40)) >> 24) |
  84. ((data & (0xffULL << 48)) >> 40) |
  85. ((data & (0xffULL << 56)) >> 56);
  86. return swap;
  87. }
  88. static unsigned int __read_4(void *ptr)
  89. {
  90. unsigned int data = *(unsigned int *)ptr;
  91. return data;
  92. }
  93. static unsigned int __read_4_sw(void *ptr)
  94. {
  95. unsigned int data = *(unsigned int *)ptr;
  96. unsigned int swap;
  97. swap = ((data & 0xffULL) << 24) |
  98. ((data & (0xffULL << 8)) << 8) |
  99. ((data & (0xffULL << 16)) >> 8) |
  100. ((data & (0xffULL << 24)) >> 24);
  101. return swap;
  102. }
  103. static unsigned long long read_8(struct kbuffer *kbuf, void *ptr)
  104. {
  105. return kbuf->read_8(ptr);
  106. }
  107. static unsigned int read_4(struct kbuffer *kbuf, void *ptr)
  108. {
  109. return kbuf->read_4(ptr);
  110. }
  111. static unsigned long long __read_long_8(struct kbuffer *kbuf, void *ptr)
  112. {
  113. return kbuf->read_8(ptr);
  114. }
  115. static unsigned long long __read_long_4(struct kbuffer *kbuf, void *ptr)
  116. {
  117. return kbuf->read_4(ptr);
  118. }
  119. static unsigned long long read_long(struct kbuffer *kbuf, void *ptr)
  120. {
  121. return kbuf->read_long(kbuf, ptr);
  122. }
  123. static int calc_index(struct kbuffer *kbuf, void *ptr)
  124. {
  125. return (unsigned long)ptr - (unsigned long)kbuf->data;
  126. }
  127. static int __next_event(struct kbuffer *kbuf);
  128. /**
  129. * kbuffer_alloc - allocat a new kbuffer
  130. * @size; enum to denote size of word
  131. * @endian: enum to denote endianness
  132. *
  133. * Allocates and returns a new kbuffer.
  134. */
  135. struct kbuffer *
  136. kbuffer_alloc(enum kbuffer_long_size size, enum kbuffer_endian endian)
  137. {
  138. struct kbuffer *kbuf;
  139. int flags = 0;
  140. switch (size) {
  141. case KBUFFER_LSIZE_4:
  142. break;
  143. case KBUFFER_LSIZE_8:
  144. flags |= KBUFFER_FL_LONG_8;
  145. break;
  146. default:
  147. return NULL;
  148. }
  149. switch (endian) {
  150. case KBUFFER_ENDIAN_LITTLE:
  151. break;
  152. case KBUFFER_ENDIAN_BIG:
  153. flags |= KBUFFER_FL_BIG_ENDIAN;
  154. break;
  155. default:
  156. return NULL;
  157. }
  158. kbuf = zmalloc(sizeof(*kbuf));
  159. if (!kbuf)
  160. return NULL;
  161. kbuf->flags = flags;
  162. if (host_is_bigendian())
  163. kbuf->flags |= KBUFFER_FL_HOST_BIG_ENDIAN;
  164. if (do_swap(kbuf)) {
  165. kbuf->read_8 = __read_8_sw;
  166. kbuf->read_4 = __read_4_sw;
  167. } else {
  168. kbuf->read_8 = __read_8;
  169. kbuf->read_4 = __read_4;
  170. }
  171. if (kbuf->flags & KBUFFER_FL_LONG_8)
  172. kbuf->read_long = __read_long_8;
  173. else
  174. kbuf->read_long = __read_long_4;
  175. /* May be changed by kbuffer_set_old_format() */
  176. kbuf->next_event = __next_event;
  177. return kbuf;
  178. }
  179. /** kbuffer_free - free an allocated kbuffer
  180. * @kbuf: The kbuffer to free
  181. *
  182. * Can take NULL as a parameter.
  183. */
  184. void kbuffer_free(struct kbuffer *kbuf)
  185. {
  186. free(kbuf);
  187. }
  188. static unsigned int type4host(struct kbuffer *kbuf,
  189. unsigned int type_len_ts)
  190. {
  191. if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
  192. return (type_len_ts >> 29) & 3;
  193. else
  194. return type_len_ts & 3;
  195. }
  196. static unsigned int len4host(struct kbuffer *kbuf,
  197. unsigned int type_len_ts)
  198. {
  199. if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
  200. return (type_len_ts >> 27) & 7;
  201. else
  202. return (type_len_ts >> 2) & 7;
  203. }
  204. static unsigned int type_len4host(struct kbuffer *kbuf,
  205. unsigned int type_len_ts)
  206. {
  207. if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
  208. return (type_len_ts >> 27) & ((1 << 5) - 1);
  209. else
  210. return type_len_ts & ((1 << 5) - 1);
  211. }
  212. static unsigned int ts4host(struct kbuffer *kbuf,
  213. unsigned int type_len_ts)
  214. {
  215. if (kbuf->flags & KBUFFER_FL_BIG_ENDIAN)
  216. return type_len_ts & ((1 << 27) - 1);
  217. else
  218. return type_len_ts >> 5;
  219. }
  220. /*
  221. * Linux 2.6.30 and earlier (not much ealier) had a different
  222. * ring buffer format. It should be obsolete, but we handle it anyway.
  223. */
  224. enum old_ring_buffer_type {
  225. OLD_RINGBUF_TYPE_PADDING,
  226. OLD_RINGBUF_TYPE_TIME_EXTEND,
  227. OLD_RINGBUF_TYPE_TIME_STAMP,
  228. OLD_RINGBUF_TYPE_DATA,
  229. };
  230. static unsigned int old_update_pointers(struct kbuffer *kbuf)
  231. {
  232. unsigned long long extend;
  233. unsigned int type_len_ts;
  234. unsigned int type;
  235. unsigned int len;
  236. unsigned int delta;
  237. unsigned int length;
  238. void *ptr = kbuf->data + kbuf->curr;
  239. type_len_ts = read_4(kbuf, ptr);
  240. ptr += 4;
  241. type = type4host(kbuf, type_len_ts);
  242. len = len4host(kbuf, type_len_ts);
  243. delta = ts4host(kbuf, type_len_ts);
  244. switch (type) {
  245. case OLD_RINGBUF_TYPE_PADDING:
  246. kbuf->next = kbuf->size;
  247. return 0;
  248. case OLD_RINGBUF_TYPE_TIME_EXTEND:
  249. extend = read_4(kbuf, ptr);
  250. extend <<= TS_SHIFT;
  251. extend += delta;
  252. delta = extend;
  253. ptr += 4;
  254. length = 0;
  255. break;
  256. case OLD_RINGBUF_TYPE_TIME_STAMP:
  257. /* should never happen! */
  258. kbuf->curr = kbuf->size;
  259. kbuf->next = kbuf->size;
  260. kbuf->index = kbuf->size;
  261. return -1;
  262. default:
  263. if (len)
  264. length = len * 4;
  265. else {
  266. length = read_4(kbuf, ptr);
  267. length -= 4;
  268. ptr += 4;
  269. }
  270. break;
  271. }
  272. kbuf->timestamp += delta;
  273. kbuf->index = calc_index(kbuf, ptr);
  274. kbuf->next = kbuf->index + length;
  275. return type;
  276. }
  277. static int __old_next_event(struct kbuffer *kbuf)
  278. {
  279. int type;
  280. do {
  281. kbuf->curr = kbuf->next;
  282. if (kbuf->next >= kbuf->size)
  283. return -1;
  284. type = old_update_pointers(kbuf);
  285. } while (type == OLD_RINGBUF_TYPE_TIME_EXTEND || type == OLD_RINGBUF_TYPE_PADDING);
  286. return 0;
  287. }
  288. static unsigned int
  289. translate_data(struct kbuffer *kbuf, void *data, void **rptr,
  290. unsigned long long *delta, int *length)
  291. {
  292. unsigned long long extend;
  293. unsigned int type_len_ts;
  294. unsigned int type_len;
  295. type_len_ts = read_4(kbuf, data);
  296. data += 4;
  297. type_len = type_len4host(kbuf, type_len_ts);
  298. *delta = ts4host(kbuf, type_len_ts);
  299. switch (type_len) {
  300. case KBUFFER_TYPE_PADDING:
  301. *length = read_4(kbuf, data);
  302. break;
  303. case KBUFFER_TYPE_TIME_EXTEND:
  304. case KBUFFER_TYPE_TIME_STAMP:
  305. extend = read_4(kbuf, data);
  306. data += 4;
  307. extend <<= TS_SHIFT;
  308. extend += *delta;
  309. *delta = extend;
  310. *length = 0;
  311. break;
  312. case 0:
  313. *length = read_4(kbuf, data) - 4;
  314. *length = (*length + 3) & ~3;
  315. data += 4;
  316. break;
  317. default:
  318. *length = type_len * 4;
  319. break;
  320. }
  321. *rptr = data;
  322. return type_len;
  323. }
  324. static unsigned int update_pointers(struct kbuffer *kbuf)
  325. {
  326. unsigned long long delta;
  327. unsigned int type_len;
  328. int length;
  329. void *ptr = kbuf->data + kbuf->curr;
  330. type_len = translate_data(kbuf, ptr, &ptr, &delta, &length);
  331. if (type_len == KBUFFER_TYPE_TIME_STAMP)
  332. kbuf->timestamp = delta;
  333. else
  334. kbuf->timestamp += delta;
  335. kbuf->index = calc_index(kbuf, ptr);
  336. kbuf->next = kbuf->index + length;
  337. return type_len;
  338. }
  339. /**
  340. * kbuffer_translate_data - read raw data to get a record
  341. * @swap: Set to 1 if bytes in words need to be swapped when read
  342. * @data: The raw data to read
  343. * @size: Address to store the size of the event data.
  344. *
  345. * Returns a pointer to the event data. To determine the entire
  346. * record size (record metadata + data) just add the difference between
  347. * @data and the returned value to @size.
  348. */
  349. void *kbuffer_translate_data(int swap, void *data, unsigned int *size)
  350. {
  351. unsigned long long delta;
  352. struct kbuffer kbuf;
  353. int type_len;
  354. int length;
  355. void *ptr;
  356. if (swap) {
  357. kbuf.read_8 = __read_8_sw;
  358. kbuf.read_4 = __read_4_sw;
  359. kbuf.flags = host_is_bigendian() ? 0 : KBUFFER_FL_BIG_ENDIAN;
  360. } else {
  361. kbuf.read_8 = __read_8;
  362. kbuf.read_4 = __read_4;
  363. kbuf.flags = host_is_bigendian() ? KBUFFER_FL_BIG_ENDIAN: 0;
  364. }
  365. type_len = translate_data(&kbuf, data, &ptr, &delta, &length);
  366. switch (type_len) {
  367. case KBUFFER_TYPE_PADDING:
  368. case KBUFFER_TYPE_TIME_EXTEND:
  369. case KBUFFER_TYPE_TIME_STAMP:
  370. return NULL;
  371. }
  372. *size = length;
  373. return ptr;
  374. }
  375. static int __next_event(struct kbuffer *kbuf)
  376. {
  377. int type;
  378. do {
  379. kbuf->curr = kbuf->next;
  380. if (kbuf->next >= kbuf->size)
  381. return -1;
  382. type = update_pointers(kbuf);
  383. } while (type == KBUFFER_TYPE_TIME_EXTEND ||
  384. type == KBUFFER_TYPE_TIME_STAMP ||
  385. type == KBUFFER_TYPE_PADDING);
  386. return 0;
  387. }
  388. static int next_event(struct kbuffer *kbuf)
  389. {
  390. return kbuf->next_event(kbuf);
  391. }
  392. /**
  393. * kbuffer_next_event - increment the current pointer
  394. * @kbuf: The kbuffer to read
  395. * @ts: Address to store the next record's timestamp (may be NULL to ignore)
  396. *
  397. * Increments the pointers into the subbuffer of the kbuffer to point to the
  398. * next event so that the next kbuffer_read_event() will return a
  399. * new event.
  400. *
  401. * Returns the data of the next event if a new event exists on the subbuffer,
  402. * NULL otherwise.
  403. */
  404. void *kbuffer_next_event(struct kbuffer *kbuf, unsigned long long *ts)
  405. {
  406. int ret;
  407. if (!kbuf || !kbuf->subbuffer)
  408. return NULL;
  409. ret = next_event(kbuf);
  410. if (ret < 0)
  411. return NULL;
  412. if (ts)
  413. *ts = kbuf->timestamp;
  414. return kbuf->data + kbuf->index;
  415. }
  416. /**
  417. * kbuffer_load_subbuffer - load a new subbuffer into the kbuffer
  418. * @kbuf: The kbuffer to load
  419. * @subbuffer: The subbuffer to load into @kbuf.
  420. *
  421. * Load a new subbuffer (page) into @kbuf. This will reset all
  422. * the pointers and update the @kbuf timestamp. The next read will
  423. * return the first event on @subbuffer.
  424. *
  425. * Returns 0 on succes, -1 otherwise.
  426. */
  427. int kbuffer_load_subbuffer(struct kbuffer *kbuf, void *subbuffer)
  428. {
  429. unsigned long long flags;
  430. void *ptr = subbuffer;
  431. if (!kbuf || !subbuffer)
  432. return -1;
  433. kbuf->subbuffer = subbuffer;
  434. kbuf->timestamp = read_8(kbuf, ptr);
  435. ptr += 8;
  436. kbuf->curr = 0;
  437. if (kbuf->flags & KBUFFER_FL_LONG_8)
  438. kbuf->start = 16;
  439. else
  440. kbuf->start = 12;
  441. kbuf->data = subbuffer + kbuf->start;
  442. flags = read_long(kbuf, ptr);
  443. kbuf->size = (unsigned int)flags & COMMIT_MASK;
  444. if (flags & MISSING_EVENTS) {
  445. if (flags & MISSING_STORED) {
  446. ptr = kbuf->data + kbuf->size;
  447. kbuf->lost_events = read_long(kbuf, ptr);
  448. } else
  449. kbuf->lost_events = -1;
  450. } else
  451. kbuf->lost_events = 0;
  452. kbuf->index = 0;
  453. kbuf->next = 0;
  454. next_event(kbuf);
  455. return 0;
  456. }
  457. /**
  458. * kbuffer_subbuf_timestamp - read the timestamp from a sub buffer
  459. * @kbuf: The kbuffer to load
  460. * @subbuf: The subbuffer to read from.
  461. *
  462. * Return the timestamp from a subbuffer.
  463. */
  464. unsigned long long kbuffer_subbuf_timestamp(struct kbuffer *kbuf, void *subbuf)
  465. {
  466. return kbuf->read_8(subbuf);
  467. }
  468. /**
  469. * kbuffer_ptr_delta - read the delta field from a record
  470. * @kbuf: The kbuffer to load
  471. * @ptr: The record in the buffe.
  472. *
  473. * Return the timestamp delta from a record
  474. */
  475. unsigned int kbuffer_ptr_delta(struct kbuffer *kbuf, void *ptr)
  476. {
  477. unsigned int type_len_ts;
  478. type_len_ts = read_4(kbuf, ptr);
  479. return ts4host(kbuf, type_len_ts);
  480. }
  481. /**
  482. * kbuffer_read_event - read the next event in the kbuffer subbuffer
  483. * @kbuf: The kbuffer to read from
  484. * @ts: The address to store the timestamp of the event (may be NULL to ignore)
  485. *
  486. * Returns a pointer to the data part of the current event.
  487. * NULL if no event is left on the subbuffer.
  488. */
  489. void *kbuffer_read_event(struct kbuffer *kbuf, unsigned long long *ts)
  490. {
  491. if (!kbuf || !kbuf->subbuffer)
  492. return NULL;
  493. if (kbuf->curr >= kbuf->size)
  494. return NULL;
  495. if (ts)
  496. *ts = kbuf->timestamp;
  497. return kbuf->data + kbuf->index;
  498. }
  499. /**
  500. * kbuffer_timestamp - Return the timestamp of the current event
  501. * @kbuf: The kbuffer to read from
  502. *
  503. * Returns the timestamp of the current (next) event.
  504. */
  505. unsigned long long kbuffer_timestamp(struct kbuffer *kbuf)
  506. {
  507. return kbuf->timestamp;
  508. }
  509. /**
  510. * kbuffer_read_at_offset - read the event that is at offset
  511. * @kbuf: The kbuffer to read from
  512. * @offset: The offset into the subbuffer
  513. * @ts: The address to store the timestamp of the event (may be NULL to ignore)
  514. *
  515. * The @offset must be an index from the @kbuf subbuffer beginning.
  516. * If @offset is bigger than the stored subbuffer, NULL will be returned.
  517. *
  518. * Returns the data of the record that is at @offset. Note, @offset does
  519. * not need to be the start of the record, the offset just needs to be
  520. * in the record (or beginning of it).
  521. *
  522. * Note, the kbuf timestamp and pointers are updated to the
  523. * returned record. That is, kbuffer_read_event() will return the same
  524. * data and timestamp, and kbuffer_next_event() will increment from
  525. * this record.
  526. */
  527. void *kbuffer_read_at_offset(struct kbuffer *kbuf, int offset,
  528. unsigned long long *ts)
  529. {
  530. void *data;
  531. if (offset < kbuf->start)
  532. offset = 0;
  533. else
  534. offset -= kbuf->start;
  535. /* Reset the buffer */
  536. kbuffer_load_subbuffer(kbuf, kbuf->subbuffer);
  537. data = kbuffer_read_event(kbuf, ts);
  538. while (kbuf->curr < offset) {
  539. data = kbuffer_next_event(kbuf, ts);
  540. if (!data)
  541. break;
  542. }
  543. return data;
  544. }
  545. /**
  546. * kbuffer_subbuffer_size - the size of the loaded subbuffer
  547. * @kbuf: The kbuffer to read from
  548. *
  549. * Returns the size of the subbuffer. Note, this size is
  550. * where the last event resides. The stored subbuffer may actually be
  551. * bigger due to padding and such.
  552. */
  553. int kbuffer_subbuffer_size(struct kbuffer *kbuf)
  554. {
  555. return kbuf->size;
  556. }
  557. /**
  558. * kbuffer_curr_index - Return the index of the record
  559. * @kbuf: The kbuffer to read from
  560. *
  561. * Returns the index from the start of the data part of
  562. * the subbuffer to the current location. Note this is not
  563. * from the start of the subbuffer. An index of zero will
  564. * point to the first record. Use kbuffer_curr_offset() for
  565. * the actually offset (that can be used by kbuffer_read_at_offset())
  566. */
  567. int kbuffer_curr_index(struct kbuffer *kbuf)
  568. {
  569. return kbuf->curr;
  570. }
  571. /**
  572. * kbuffer_curr_offset - Return the offset of the record
  573. * @kbuf: The kbuffer to read from
  574. *
  575. * Returns the offset from the start of the subbuffer to the
  576. * current location.
  577. */
  578. int kbuffer_curr_offset(struct kbuffer *kbuf)
  579. {
  580. return kbuf->curr + kbuf->start;
  581. }
  582. /**
  583. * kbuffer_event_size - return the size of the event data
  584. * @kbuf: The kbuffer to read
  585. *
  586. * Returns the size of the event data (the payload not counting
  587. * the meta data of the record) of the current event.
  588. */
  589. int kbuffer_event_size(struct kbuffer *kbuf)
  590. {
  591. return kbuf->next - kbuf->index;
  592. }
  593. /**
  594. * kbuffer_curr_size - return the size of the entire record
  595. * @kbuf: The kbuffer to read
  596. *
  597. * Returns the size of the entire record (meta data and payload)
  598. * of the current event.
  599. */
  600. int kbuffer_curr_size(struct kbuffer *kbuf)
  601. {
  602. return kbuf->next - kbuf->curr;
  603. }
  604. /**
  605. * kbuffer_missed_events - return the # of missed events from last event.
  606. * @kbuf: The kbuffer to read from
  607. *
  608. * Returns the # of missed events (if recorded) before the current
  609. * event. Note, only events on the beginning of a subbuffer can
  610. * have missed events, all other events within the buffer will be
  611. * zero.
  612. */
  613. int kbuffer_missed_events(struct kbuffer *kbuf)
  614. {
  615. /* Only the first event can have missed events */
  616. if (kbuf->curr)
  617. return 0;
  618. return kbuf->lost_events;
  619. }
  620. /**
  621. * kbuffer_set_old_forma - set the kbuffer to use the old format parsing
  622. * @kbuf: The kbuffer to set
  623. *
  624. * This is obsolete (or should be). The first kernels to use the
  625. * new ring buffer had a slightly different ring buffer format
  626. * (2.6.30 and earlier). It is still somewhat supported by kbuffer,
  627. * but should not be counted on in the future.
  628. */
  629. void kbuffer_set_old_format(struct kbuffer *kbuf)
  630. {
  631. kbuf->flags |= KBUFFER_FL_OLD_FORMAT;
  632. kbuf->next_event = __old_next_event;
  633. }
  634. /**
  635. * kbuffer_start_of_data - return offset of where data starts on subbuffer
  636. * @kbuf: The kbuffer
  637. *
  638. * Returns the location on the subbuffer where the data starts.
  639. */
  640. int kbuffer_start_of_data(struct kbuffer *kbuf)
  641. {
  642. return kbuf->start;
  643. }
  644. /**
  645. * kbuffer_raw_get - get raw buffer info
  646. * @kbuf: The kbuffer
  647. * @subbuf: Start of mapped subbuffer
  648. * @info: Info descriptor to fill in
  649. *
  650. * For debugging. This can return internals of the ring buffer.
  651. * Expects to have info->next set to what it will read.
  652. * The type, length and timestamp delta will be filled in, and
  653. * @info->next will be updated to the next element.
  654. * The @subbuf is used to know if the info is passed the end of
  655. * data and NULL will be returned if it is.
  656. */
  657. struct kbuffer_raw_info *
  658. kbuffer_raw_get(struct kbuffer *kbuf, void *subbuf, struct kbuffer_raw_info *info)
  659. {
  660. unsigned long long flags;
  661. unsigned long long delta;
  662. unsigned int type_len;
  663. unsigned int size;
  664. int start;
  665. int length;
  666. void *ptr = info->next;
  667. if (!kbuf || !subbuf)
  668. return NULL;
  669. if (kbuf->flags & KBUFFER_FL_LONG_8)
  670. start = 16;
  671. else
  672. start = 12;
  673. flags = read_long(kbuf, subbuf + 8);
  674. size = (unsigned int)flags & COMMIT_MASK;
  675. if (ptr < subbuf || ptr >= subbuf + start + size)
  676. return NULL;
  677. type_len = translate_data(kbuf, ptr, &ptr, &delta, &length);
  678. info->next = ptr + length;
  679. info->type = type_len;
  680. info->delta = delta;
  681. info->length = length;
  682. return info;
  683. }