Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forced to define Go struct for casting an unsafe.Pointer() to a C struct

Interoperating with C code, I was not able to directly cast a structure and I was forced to define an equivalent one in Go. The C function from libproc.h is

int proc_pidinfo(int pid, int flavor, uint64_t arg,  void *buffer, int buffersize)

The C structure for flavor==PROC_PIDTASKINFO is proc_taskinfo as defined in sys/proc_info.h (included by libproc.h):

struct proc_taskinfo {
    uint64_t        pti_virtual_size;   /* virtual memory size (bytes) */
    uint64_t        pti_resident_size;  /* resident memory size (bytes) */
    uint64_t        pti_total_user;     /* total time */
    uint64_t        pti_total_system;
    uint64_t        pti_threads_user;   /* existing threads only */
    uint64_t        pti_threads_system;
    int32_t         pti_policy;     /* default policy for new threads */
    int32_t         pti_faults;     /* number of page faults */
    int32_t         pti_pageins;        /* number of actual pageins */
    int32_t         pti_cow_faults;     /* number of copy-on-write faults */
    int32_t         pti_messages_sent;  /* number of messages sent */
    int32_t         pti_messages_received;  /* number of messages received */
    int32_t         pti_syscalls_mach;  /* number of mach system calls */
    int32_t         pti_syscalls_unix;  /* number of unix system calls */
    int32_t         pti_csw;            /* number of context switches */
    int32_t         pti_threadnum;      /* number of threads in the task */
    int32_t         pti_numrunning;     /* number of running threads */
    int32_t         pti_priority;       /* task priority*/
};

Even if Go code actually works, I was not able to use C.proc_taskinfo directly. The Go function is propertiesOf(): complete source here.

If I reference the C structure, I got a similar error reported as in my latest question on the subject: could not determine kind of name for C.proc_taskinfo, but this time I'm sure the definition is imported with #include.

like image 491
gsscoder Avatar asked Mar 16 '23 21:03

gsscoder


1 Answers

As per documentation

To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.

Use C.struct_proc_taskinfo.

like image 106
Ainar-G Avatar answered Apr 06 '23 09:04

Ainar-G