variable type passed in call

I want to get the task ID from the syscall function defined in libc as the function gettid() cannot be directly called in the same manner as getpid(). In C, it should be called from syscall function as

tid = syscall(SYS_gettid)

where SYS_gettid is defined as __NR_getid in the header bits/syscall.h with the type of long number.

I tried within julia

ccall((:syscall,“libc”), Int32, (Any,),“__NR_gettid”)

But it returns to -1 which indicate syscall returns to an error.

How should I do it?

(I can’t answer your question, but you should read PSA: how to quote code with backticks)

__NR_getid isn’t a string, it’s a C preprocessor constant holding an integer. So you need to pass that integer. Unfortunately it seems to change depending on the platform:

$ grep NR_gettid -R /usr/include
/usr/include/asm-generic/unistd.h:#define __NR_gettid 178
/usr/include/asm-generic/unistd.h:__SYSCALL(__NR_gettid, sys_gettid)
/usr/include/asm/unistd_32.h:#define __NR_gettid 224
/usr/include/asm/unistd_64.h:#define __NR_gettid 186
/usr/include/asm/unistd_x32.h:#define __NR_gettid (__X32_SYSCALL_BIT + 186)
/usr/include/bits/syscall.h:#ifdef __NR_gettid
/usr/include/bits/syscall.h:# define SYS_gettid __NR_gettid

I’m not sure what’s the best solution to that. Probably just add conditions based on the platform.