From 49519f067f7fc9bfbf869e6851a4d398a9f7863f Mon Sep 17 00:00:00 2001 From: Florent Revest Date: Thu, 8 Jun 2023 17:56:49 +0200 Subject: pkg/csource: annotate syscall() args with their names This annotates syscall arguments so they are easier to read without having to pull out the syscall's man page. E.g: syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul, ... Signed-off-by: Florent Revest --- pkg/csource/csource.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'pkg/csource/csource.go') diff --git a/pkg/csource/csource.go b/pkg/csource/csource.go index 0c9f78e71..54cf2fa39 100644 --- a/pkg/csource/csource.go +++ b/pkg/csource/csource.go @@ -351,25 +351,27 @@ func (ctx *context) fmtCallBody(call prog.ExecCall) string { } funcName = fmt.Sprintf("((intptr_t(*)(%v))CAST(%v))", args, callName) } - for _, arg := range call.Args { + for i, arg := range call.Args { switch arg := arg.(type) { case prog.ExecArgConst: if arg.Format != prog.FormatNative && arg.Format != prog.FormatBigEndian { panic("string format in syscall argument") } + com := ctx.argComment(call.Meta.Args[i], arg) suf := ctx.literalSuffix(arg, native) - argsStrs = append(argsStrs, handleBigEndian(arg, ctx.constArgToStr(arg, suf))) + argsStrs = append(argsStrs, com+handleBigEndian(arg, ctx.constArgToStr(arg, suf))) case prog.ExecArgResult: if arg.Format != prog.FormatNative && arg.Format != prog.FormatBigEndian { panic("string format in syscall argument") } + com := ctx.argComment(call.Meta.Args[i], arg) val := ctx.resultArgToStr(arg) if native && ctx.target.PtrSize == 4 { // syscall accepts args as ellipsis, resources are uint64 // and take 2 slots without the cast, which would be wrong. val = "(intptr_t)" + val } - argsStrs = append(argsStrs, val) + argsStrs = append(argsStrs, com+val) default: panic(fmt.Sprintf("unknown arg type: %+v", arg)) } @@ -499,6 +501,10 @@ func (ctx *context) copyout(w *bytes.Buffer, call prog.ExecCall, resCopyout bool } } +func (ctx *context) argComment(field prog.Field, arg prog.ExecArg) string { + return "/*" + field.Name + "=" + "*/" +} + func (ctx *context) constArgToStr(arg prog.ExecArgConst, suffix string) string { mask := (uint64(1) << (arg.Size * 8)) - 1 v := arg.Value & mask -- cgit mrf-deployment