Mount ftrace control directory on Void Linux

On Void Linux, the /sys/kernel/tracing is empty, which means the ftrace control directory is not mounted automatically, so you need mount it manually:

# mount -t tracefs nodev /sys/kernel/tracing
# cd /sys/kernel/tracing
# ls
README                      free_buffer               set_event               trace_clock
available_events            function_profile_enabled  set_event_notrace_pid   trace_marker
available_filter_functions  hwlat_detector            set_event_pid           trace_marker_raw
......

Fix “glibconfig.h: No such file or directory” error

I installed glib-devel on Void Linux, and wrote a simple program for test. But the compilation reported following error:

# gcc -I/usr/include/glib-2.0 main.c -lglib-2.0
......
/usr/include/glib-2.0/glib/gtypes.h:32:10: fatal error: glibconfig.h: No such file or directory
   32 | #include <glibconfig.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

glibconfig.h is in /usr/lib/glib-2.0/include directory, so it should be added into header file search path:

# gcc -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include main.c -lglib-2.0
#

Fix “Address 0xxxxxxxxx out of bounds” issue

Yesterday, I came across a crash issue when program accessed “out of bound” memory:

......
func = 0x7ffff7eb1dc8 <Address 0x7ffff7eb1dc8 out of bounds>
......

After some debugging, I found the reason is program uses dlopen to load some dynamic library, records the memory address of the library, but still read the memory after dlclose it.

__COUNTER__ macro in gcc/clang

Both gcc and clang define __COUNTER__ marco:

Defined to an integer value that starts at zero and is incremented each time the COUNTER macro is expanded.

Check following code:

# cat foo.c
#include <stdio.h>

void foo1(void)
{
    printf("%s:%d\n", __func__, __COUNTER__);
}

void foo2(void)
{
    printf("%s:%d\n", __func__, __COUNTER__);
}
# cat bar.c
#include <stdio.h>

void bar1(void)
{
    printf("%s:%d\n", __func__, __COUNTER__);
}

void bar2(void)
{
    printf("%s:%d\n", __func__, __COUNTER__);
}
# cat main.c
#include "foo.h"
#include "bar.h"

int main(void)
{
    foo1();
    foo2();
    bar1();
    bar2();
    return 0;
}

Run the program:

# ./main
foo_1:0
foo_2:1
bar_1:0
bar_2:1

You can see for every translate unit (.c) file, the __COUNTER__ begins at 0.

P.S., the code can be referenced here.