Can anyone please share how to modify go code by calling go build -a -toolexec <mytool>
What's the goal?
Log what function was called.
So that moontrace will add a print at the beginning of each function body, for example:
fmt.Println(fmt.Sprintf("Calling %s func", funcName))
Motivation?
At the moment just for the sake of experimentation and trying to reduce some of the routine when developing and debugging personal projects.
What I tried so far
Prerequisite: unfortunately, I have found mercilessly little information regarding the use of the toolexec flag, so all attempts made are made more on the basis of indirect inference.
My final result so far: https://go.dev/play/p/Jk_vWDY7fO4
Everything looks as if the approach itself is incorrect. My personal feeling is that compilation should be handled by tool, which is now proxied through toolexec, rather than an explicit call to go build inside moontrace code, but i might be wrong.
Of interest: a video was recently released from a developer at Datadog where they used toolexec to autoinstrument code. And at the stage of explaining how it all works, they showed the example of a commandline tool thats run for toolexec tool:
https://youtu.be/5l-W7vPSbuc?si=tDocHZREZPgIUpWb&t=1924
(timecode included)
But when I output the result of os.Args inside moontrace code, all I see is the following:
[/Users/mark/Projects/moontrace/moontrace /Users/mark/sdk/go1.22.0/pkg/tool/darwin_arm64/compile -V=full]
So even if I want to use go build call, at the moontrace stage of execution, I lose all the options that were passed to build command.
I've also found projects that actively use toolexec, like xgo or burrowers/garble, but honestly I don't have enough experience to get the basics out of them.
I would like to know only the most basic things, for modifying code at compile time with toolexec, without modifying the source code.
Thank you in advance!
This talk presents Orchestrion, a tool that we are developing at Datadog for the automatic instrumentation of Go source code. There are other tools (some open source) that add tracing information to Go programs. What makes Orchestrion’s design interesting is that it’s meant to be extensible, both in how it can find what should be instrumented an...
