ptrace-syscalls-macros 0.0.0-experimental.3

Internal proc macros for ptrace-syscalls
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
use convert_case::{Case, Casing};
use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{
  braced, bracketed, parenthesized, parse::Parse, parse_macro_input, punctuated::Punctuated, spanned::Spanned, token,
  Expr, GenericArgument, Ident, PathArguments, Token, Type,
};

#[allow(dead_code)]
struct ModifiedArgsExpr {
  plus: Token![+],
  brace_token: token::Brace,
  args: Punctuated<ArgField, Token![,]>,
}

impl Parse for ModifiedArgsExpr {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let content;
    Ok(Self {
      plus: input.parse()?,
      brace_token: braced!(content in input),
      args: content.parse_terminated(ArgField::parse, Token![,])?,
    })
  }
}

#[allow(dead_code)]
struct Decoder {
  at: Token![@],
  func: Ident,
  paren_token: token::Paren,
  args: Punctuated<Expr, Token![,]>,
}

impl Decoder {
  fn decoded(&self, arg_name: &Ident, arg_type: proc_macro2::TokenStream, span: Span) -> proc_macro2::TokenStream {
    let mut counter = 0u32;
    let func: &Ident = &self.func;
    let (target_trait, is_result) = match func.to_string().as_str() {
      "sized_by" => (format_ident!("InspectDynSizedFromPid"), false),
      "counted_by" => (format_ident!("InspectCountedFromPid"), false),
      "sized_by_result" => (format_ident!("InspectDynSizedFromPid"), true),
      "counted_by_result" => (format_ident!("InspectCountedFromPid"), true),
      _ => panic!("Unsupported decoder function: {:?}", func),
    };
    let args = &self.args;
    if !is_result {
      quote_spanned! {
        span =>
        let #arg_name = <#arg_type as #target_trait>::inspect_from(inspectee_pid, raw_args.#arg_name as AddressType, (#args) as usize);
      }
    } else {
      counter += 1;
      let tmp_name = format_ident!("tmp_{}", counter);
      quote_spanned! {
        span =>
        let #arg_name = if let Ok(&#tmp_name) = (#args).as_ref() {
          <#arg_type as #target_trait>::inspect_from(inspectee_pid, raw_args.#arg_name as AddressType, #tmp_name as usize)
        } else {
          Err(InspectError::DependencyInspectFailure { field: stringify!(#arg_name) })
        };
      }
    }
  }
}

impl Parse for Decoder {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let content;
    Ok(Self {
      at: input.parse()?,
      func: input.parse()?,
      paren_token: parenthesized!(content in input),
      args: content.parse_terminated(Expr::parse, Token![,])?,
    })
  }
}

#[allow(dead_code)]
struct ArgField {
  ident: Ident,
  colon_token: Token![:],
  ty: Type,
  decoder: Option<Decoder>,
}

impl Parse for ArgField {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let ident = input.parse()?;
    let colon_token = input.parse()?;
    let ty = input.parse()?;
    let lookahead = input.lookahead1();
    let decoder = if lookahead.peek(Token![@]) {
      Some(input.parse()?)
    } else {
      None
    };
    Ok(Self {
      ident,
      colon_token,
      ty,
      decoder,
    })
  }
}

#[allow(dead_code)]
struct SyscallEntry {
  name: syn::Ident,
  paren_token: token::Paren,
  raw_args: Punctuated<ArgField, Token![,]>,
  separator: Token![/],
  brace_token: token::Brace,
  args: Punctuated<ArgField, Token![,]>,
  arrow: Token![->],
  result: Ident,
  modified_args: Option<ModifiedArgsExpr>,
  for_token: Token![for],
  bracket_token: token::Bracket,
  archs: Punctuated<Arch, Token![,]>,
  group_token: Token![~],
  bracket_token_2: token::Bracket,
  groups: Punctuated<Ident, Token![,]>,
  span: Option<Span>,
}

impl Parse for SyscallEntry {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let content;
    let raw_args;
    let archs_content;
    let groups_content;
    let start;
    let end;
    Ok(SyscallEntry {
      name: {
        let name: Ident = input.parse()?;
        start = name.span();
        name
      },
      paren_token: parenthesized!(raw_args in input),
      raw_args: raw_args.parse_terminated(ArgField::parse, Token![,])?,
      separator: input.parse()?,
      brace_token: braced!(content in input),
      args: content.parse_terminated(ArgField::parse, Token![,])?,
      arrow: input.parse()?,
      result: input.parse()?,
      modified_args: {
        let lookahead = input.lookahead1();
        if lookahead.peek(token::Plus) {
          Some(input.parse()?)
        } else {
          None
        }
      },
      group_token: input.parse()?,
      bracket_token_2: bracketed!(groups_content in input),
      groups: groups_content.parse_terminated(Ident::parse, Token![,])?,
      for_token: input.parse()?,
      bracket_token: bracketed!(archs_content in input),
      archs: {
        let archs = archs_content.parse_terminated(Arch::parse, Token![,])?;
        end = archs.last().unwrap().span;
        archs
      },
      span: end.and_then(|end| start.join(end)),
    })
  }
}

#[allow(dead_code)]
struct Arch {
  name: syn::Ident,
  colon: Token![:],
  number: syn::LitInt,
  span: Option<Span>,
}

impl Parse for Arch {
  fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
    let start;
    let end;
    Ok(Arch {
      name: {
        let name: Ident = input.parse()?;
        start = name.span();
        name
      },
      colon: input.parse()?,
      number: {
        let number: syn::LitInt = input.parse()?;
        end = number.span();
        number
      },
      span: start.join(end),
    })
  }
}

struct GenSyscallArgsStructResult {
  args_struct: proc_macro2::TokenStream,
  raw_args_struct: proc_macro2::TokenStream,
  modified_args_struct: proc_macro2::TokenStream,
  name: Ident,
  args_struct_type: Ident,
  raw_args_struct_type: Ident,
  modified_args_struct_type: Ident,
  archs: Vec<String>,
  syscall_number: Ident,
  syscall_const_name: Ident,
}

fn gen_syscall_args_struct(
  syscall: &SyscallEntry,
  crate_token: proc_macro2::TokenStream,
) -> GenSyscallArgsStructResult {
  let span = syscall.span.unwrap_or_else(Span::call_site);
  let name = &syscall.name;
  let args = &syscall.args;
  let groups_idents = &syscall.groups.iter().collect::<Vec<_>>();
  let groups = if groups_idents.is_empty() {
    quote_spanned! { span =>
      #crate_token::SyscallGroups::empty()
    }
  } else {
    quote_spanned! { span =>
      #(#crate_token::SyscallGroups::#groups_idents)|*
    }
  };
  let arch_name_0 = syscall.archs.first().as_ref().unwrap().name.to_string();
  let arch_number_0 = &syscall.archs.first().as_ref().unwrap().number;
  let (mut arch_names, numbers): (Vec<_>, Vec<_>) = syscall
    .archs
    .iter()
    .skip(1)
    .map(|x| (x.name.to_string(), x.number.clone()))
    .unzip();
  let syscall_number = quote_spanned! { span =>
    if cfg!(target_arch = #arch_name_0) {
      #arch_number_0
    }
    #(
    else if cfg!(target_arch = #arch_names) {
      #numbers
    }
    )*
    else {
      unreachable!()
    }
  };
  arch_names.insert(0, arch_name_0);
  let mut camel_case_name = name.to_string().to_case(Case::UpperCamel);
  let camel_case_ident = Ident::new(&camel_case_name, name.span());
  camel_case_name.push_str("Args");
  let camel_case_args_type = Ident::new(&camel_case_name, name.span());
  camel_case_name.replace_range((camel_case_name.len() - 4).., "RawArgs");
  let camel_case_raw_args_type = Ident::new(&camel_case_name, name.span());
  camel_case_name.replace_range((camel_case_name.len() - 7).., "ModifiedArgs");
  let camel_case_modified_args_type = Ident::new(&camel_case_name, name.span());
  let mut inspects = vec![];
  let mut arg_names = vec![];
  let mut wrapped_arg_types = vec![];
  for arg in args.iter() {
    let arg_name = &arg.ident;
    arg_names.push(arg_name.clone());
    let arg_type = &arg.ty;
    let (wrapped_arg_type, need_memory_inspection) = wrap_syscall_arg_type(arg_type, span);
    wrapped_arg_types.push(wrapped_arg_type.clone());
    if !need_memory_inspection {
      let inspect = quote_spanned! { span =>
        let #arg_name = raw_args.#arg_name as #wrapped_arg_type;
      };
      inspects.push(inspect);
    } else if let Some(decoder) = &arg.decoder {
      let decoded = decoder.decoded(arg_name, wrapped_arg_type, span);
      inspects.push(decoded);
    } else {
      inspects.push(quote_spanned! { span =>
          let #arg_name: #wrapped_arg_type = #crate_token::InspectFromPid::inspect_from(inspectee_pid, raw_args.#arg_name as #crate_token::AddressType);
        });
    }
  }
  let mut raw_arg_names = vec![];
  let mut raw_arg_types = vec![];
  let mut inspect_raw_args = vec![];
  for (i, raw_arg) in syscall.raw_args.iter().enumerate() {
    let arg_name = &raw_arg.ident;
    raw_arg_names.push(arg_name.clone());
    let arg_type = &raw_arg.ty;
    raw_arg_types.push(arg_type.clone());
    let literal_i = syn::LitInt::new(&i.to_string(), arg_type.span());
    inspect_raw_args.push(quote_spanned! { span =>
      let #arg_name = syscall_arg!(regs, #literal_i) as #arg_type;
    });
  }
  let mut modified_arg_names = vec![];
  let mut modified_arg_names_err = vec![];
  let mut modified_arg_types = vec![];
  let mut inspect_modified_args = vec![];
  let syscall_result_type = &syscall.result;
  modified_arg_names.push(format_ident!("syscall_result"));
  modified_arg_types.push(quote_spanned! { span => #syscall_result_type });
  let (inspect_syscall_result, is_syscall_failed) = if syscall_result_type != "Unit" {
    (
      quote_spanned! {
        span =>
        let syscall_result = syscall_res_from_regs!(regs) as #syscall_result_type;
      },
      quote_spanned! { span => (syscall_result as isize) < 0 },
    )
  } else {
    (quote_spanned! { span => let syscall_result = (); }, quote_spanned! { span => false })
  };
  if let Some(modified_args) = &syscall.modified_args {
    for modified_arg in modified_args.args.iter() {
      let arg_name = &modified_arg.ident;
      modified_arg_names.push(arg_name.clone());
      modified_arg_names_err.push(quote_spanned! { span =>
        #arg_name: Err(InspectError::SyscallFailure)
      });
      let arg_type = &modified_arg.ty;
      let (wrapped_arg_type, need_memory_inspection) = wrap_syscall_arg_type(arg_type, span);
      modified_arg_types.push(wrapped_arg_type.clone());
      if !need_memory_inspection {
        let inspect = quote_spanned! { span =>
          let #arg_name = raw_args.#arg_name as #wrapped_arg_type;
        };
        inspect_modified_args.push(inspect);
      } else if let Some(decoder) = &modified_arg.decoder {
        let decoded = decoder.decoded(arg_name, wrapped_arg_type, span);
        inspect_modified_args.push(decoded);
      } else {
        inspect_modified_args.push(quote_spanned! { span =>
            let #arg_name: #wrapped_arg_type = #crate_token::InspectFromPid::inspect_from(inspectee_pid, raw_args.#arg_name as #crate_token::AddressType);
        });
      }
    }
  }
  let syscall_const_name = format_ident!("SYS_{}", name);
  GenSyscallArgsStructResult {
    syscall_number: syscall_const_name.clone(),
    raw_args_struct: quote_spanned! { span =>
      #[cfg(any(#(target_arch = #arch_names),*))]
      pub const #syscall_const_name: isize = #syscall_number;

      #[cfg(any(#(target_arch = #arch_names),*))]
      #[derive(Debug, Clone, Copy, PartialEq)]
      pub struct #camel_case_raw_args_type {
        #(pub #raw_arg_names: #raw_arg_types),*
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #camel_case_raw_args_type {
        fn from_regs(regs: &#crate_token::arch::PtraceRegisters) -> Self {
          use #crate_token::arch::syscall_arg;
          #(#inspect_raw_args)*
          Self {
            #(#raw_arg_names),*
          }
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallNumber for #camel_case_raw_args_type {
        #[inline(always)]
        fn syscall_number(&self) -> isize {
          #syscall_const_name
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallGroupsGetter for #camel_case_raw_args_type {
        #[inline(always)]
        fn syscall_groups(&self) -> ::enumflags2::BitFlags<#crate_token::SyscallGroups> {
          use ::enumflags2::BitFlag;
          {
            #groups
          }.into()
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallStopInspect for #camel_case_raw_args_type {
        type Args = #camel_case_args_type;
        type Result = #camel_case_modified_args_type;
        fn inspect_sysenter(self, inspectee_pid: Pid) -> Self::Args {
          let raw_args = self;
          #(#inspects)*
          Self::Args {
            #(#arg_names),*
          }
        }
        fn inspect_sysexit(self, inspectee_pid: Pid, regs: &PtraceRegisters) -> Self::Result {
          let raw_args = self;
          #inspect_syscall_result
          if #is_syscall_failed {
            Self::Result {
              syscall_result,
              #(#modified_arg_names_err),*
            }
          } else {
            #(#inspect_modified_args)*
            Self::Result {
              #(#modified_arg_names),*
            }
          }
        }
      }
    },
    args_struct: quote_spanned! { span =>
      #[cfg(any(#(target_arch = #arch_names),*))]
      #[derive(Debug, Clone, PartialEq)]
      pub struct #camel_case_args_type {
        #(pub #arg_names: #wrapped_arg_types),*
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallNumber for #camel_case_args_type {
        #[inline(always)]
        fn syscall_number(&self) -> isize {
          #syscall_const_name
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallGroupsGetter for #camel_case_args_type {
        #[inline(always)]
        fn syscall_groups(&self) -> ::enumflags2::BitFlags<#crate_token::SyscallGroups> {
          use ::enumflags2::BitFlag;
          {
            #groups
          }.into()
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl From<#camel_case_args_type> for #crate_token::SyscallArgs {
        fn from(args: #camel_case_args_type) -> Self {
          #crate_token::SyscallArgs::#camel_case_ident(args)
        }
      }
    },
    modified_args_struct: quote_spanned! { span =>
      #[cfg(any(#(target_arch = #arch_names),*))]
      #[derive(Debug, Clone, PartialEq)]
      pub struct #camel_case_modified_args_type {
        #(pub #modified_arg_names: #modified_arg_types),*
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallNumber for #camel_case_modified_args_type {
        #[inline(always)]
        fn syscall_number(&self) -> isize {
          #syscall_const_name
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl #crate_token::SyscallGroupsGetter for #camel_case_modified_args_type {
        #[inline(always)]
        fn syscall_groups(&self) -> ::enumflags2::BitFlags<#crate_token::SyscallGroups> {
          use ::enumflags2::BitFlag;
          {
            #groups
          }.into()
        }
      }

      #[cfg(any(#(target_arch = #arch_names),*))]
      impl From<#camel_case_modified_args_type> for #crate_token::SyscallModifiedArgs {
        fn from(args: #camel_case_modified_args_type) -> Self {
          #crate_token::SyscallModifiedArgs::#camel_case_ident(args)
        }
      }

    },
    name: camel_case_ident,
    args_struct_type: camel_case_args_type,
    raw_args_struct_type: camel_case_raw_args_type,
    modified_args_struct_type: camel_case_modified_args_type,
    archs: arch_names.clone(),
    syscall_const_name,
  }
}

#[proc_macro]
pub fn gen_syscalls(input: TokenStream) -> TokenStream {
  let input = parse_macro_input!(input with Punctuated::<SyscallEntry, syn::Token![,]>::parse_terminated);
  let mut arg_structs = vec![];
  let mut raw_arg_structs = vec![];
  let mut modified_arg_structs = vec![];
  let mut names = vec![];
  let mut raw_arg_struct_types = vec![];
  let mut arg_struct_types = vec![];
  let mut modified_arg_struct_types = vec![];
  let mut supported_archs = vec![];
  let mut syscall_numbers = vec![];
  let crate_token = get_crate("ptrace-syscalls");
  let mut syscall_names_dedup = vec![];
  let mut supported_archs_dedup = vec![];
  let mut syscall_consts = vec![];
  for syscall in &input {
    let GenSyscallArgsStructResult {
      args_struct,
      name,
      args_struct_type,
      archs,
      syscall_number,
      raw_args_struct,
      raw_args_struct_type,
      modified_args_struct,
      modified_args_struct_type,
      syscall_const_name,
    } = gen_syscall_args_struct(syscall, crate_token.clone());
    arg_structs.push(args_struct);
    raw_arg_structs.push(raw_args_struct);
    modified_arg_structs.push(modified_args_struct);
    arg_struct_types.push(args_struct_type);
    raw_arg_struct_types.push(raw_args_struct_type);
    modified_arg_struct_types.push(modified_args_struct_type);
    names.push(name.clone());
    supported_archs.push(archs.clone());
    syscall_numbers.push(syscall_number.clone());
    syscall_consts.push(syscall_const_name);
    if syscall_names_dedup.last() != Some(&syscall.name) {
      syscall_names_dedup.push(syscall.name.clone());
      supported_archs_dedup.push(archs.clone());
    } else {
      supported_archs_dedup.last_mut().unwrap().extend(archs);
    }
  }
  TokenStream::from(quote::quote! {
    #(#raw_arg_structs)*
    #(#arg_structs)*
    #(#modified_arg_structs)*
    #[non_exhaustive]
    #[derive(Debug, Clone, Copy, PartialEq)]
    pub enum SyscallRawArgs {
      #(
        #[cfg(any(#(target_arch = #supported_archs),*))]
        #names(#raw_arg_struct_types),
      )*
      Unknown(#crate_token::UnknownArgs),
    }

    #[non_exhaustive]
    #[derive(Debug, Clone, PartialEq)]
    pub enum SyscallArgs {
      #(
        #[cfg(any(#(target_arch = #supported_archs),*))]
        #names(#arg_struct_types),
      )*
      Unknown(#crate_token::UnknownArgs),
    }

    #[non_exhaustive]
    #[derive(Debug, Clone, PartialEq)]
    pub enum SyscallModifiedArgs {
      #(
        #[cfg(any(#(target_arch = #supported_archs),*))]
        #names(#modified_arg_struct_types),
      )*
      Unknown(#crate_token::UnknownArgs),
    }

    impl #crate_token::SyscallNumber for SyscallRawArgs {
      #[inline(always)]
      fn syscall_number(&self) -> isize {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(args) => args.syscall_number(),
          )*
          Self::Unknown(args) => args.syscall_number(),
        }
      }
    }

    impl #crate_token::SyscallGroupsGetter for SyscallRawArgs {
      #[inline(always)]
      fn syscall_groups(&self) -> ::enumflags2::BitFlags<#crate_token::SyscallGroups> {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(args) => args.syscall_groups(),
          )*
          Self::Unknown(args) => args.syscall_groups(),
        }
      }
    }

    impl #crate_token::SyscallNumber for SyscallArgs {
      #[inline(always)]
      fn syscall_number(&self) -> isize {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(args) => args.syscall_number(),
          )*
          Self::Unknown(args) => args.syscall_number(),
        }
      }
    }

    impl #crate_token::SyscallGroupsGetter for SyscallArgs {
      #[inline(always)]
      fn syscall_groups(&self) -> ::enumflags2::BitFlags<#crate_token::SyscallGroups> {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(args) => args.syscall_groups(),
          )*
          Self::Unknown(args) => args.syscall_groups(),
        }
      }
    }

    impl SyscallRawArgs {
      fn from_regs(regs: &#crate_token::arch::PtraceRegisters) -> Self {
        use #crate_token::arch::syscall_no_from_regs;
        match syscall_no_from_regs!(regs) as isize {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            #syscall_numbers => {
              Self::#names(#raw_arg_struct_types::from_regs(regs))
            },
          )*
          _ => {
            Self::Unknown(UnknownArgs::from_regs(regs))
          }
        }
      }
    }

    impl SyscallStopInspect for SyscallRawArgs {
      type Args = SyscallArgs;
      type Result = SyscallModifiedArgs;

      fn inspect_sysenter(self, inspectee_pid: Pid) -> Self::Args {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(raw_args) => {
              SyscallArgs::#names(raw_args.inspect_sysenter(inspectee_pid))
            },
          )*
          Self::Unknown(unknown) => {
            SyscallArgs::Unknown(unknown)
          }
        }
      }

      fn inspect_sysexit(self, inspectee_pid: Pid, regs: &PtraceRegisters) -> Self::Result {
        match self {
          #(
            #[cfg(any(#(target_arch = #supported_archs),*))]
            Self::#names(raw_args) => {
              SyscallModifiedArgs::#names(raw_args.inspect_sysexit(inspectee_pid, regs))
            },
          )*
          Self::Unknown(unknown) => {
            SyscallModifiedArgs::Unknown(unknown)
          }
        }
      }
    }

    // This is not going to work. TODO: maybe create a rust issue for #[cfg(macro!(...))]?
    // #[macro_export]
    // macro_rules! cfg_if_has_syscall {
    //   // match if/else chains with a final `else`
    //   (
    //       $(
    //           if #[has $i_syscall:ident] { $( $i_tokens:tt )* }
    //       ) else+
    //       else { $( $e_tokens:tt )* }
    //   ) => {
    //     ::cfg_if::cfg_if! {
    //       $(
    //           if #[cfg($crate::cfg_if_has_syscall!(__internal__, $i_syscall))] { $( $i_tokens:tt )* }
    //       ) else+
    //       else { $( $e_tokens:tt )* }
    //     }
    //   };

    //   // match if/else chains lacking a final `else`
    //   (
    //       if #[has $i_syscall:ident] { $( $i_tokens:tt )* }
    //       $(
    //           else if #[has $e_syscall:ident] { $( $e_tokens:tt )* }
    //       )*
    //   ) => {
    //     ::cfg_if::cfg_if! {
    //       if #[cfg($crate::cfg_if_has_syscall!(__internal__, $i_syscall))] { $( $i_tokens:tt )* }
    //       $(
    //           else if #[cfg($crate::cfg_if_has_syscall!(__internal__, $e_syscall))] { $( $e_tokens:tt )* }
    //       )*
    //     }
    //   };
    //   #(
    //     (__internal__, #syscall_names_dedup) => {
    //       any(#(target_arch = #supported_archs_dedup),*)
    //     }
    //   );*
    // }
  })
}

fn get_crate(name: &str) -> proc_macro2::TokenStream {
  let found_crate = crate_name(name).unwrap_or_else(|_| panic!("`{}` not found in `Cargo.toml`", name));

  match found_crate {
    FoundCrate::Itself => quote!(crate),
    FoundCrate::Name(name) => {
      let ident = format_ident!("{}", &name);
      quote!( #ident )
    }
  }
}

/// returns: (wrapped type, needs memory inspection)
fn wrap_syscall_arg_type(ty: &Type, span: Span) -> (proc_macro2::TokenStream, bool) {
  match ty {
    Type::Array(ty) => {
      let element_ty = &ty.elem;
      (quote_spanned!(span =>Result<#ty, InspectError<Vec<#element_ty>>>), true)
    }
    Type::Path(ty) => {
      assert_eq!(ty.path.segments.len(), 1);
      let ty = &ty.path.segments[0];
      let ty_str = ty.to_token_stream().to_string();
      match ty_str.as_str() {
        "Unit" => (quote_spanned!(span => ()), false),
        // Primitive types
        "RawFd" | "socklen_t" | "c_int" | "c_uint" | "c_ulong" | "c_long" | "i16" | "i32" | "i64" | "u64" | "usize"
        | "isize" | "size_t" | "key_serial_t" | "AddressType" | "mode_t" | "uid_t" | "pid_t" | "gid_t" | "off_t"
        | "u32" | "clockid_t" | "id_t" | "key_t" | "mqd_t" | "aio_context_t" | "dev_t" | "nfds_t" | "loff_t"
        | "qid_t" | "idtype_t" | "time_t" | "timer_t" => (ty.to_token_stream(), false),
        // Types that need to be wrapped
        "sockaddr"
        | "CString"
        | "PathBuf"
        | "iovec"
        | "timex"
        | "cap_user_header"
        | "cap_user_data"
        | "timespec"
        | "clone_args"
        | "epoll_event"
        | "sigset_t"
        | "stat"
        | "statfs"
        | "futex_waitv"
        | "user_desc"
        | "itimerval"
        | "rlimit"
        | "rusage"
        | "timeval"
        | "__aio_sigset"
        | "timezone"
        | "iocb"
        | "io_event"
        | "io_uring_params"
        | "open_how"
        | "landlock_ruleset_attr"
        | "mq_attr"
        | "sigevent"
        | "msqid_ds"
        | "pollfd"
        | "__mount_arg"
        | "msghdr"
        | "riscv_hwprobe"
        | "siginfo_t"
        | "sched_attr"
        | "sched_param"
        | "stack_t"
        | "mnt_id_req"
        | "statx"
        | "sysinfo"
        | "itimerspec"
        | "tms"
        | "utsname"
        | "ustat"
        | "shmid_ds"
        | "cachestat"
        | "cachestat_range" => (quote_spanned!(span => InspectResult<#ty>), true),
        _ => {
          if ty.ident == "Option" {
            let PathArguments::AngleBracketed(arg) = &ty.arguments else {
              panic!("Unsupported syscall arg type: {:?}", ty_str);
            };
            let argstr = arg.args.to_token_stream().to_string();
            match argstr.as_str() {
              "PathBuf" | "timespec" | "Vec < CString >" | "CString" | "Vec < c_ulong >" | "Vec < c_uint >"
              | "Vec < gid_t >" | "timezone" | "mq_attr" | "siginfo_t" | "sigset_t" | "iovec" | "rlimit64"
              | "fd_set" | "sockaddr" | "sigaction" | "timeval" | "itimerval" | "stack_t" | "timer_t" | "time_t"
              | "sigevent" | "itimerspec" | "utimbuf" | "rusage" => (quote_spanned!(span => InspectResult<#ty>), true),
              "[timespec; 2]" | "[timeval; 2]" | "[timespec ; 2]" | "[timeval ; 2]" => {
                let GenericArgument::Type(inner) = arg.args.first().unwrap() else {
                  panic!("Unsupported inner syscall arg type: {:?}", argstr);
                };
                let Type::Array(inner) = inner else {
                  panic!("Unsupported inner syscall arg type: {:?}", argstr)
                };
                let element_ty = &inner.elem;
                (quote_spanned!(span => Result<#ty, InspectError<Vec<#element_ty>>>), true)
              }
              _ => panic!("Unsupported inner syscall arg type: {:?}", argstr),
            }
          } else if ty.ident == "Vec" {
            let PathArguments::AngleBracketed(arg) = &ty.arguments else {
              panic!("Unsupported inner syscall arg type: {:?}", ty_str);
            };
            let arg = arg.args.to_token_stream().to_string();
            match arg.as_str() {
              "c_int" | "u8" | "CString" | "epoll_event" | "futex_waitv" | "c_ulong" | "linux_dirent" | "io_event"
              | "linux_dirent64" | "gid_t" | "AddressType" | "kexec_segment" | "c_uchar" | "u64" | "mount_attr"
              | "pollfd" | "iovec" | "riscv_hwprobe" | "mmsghdr" | "sembuf" => {
                (quote_spanned!(span => InspectResult<#ty>), true)
              }
              _ => panic!("Unsupported inner syscall arg type: {:?}", arg),
            }
          } else if ty.ident == "InspectResult" {
            (quote_spanned!(span => #ty), true)
          } else if ty.ident == "Arc" {
            let PathArguments::AngleBracketed(arg) = &ty.arguments else {
              panic!("Unsupported inner syscall arg type: {:?}", ty_str);
            };
            let arg = arg.args.to_token_stream().to_string();
            match arg.as_str() {
              "rseq" | "statmount" | "msgbuf" | "file_handle" | "xattr_args" | "mount_attr" => {
                (quote_spanned!(span => InspectResult<#ty>), true)
              }
              _ => panic!("Unsupported inner syscall arg type: {:?}", arg),
            }
          } else {
            panic!("Unsupported syscall arg type: {:?}", ty_str);
          }
        }
      }
    }
    _ => panic!("Multi segment path is not supported here"),
  }
}