probe-file.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * probe-file.c : operate ftrace k/uprobe events files
  3. *
  4. * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include "util.h"
  18. #include "event.h"
  19. #include "strlist.h"
  20. #include "debug.h"
  21. #include "cache.h"
  22. #include "color.h"
  23. #include "symbol.h"
  24. #include "thread.h"
  25. #include <api/fs/debugfs.h>
  26. #include <api/fs/tracefs.h>
  27. #include "probe-event.h"
  28. #include "probe-file.h"
  29. #include "session.h"
  30. #define MAX_CMDLEN 256
  31. static void print_open_warning(int err, bool uprobe)
  32. {
  33. char sbuf[STRERR_BUFSIZE];
  34. if (err == -ENOENT) {
  35. const char *config;
  36. if (uprobe)
  37. config = "CONFIG_UPROBE_EVENTS";
  38. else
  39. config = "CONFIG_KPROBE_EVENTS";
  40. pr_warning("%cprobe_events file does not exist"
  41. " - please rebuild kernel with %s.\n",
  42. uprobe ? 'u' : 'k', config);
  43. } else if (err == -ENOTSUP)
  44. pr_warning("Tracefs or debugfs is not mounted.\n");
  45. else
  46. pr_warning("Failed to open %cprobe_events: %s\n",
  47. uprobe ? 'u' : 'k',
  48. strerror_r(-err, sbuf, sizeof(sbuf)));
  49. }
  50. static void print_both_open_warning(int kerr, int uerr)
  51. {
  52. /* Both kprobes and uprobes are disabled, warn it. */
  53. if (kerr == -ENOTSUP && uerr == -ENOTSUP)
  54. pr_warning("Tracefs or debugfs is not mounted.\n");
  55. else if (kerr == -ENOENT && uerr == -ENOENT)
  56. pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
  57. "or/and CONFIG_UPROBE_EVENTS.\n");
  58. else {
  59. char sbuf[STRERR_BUFSIZE];
  60. pr_warning("Failed to open kprobe events: %s.\n",
  61. strerror_r(-kerr, sbuf, sizeof(sbuf)));
  62. pr_warning("Failed to open uprobe events: %s.\n",
  63. strerror_r(-uerr, sbuf, sizeof(sbuf)));
  64. }
  65. }
  66. static int open_probe_events(const char *trace_file, bool readwrite)
  67. {
  68. char buf[PATH_MAX];
  69. const char *__debugfs;
  70. const char *tracing_dir = "";
  71. int ret;
  72. __debugfs = tracefs_find_mountpoint();
  73. if (__debugfs == NULL) {
  74. tracing_dir = "tracing/";
  75. __debugfs = debugfs_find_mountpoint();
  76. if (__debugfs == NULL)
  77. return -ENOTSUP;
  78. }
  79. ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
  80. __debugfs, tracing_dir, trace_file);
  81. if (ret >= 0) {
  82. pr_debug("Opening %s write=%d\n", buf, readwrite);
  83. if (readwrite && !probe_event_dry_run)
  84. ret = open(buf, O_RDWR | O_APPEND, 0);
  85. else
  86. ret = open(buf, O_RDONLY, 0);
  87. if (ret < 0)
  88. ret = -errno;
  89. }
  90. return ret;
  91. }
  92. static int open_kprobe_events(bool readwrite)
  93. {
  94. return open_probe_events("kprobe_events", readwrite);
  95. }
  96. static int open_uprobe_events(bool readwrite)
  97. {
  98. return open_probe_events("uprobe_events", readwrite);
  99. }
  100. int probe_file__open(int flag)
  101. {
  102. int fd;
  103. if (flag & PF_FL_UPROBE)
  104. fd = open_uprobe_events(flag & PF_FL_RW);
  105. else
  106. fd = open_kprobe_events(flag & PF_FL_RW);
  107. if (fd < 0)
  108. print_open_warning(fd, flag & PF_FL_UPROBE);
  109. return fd;
  110. }
  111. int probe_file__open_both(int *kfd, int *ufd, int flag)
  112. {
  113. if (!kfd || !ufd)
  114. return -EINVAL;
  115. *kfd = open_kprobe_events(flag & PF_FL_RW);
  116. *ufd = open_uprobe_events(flag & PF_FL_RW);
  117. if (*kfd < 0 && *ufd < 0) {
  118. print_both_open_warning(*kfd, *ufd);
  119. return *kfd;
  120. }
  121. return 0;
  122. }
  123. /* Get raw string list of current kprobe_events or uprobe_events */
  124. struct strlist *probe_file__get_rawlist(int fd)
  125. {
  126. int ret, idx;
  127. FILE *fp;
  128. char buf[MAX_CMDLEN];
  129. char *p;
  130. struct strlist *sl;
  131. sl = strlist__new(NULL, NULL);
  132. fp = fdopen(dup(fd), "r");
  133. while (!feof(fp)) {
  134. p = fgets(buf, MAX_CMDLEN, fp);
  135. if (!p)
  136. break;
  137. idx = strlen(p) - 1;
  138. if (p[idx] == '\n')
  139. p[idx] = '\0';
  140. ret = strlist__add(sl, buf);
  141. if (ret < 0) {
  142. pr_debug("strlist__add failed (%d)\n", ret);
  143. strlist__delete(sl);
  144. return NULL;
  145. }
  146. }
  147. fclose(fp);
  148. return sl;
  149. }
  150. static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
  151. {
  152. char buf[128];
  153. struct strlist *sl, *rawlist;
  154. struct str_node *ent;
  155. struct probe_trace_event tev;
  156. int ret = 0;
  157. memset(&tev, 0, sizeof(tev));
  158. rawlist = probe_file__get_rawlist(fd);
  159. if (!rawlist)
  160. return NULL;
  161. sl = strlist__new(NULL, NULL);
  162. strlist__for_each(ent, rawlist) {
  163. ret = parse_probe_trace_command(ent->s, &tev);
  164. if (ret < 0)
  165. break;
  166. if (include_group) {
  167. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  168. tev.event);
  169. if (ret >= 0)
  170. ret = strlist__add(sl, buf);
  171. } else
  172. ret = strlist__add(sl, tev.event);
  173. clear_probe_trace_event(&tev);
  174. if (ret < 0)
  175. break;
  176. }
  177. strlist__delete(rawlist);
  178. if (ret < 0) {
  179. strlist__delete(sl);
  180. return NULL;
  181. }
  182. return sl;
  183. }
  184. /* Get current perf-probe event names */
  185. struct strlist *probe_file__get_namelist(int fd)
  186. {
  187. return __probe_file__get_namelist(fd, false);
  188. }
  189. int probe_file__add_event(int fd, struct probe_trace_event *tev)
  190. {
  191. int ret = 0;
  192. char *buf = synthesize_probe_trace_command(tev);
  193. char sbuf[STRERR_BUFSIZE];
  194. if (!buf) {
  195. pr_debug("Failed to synthesize probe trace event.\n");
  196. return -EINVAL;
  197. }
  198. pr_debug("Writing event: %s\n", buf);
  199. if (!probe_event_dry_run) {
  200. ret = write(fd, buf, strlen(buf));
  201. if (ret <= 0) {
  202. ret = -errno;
  203. pr_warning("Failed to write event: %s\n",
  204. strerror_r(errno, sbuf, sizeof(sbuf)));
  205. }
  206. }
  207. free(buf);
  208. return ret;
  209. }
  210. static int __del_trace_probe_event(int fd, struct str_node *ent)
  211. {
  212. char *p;
  213. char buf[128];
  214. int ret;
  215. /* Convert from perf-probe event to trace-probe event */
  216. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  217. if (ret < 0)
  218. goto error;
  219. p = strchr(buf + 2, ':');
  220. if (!p) {
  221. pr_debug("Internal error: %s should have ':' but not.\n",
  222. ent->s);
  223. ret = -ENOTSUP;
  224. goto error;
  225. }
  226. *p = '/';
  227. pr_debug("Writing event: %s\n", buf);
  228. ret = write(fd, buf, strlen(buf));
  229. if (ret < 0) {
  230. ret = -errno;
  231. goto error;
  232. }
  233. pr_info("Removed event: %s\n", ent->s);
  234. return 0;
  235. error:
  236. pr_warning("Failed to delete event: %s\n",
  237. strerror_r(-ret, buf, sizeof(buf)));
  238. return ret;
  239. }
  240. int probe_file__del_events(int fd, struct strfilter *filter)
  241. {
  242. struct strlist *namelist;
  243. struct str_node *ent;
  244. const char *p;
  245. int ret = -ENOENT;
  246. namelist = __probe_file__get_namelist(fd, true);
  247. if (!namelist)
  248. return -ENOENT;
  249. strlist__for_each(ent, namelist) {
  250. p = strchr(ent->s, ':');
  251. if ((p && strfilter__compare(filter, p + 1)) ||
  252. strfilter__compare(filter, ent->s)) {
  253. ret = __del_trace_probe_event(fd, ent);
  254. if (ret < 0)
  255. break;
  256. }
  257. }
  258. strlist__delete(namelist);
  259. return ret;
  260. }