tracex4_user.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #include <unistd.h>
  11. #include <stdbool.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <linux/bpf.h>
  15. #include "libbpf.h"
  16. #include "bpf_load.h"
  17. struct pair {
  18. long long val;
  19. __u64 ip;
  20. };
  21. static __u64 time_get_ns(void)
  22. {
  23. struct timespec ts;
  24. clock_gettime(CLOCK_MONOTONIC, &ts);
  25. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  26. }
  27. static void print_old_objects(int fd)
  28. {
  29. long long val = time_get_ns();
  30. __u64 key, next_key;
  31. struct pair v;
  32. key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
  33. key = -1;
  34. while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
  35. bpf_lookup_elem(map_fd[0], &next_key, &v);
  36. key = next_key;
  37. if (val - v.val < 1000000000ll)
  38. /* object was allocated more then 1 sec ago */
  39. continue;
  40. printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
  41. next_key, (val - v.val) / 1000000000ll, v.ip);
  42. }
  43. }
  44. int main(int ac, char **argv)
  45. {
  46. char filename[256];
  47. int i;
  48. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  49. if (load_bpf_file(filename)) {
  50. printf("%s", bpf_log_buf);
  51. return 1;
  52. }
  53. for (i = 0; ; i++) {
  54. print_old_objects(map_fd[1]);
  55. sleep(1);
  56. }
  57. return 0;
  58. }