Use cases of the GCC "artificial" function attribute

attributes, c, gcc

Solution

The other answer isn't wrong, but perhaps I can explain it a bit better.

Imagine this function in `foo.c`, with line numbers:

10: static inline int foo(struct q *x)
11: {
12:     return bar(x + 1);
13: }

This is called twice from another function:

20: void baz(void)
21: {
22:     x = foo(qa);
23:     x = foo(qb);
24: }

Unfortunately, `bar()` crashes. Here's the backtrace: ` #0 0x00000000004b1a2a in bar (x=0x8) at foo.c:5 #1 0x0000000000416ee0 in baz () at foo.c:12 #2 0x0000000000413fab in main () at foo.c:30 `

Since `foo` is inlined, it's not part of the backtrace, but wait, `foo.c:12` is in `foo`, and below it is just the line in `main`. There's nothing to tell us what line in `baz` caused the crash.

If we mark foo as artificial, we'd instead get this backtrace: ` #0 0x00000000004b1a2a in bar (x=0x8) at foo.c:5 #1 0x0000000000416ee0 in baz () at foo.c:22 #2 0x0000000000413fab in main () at foo.c:30 `

It no longer points to `foo`. Instead it shows us where `foo` was called from `foo.c:22`. Suddendly it's easy to tell that `qa` is the problematic variable.

Problem

I just read about the GCC function attribute `artificial` but did not quite get the description. Can you give me some examples where it is useful?

Original source