probe-file.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/tracing_path.h>
  26. #include "probe-event.h"
  27. #include "probe-file.h"
  28. #include "session.h"
  29. #define MAX_CMDLEN 256
  30. static void print_open_warning(int err, bool uprobe)
  31. {
  32. char sbuf[STRERR_BUFSIZE];
  33. if (err == -ENOENT) {
  34. const char *config;
  35. if (uprobe)
  36. config = "CONFIG_UPROBE_EVENTS";
  37. else
  38. config = "CONFIG_KPROBE_EVENTS";
  39. pr_warning("%cprobe_events file does not exist"
  40. " - please rebuild kernel with %s.\n",
  41. uprobe ? 'u' : 'k', config);
  42. } else if (err == -ENOTSUP)
  43. pr_warning("Tracefs or debugfs is not mounted.\n");
  44. else
  45. pr_warning("Failed to open %cprobe_events: %s\n",
  46. uprobe ? 'u' : 'k',
  47. strerror_r(-err, sbuf, sizeof(sbuf)));
  48. }
  49. static void print_both_open_warning(int kerr, int uerr)
  50. {
  51. /* Both kprobes and uprobes are disabled, warn it. */
  52. if (kerr == -ENOTSUP && uerr == -ENOTSUP)
  53. pr_warning("Tracefs or debugfs is not mounted.\n");
  54. else if (kerr == -ENOENT && uerr == -ENOENT)
  55. pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
  56. "or/and CONFIG_UPROBE_EVENTS.\n");
  57. else {
  58. char sbuf[STRERR_BUFSIZE];
  59. pr_warning("Failed to open kprobe events: %s.\n",
  60. strerror_r(-kerr, sbuf, sizeof(sbuf)));
  61. pr_warning("Failed to open uprobe events: %s.\n",
  62. strerror_r(-uerr, sbuf, sizeof(sbuf)));
  63. }
  64. }
  65. static int open_probe_events(const char *trace_file, bool readwrite)
  66. {
  67. char buf[PATH_MAX];
  68. const char *tracing_dir = "";
  69. int ret;
  70. ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
  71. tracing_path, tracing_dir, trace_file);
  72. if (ret >= 0) {
  73. pr_debug("Opening %s write=%d\n", buf, readwrite);
  74. if (readwrite && !probe_event_dry_run)
  75. ret = open(buf, O_RDWR | O_APPEND, 0);
  76. else
  77. ret = open(buf, O_RDONLY, 0);
  78. if (ret < 0)
  79. ret = -errno;
  80. }
  81. return ret;
  82. }
  83. static int open_kprobe_events(bool readwrite)
  84. {
  85. return open_probe_events("kprobe_events", readwrite);
  86. }
  87. static int open_uprobe_events(bool readwrite)
  88. {
  89. return open_probe_events("uprobe_events", readwrite);
  90. }
  91. int probe_file__open(int flag)
  92. {
  93. int fd;
  94. if (flag & PF_FL_UPROBE)
  95. fd = open_uprobe_events(flag & PF_FL_RW);
  96. else
  97. fd = open_kprobe_events(flag & PF_FL_RW);
  98. if (fd < 0)
  99. print_open_warning(fd, flag & PF_FL_UPROBE);
  100. return fd;
  101. }
  102. int probe_file__open_both(int *kfd, int *ufd, int flag)
  103. {
  104. if (!kfd || !ufd)
  105. return -EINVAL;
  106. *kfd = open_kprobe_events(flag & PF_FL_RW);
  107. *ufd = open_uprobe_events(flag & PF_FL_RW);
  108. if (*kfd < 0 && *ufd < 0) {
  109. print_both_open_warning(*kfd, *ufd);
  110. return *kfd;
  111. }
  112. return 0;
  113. }
  114. /* Get raw string list of current kprobe_events or uprobe_events */
  115. struct strlist *probe_file__get_rawlist(int fd)
  116. {
  117. int ret, idx;
  118. FILE *fp;
  119. char buf[MAX_CMDLEN];
  120. char *p;
  121. struct strlist *sl;
  122. if (fd < 0)
  123. return NULL;
  124. sl = strlist__new(NULL, NULL);
  125. fp = fdopen(dup(fd), "r");
  126. while (!feof(fp)) {
  127. p = fgets(buf, MAX_CMDLEN, fp);
  128. if (!p)
  129. break;
  130. idx = strlen(p) - 1;
  131. if (p[idx] == '\n')
  132. p[idx] = '\0';
  133. ret = strlist__add(sl, buf);
  134. if (ret < 0) {
  135. pr_debug("strlist__add failed (%d)\n", ret);
  136. strlist__delete(sl);
  137. return NULL;
  138. }
  139. }
  140. fclose(fp);
  141. return sl;
  142. }
  143. static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
  144. {
  145. char buf[128];
  146. struct strlist *sl, *rawlist;
  147. struct str_node *ent;
  148. struct probe_trace_event tev;
  149. int ret = 0;
  150. memset(&tev, 0, sizeof(tev));
  151. rawlist = probe_file__get_rawlist(fd);
  152. if (!rawlist)
  153. return NULL;
  154. sl = strlist__new(NULL, NULL);
  155. strlist__for_each(ent, rawlist) {
  156. ret = parse_probe_trace_command(ent->s, &tev);
  157. if (ret < 0)
  158. break;
  159. if (include_group) {
  160. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  161. tev.event);
  162. if (ret >= 0)
  163. ret = strlist__add(sl, buf);
  164. } else
  165. ret = strlist__add(sl, tev.event);
  166. clear_probe_trace_event(&tev);
  167. if (ret < 0)
  168. break;
  169. }
  170. strlist__delete(rawlist);
  171. if (ret < 0) {
  172. strlist__delete(sl);
  173. return NULL;
  174. }
  175. return sl;
  176. }
  177. /* Get current perf-probe event names */
  178. struct strlist *probe_file__get_namelist(int fd)
  179. {
  180. return __probe_file__get_namelist(fd, false);
  181. }
  182. int probe_file__add_event(int fd, struct probe_trace_event *tev)
  183. {
  184. int ret = 0;
  185. char *buf = synthesize_probe_trace_command(tev);
  186. char sbuf[STRERR_BUFSIZE];
  187. if (!buf) {
  188. pr_debug("Failed to synthesize probe trace event.\n");
  189. return -EINVAL;
  190. }
  191. pr_debug("Writing event: %s\n", buf);
  192. if (!probe_event_dry_run) {
  193. ret = write(fd, buf, strlen(buf));
  194. if (ret <= 0) {
  195. ret = -errno;
  196. pr_warning("Failed to write event: %s\n",
  197. strerror_r(errno, sbuf, sizeof(sbuf)));
  198. }
  199. }
  200. free(buf);
  201. return ret;
  202. }
  203. static int __del_trace_probe_event(int fd, struct str_node *ent)
  204. {
  205. char *p;
  206. char buf[128];
  207. int ret;
  208. /* Convert from perf-probe event to trace-probe event */
  209. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  210. if (ret < 0)
  211. goto error;
  212. p = strchr(buf + 2, ':');
  213. if (!p) {
  214. pr_debug("Internal error: %s should have ':' but not.\n",
  215. ent->s);
  216. ret = -ENOTSUP;
  217. goto error;
  218. }
  219. *p = '/';
  220. pr_debug("Writing event: %s\n", buf);
  221. ret = write(fd, buf, strlen(buf));
  222. if (ret < 0) {
  223. ret = -errno;
  224. goto error;
  225. }
  226. return 0;
  227. error:
  228. pr_warning("Failed to delete event: %s\n",
  229. strerror_r(-ret, buf, sizeof(buf)));
  230. return ret;
  231. }
  232. int probe_file__get_events(int fd, struct strfilter *filter,
  233. struct strlist *plist)
  234. {
  235. struct strlist *namelist;
  236. struct str_node *ent;
  237. const char *p;
  238. int ret = -ENOENT;
  239. if (!plist)
  240. return -EINVAL;
  241. namelist = __probe_file__get_namelist(fd, true);
  242. if (!namelist)
  243. return -ENOENT;
  244. strlist__for_each(ent, namelist) {
  245. p = strchr(ent->s, ':');
  246. if ((p && strfilter__compare(filter, p + 1)) ||
  247. strfilter__compare(filter, ent->s)) {
  248. strlist__add(plist, ent->s);
  249. ret = 0;
  250. }
  251. }
  252. strlist__delete(namelist);
  253. return ret;
  254. }
  255. int probe_file__del_strlist(int fd, struct strlist *namelist)
  256. {
  257. int ret = 0;
  258. struct str_node *ent;
  259. strlist__for_each(ent, namelist) {
  260. ret = __del_trace_probe_event(fd, ent);
  261. if (ret < 0)
  262. break;
  263. }
  264. return ret;
  265. }
  266. int probe_file__del_events(int fd, struct strfilter *filter)
  267. {
  268. struct strlist *namelist;
  269. int ret;
  270. namelist = strlist__new(NULL, NULL);
  271. if (!namelist)
  272. return -ENOMEM;
  273. ret = probe_file__get_events(fd, filter, namelist);
  274. if (ret < 0)
  275. return ret;
  276. ret = probe_file__del_strlist(fd, namelist);
  277. strlist__delete(namelist);
  278. return ret;
  279. }