tracex4_user.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <sys/resource.h>
  16. #include <bpf/bpf.h>
  17. #include "bpf_load.h"
  18. struct pair {
  19. long long val;
  20. __u64 ip;
  21. };
  22. static __u64 time_get_ns(void)
  23. {
  24. struct timespec ts;
  25. clock_gettime(CLOCK_MONOTONIC, &ts);
  26. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  27. }
  28. static void print_old_objects(int fd)
  29. {
  30. long long val = time_get_ns();
  31. __u64 key, next_key;
  32. struct pair v;
  33. key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
  34. key = -1;
  35. while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
  36. bpf_map_lookup_elem(map_fd[0], &next_key, &v);
  37. key = next_key;
  38. if (val - v.val < 1000000000ll)
  39. /* object was allocated more then 1 sec ago */
  40. continue;
  41. printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
  42. next_key, (val - v.val) / 1000000000ll, v.ip);
  43. }
  44. }
  45. int main(int ac, char **argv)
  46. {
  47. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  48. char filename[256];
  49. int i;
  50. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  51. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  52. perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
  53. return 1;
  54. }
  55. if (load_bpf_file(filename)) {
  56. printf("%s", bpf_log_buf);
  57. return 1;
  58. }
  59. for (i = 0; ; i++) {
  60. print_old_objects(map_fd[1]);
  61. sleep(1);
  62. }
  63. return 0;
  64. }