uprobe.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. use std::borrow::Cow;
  2. use proc_macro2::TokenStream;
  3. use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt as _};
  4. use quote::quote;
  5. use syn::{spanned::Spanned as _, ItemFn};
  6. use crate::args::{err_on_unknown_args, pop_bool_arg, pop_string_arg};
  7. #[allow(clippy::enum_variant_names)]
  8. #[derive(Debug, Copy, Clone)]
  9. pub(crate) enum UProbeKind {
  10. UProbe,
  11. URetProbe,
  12. }
  13. impl std::fmt::Display for UProbeKind {
  14. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  15. use UProbeKind::*;
  16. match self {
  17. UProbe => write!(f, "uprobe"),
  18. URetProbe => write!(f, "uretprobe"),
  19. }
  20. }
  21. }
  22. pub(crate) struct UProbe {
  23. kind: UProbeKind,
  24. path: Option<String>,
  25. function: Option<String>,
  26. offset: Option<u64>,
  27. item: ItemFn,
  28. sleepable: bool,
  29. }
  30. impl UProbe {
  31. pub(crate) fn parse(
  32. kind: UProbeKind,
  33. attrs: TokenStream,
  34. item: TokenStream,
  35. ) -> Result<Self, Diagnostic> {
  36. let item = syn::parse2(item)?;
  37. let span = attrs.span();
  38. let mut args = syn::parse2(attrs)?;
  39. let path = pop_string_arg(&mut args, "path");
  40. let function = pop_string_arg(&mut args, "function");
  41. let offset = pop_string_arg(&mut args, "offset")
  42. .as_deref()
  43. .map(str::parse)
  44. .transpose()
  45. .map_err(|err| span.error(format!("failed to parse `offset` argument: {}", err)))?;
  46. let sleepable = pop_bool_arg(&mut args, "sleepable");
  47. err_on_unknown_args(&args)?;
  48. Ok(Self {
  49. kind,
  50. item,
  51. path,
  52. function,
  53. offset,
  54. sleepable,
  55. })
  56. }
  57. pub(crate) fn expand(&self) -> Result<TokenStream, Diagnostic> {
  58. let Self {
  59. kind,
  60. path,
  61. function,
  62. offset,
  63. item,
  64. sleepable,
  65. } = self;
  66. let ItemFn {
  67. attrs: _,
  68. vis,
  69. sig,
  70. block: _,
  71. } = item;
  72. let mut prefix = kind.to_string();
  73. if *sleepable {
  74. prefix.push_str(".s");
  75. }
  76. let section_name: Cow<'_, _> = match path {
  77. None => prefix.into(),
  78. Some(path) => {
  79. let path = path.strip_prefix("/").unwrap_or(path);
  80. // TODO: check this in parse instead.
  81. let function = function
  82. .as_deref()
  83. .ok_or(item.sig.span().error("expected `function` attribute"))?;
  84. match offset {
  85. None => format!("{prefix}/{path}:{function}").into(),
  86. Some(offset) => format!("{prefix}/{path}:{function}+{offset}",).into(),
  87. }
  88. }
  89. };
  90. let probe_type = if section_name.as_ref().starts_with("uprobe") {
  91. quote! { ProbeContext }
  92. } else {
  93. quote! { RetProbeContext }
  94. };
  95. let fn_name = &sig.ident;
  96. Ok(quote! {
  97. #[no_mangle]
  98. #[link_section = #section_name]
  99. #vis fn #fn_name(ctx: *mut ::core::ffi::c_void) -> u32 {
  100. let _ = #fn_name(::aya_ebpf::programs::#probe_type::new(ctx));
  101. return 0;
  102. #item
  103. }
  104. })
  105. }
  106. }
  107. #[cfg(test)]
  108. mod tests {
  109. use syn::parse_quote;
  110. use super::*;
  111. #[test]
  112. fn uprobe() {
  113. let uprobe = UProbe::parse(
  114. UProbeKind::UProbe,
  115. parse_quote! {},
  116. parse_quote! {
  117. fn foo(ctx: ProbeContext) -> u32 {
  118. 0
  119. }
  120. },
  121. )
  122. .unwrap();
  123. assert_eq!(
  124. uprobe.expand().unwrap().to_string(),
  125. quote! {
  126. #[no_mangle]
  127. #[link_section = "uprobe"]
  128. fn foo(ctx: *mut ::core::ffi::c_void) -> u32 {
  129. let _ = foo(::aya_ebpf::programs::ProbeContext::new(ctx));
  130. return 0;
  131. fn foo(ctx: ProbeContext) -> u32 {
  132. 0
  133. }
  134. }
  135. }
  136. .to_string()
  137. );
  138. }
  139. #[test]
  140. fn uprobe_sleepable() {
  141. let uprobe = UProbe::parse(
  142. UProbeKind::UProbe,
  143. parse_quote! {sleepable},
  144. parse_quote! {
  145. fn foo(ctx: ProbeContext) -> u32 {
  146. 0
  147. }
  148. },
  149. )
  150. .unwrap();
  151. assert_eq!(
  152. uprobe.expand().unwrap().to_string(),
  153. quote! {
  154. #[no_mangle]
  155. #[link_section = "uprobe.s"]
  156. fn foo(ctx: *mut ::core::ffi::c_void) -> u32 {
  157. let _ = foo(::aya_ebpf::programs::ProbeContext::new(ctx));
  158. return 0;
  159. fn foo(ctx: ProbeContext) -> u32 {
  160. 0
  161. }
  162. }
  163. }
  164. .to_string()
  165. );
  166. }
  167. #[test]
  168. fn uprobe_with_path() {
  169. let uprobe = UProbe::parse(
  170. UProbeKind::UProbe,
  171. parse_quote! {
  172. path = "/self/proc/exe",
  173. function = "trigger_uprobe"
  174. },
  175. parse_quote! {
  176. fn foo(ctx: ProbeContext) -> u32 {
  177. 0
  178. }
  179. },
  180. )
  181. .unwrap();
  182. assert_eq!(
  183. uprobe.expand().unwrap().to_string(),
  184. quote! {
  185. #[no_mangle]
  186. #[link_section = "uprobe/self/proc/exe:trigger_uprobe"]
  187. fn foo(ctx: *mut ::core::ffi::c_void) -> u32 {
  188. let _ = foo(::aya_ebpf::programs::ProbeContext::new(ctx));
  189. return 0;
  190. fn foo(ctx: ProbeContext) -> u32 {
  191. 0
  192. }
  193. }
  194. }
  195. .to_string()
  196. );
  197. }
  198. #[test]
  199. fn test_uprobe_with_path_and_offset() {
  200. let uprobe = UProbe::parse(
  201. UProbeKind::UProbe,
  202. parse_quote! {
  203. path = "/self/proc/exe", function = "foo", offset = "123"
  204. },
  205. parse_quote! {
  206. fn foo(ctx: ProbeContext) -> u32 {
  207. 0
  208. }
  209. },
  210. )
  211. .unwrap();
  212. assert_eq!(
  213. uprobe.expand().unwrap().to_string(),
  214. quote! {
  215. #[no_mangle]
  216. #[link_section = "uprobe/self/proc/exe:foo+123"]
  217. fn foo(ctx: *mut ::core::ffi::c_void) -> u32 {
  218. let _ = foo(::aya_ebpf::programs::ProbeContext::new(ctx));
  219. return 0;
  220. fn foo(ctx: ProbeContext) -> u32 {
  221. 0
  222. }
  223. }
  224. }
  225. .to_string()
  226. );
  227. }
  228. #[test]
  229. fn test_uretprobe() {
  230. let uprobe = UProbe::parse(
  231. UProbeKind::URetProbe,
  232. parse_quote! {},
  233. parse_quote! {
  234. fn foo(ctx: ProbeContext) -> u32 {
  235. 0
  236. }
  237. },
  238. )
  239. .unwrap();
  240. assert_eq!(
  241. uprobe.expand().unwrap().to_string(),
  242. quote! {
  243. #[no_mangle]
  244. #[link_section = "uretprobe"]
  245. fn foo(ctx: *mut ::core::ffi::c_void) -> u32 {
  246. let _ = foo(::aya_ebpf::programs::RetProbeContext::new(ctx));
  247. return 0;
  248. fn foo(ctx: ProbeContext) -> u32 {
  249. 0
  250. }
  251. }
  252. }
  253. .to_string()
  254. );
  255. }
  256. }