-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bypass untyped template arguments to C macro #14559
Comments
Use emit ? |
|
This should work: {.emit: """
/*INCLUDESECTION*/
#include <sys/sdt.h>
""".}
template DTRACE_PROBE(namespace, name: untyped) =
{.emit: ["DTRACE_PROBE(", astToStr(namespace), ", ", astToStr(name), ");"].}
proc main() =
DTRACE_PROBE(a, b)
main() Quirks:
|
I believe this is one the things that #13953 helps with |
I think the best way for now would be to translate the macro you're wrapping into Nim. I do understand that some macros can be ridiculously complex though, so maybe we can provide a specialized flag which can be used for |
I don't think so, more |
that comment seems like a copy paste from #13953 (comment) ; I don't see why that's a hack, it allows you to control which C section it goes to, including HERE, which the current emit doesn't allow (see #13943) |
The The name of the flag makes no sense though. In the perspective of top-level statements, |
I'd prefer that |
alternative name suggestions welcome, but it conveys that the code is emitted at where the emit appears (or at least at beginning of proc) instead of moved to a top level section, so I think the name reflects this well. See example usages here https://github.com/nim-lang/Nim/pull/13953/files#diff-b549242434b5227b402f18f17c4752ec
emit is a necessary evil; you should only use it when other approaches don't work. But for things like C function macros or anything that's not a function call, it remains awefully useful and practical, and duplicating macro content is often undesirable, unportable, or impossible (eg, the stringfification, c_sizeof to verify sizeof, Shifting this discussion to dtrace, and regardless whether this issue can be closed, I think we could start offering dtrace-specific apis to make it easier to use in nim; it's an incredibly powerful tool for debugging (performance, counters, logic etc). Allows tracing running applications not written with dtrace in mind, or custom tracing of applications with dtrace hooks that can be changed at runtime. All with zero overhead if you're not tracing, so it's easy to switch on/off. As an example, nim binary itself could be instrumented to have counters for various things (number of allocations, number of calls to key functions, vm calls etc) |
Sounds like it could be a library |
yes; the D language (not the dlang one ;-)) could potentially be generated from nim or some nim DSL too. |
Hope nim compiler can support usdt. I will close this issue because @alaviss solve my problem, thank you! |
I'd like to add usdt to my program, but no way to bind
<sys/sdt.h>
due to untyped C macro arguments.Description
In the C code below, both
test
andp1
are untyped to C compiler, which create a probe point at compile time.To run it, Install dependencies and build
test.c
Use
bpftrace
to detect hitting of probe pointEvery time
test
runs,bpftrace
printHello
.If nim can accept template in below form, maybe more C macro can be bound.
Alternatives
<sys/sdt.h>
is static way to add user probe points, if solution above is too dirty to implement, the only way to support usdt will be to bind libstapsdt which generate usdt at runtime, but since nim is static and system language, static way is more appropriate in my opinion.Additional Information
The text was updated successfully, but these errors were encountered: