Skip to content

About

C library for a manual function call trace.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Developers Toolbox

Libbacktrace

C library for a manual function call trace.

Each trace is one list of frames with a capacity chosen at creation. A push records a file, a line, and a function name. A pop removes the newest frame when the function name matches that frame. The trace type is opaque. The public header is include/backtrace.h. It uses C linkage, so a C++ translation unit can include it.

This is a list the program records itself. It does not walk the machine stack.

Requirements

  • GNU make.
  • gcc, unless CC is set. The C skill uses gcc for syntax checks.
  • cppcheck, for make c-check.

Build

make lib writes the static archive build/libbacktrace.a. The archive is built as C11 with -Wall -Wextra -Wpedantic -Werror. The compiler is gcc unless CC is set. make clean removes build/.

make lib
cc -std=c11 -Iinclude -o demo demo.c build/libbacktrace.a

make lib CC=clang selects the compiler. make lib CFLAGS=-g adds flags to that C11 build. make with no target prints the makefile help.

make update refreshes the makefile skills. make lib and make test do not need that checkout. make c-check and make version do.

Quick Start

Create a trace, record the current function, and destroy it. Destroy frees the trace.

#include "backtrace.h"

int main(void)
{
    backtrace *trace = NULL;

    if (backtrace_create(&trace, 32) != BACKTRACE_OK) {
        return 1;
    }

    if (BACKTRACE_PUSH(trace) != BACKTRACE_OK) {
        backtrace_destroy(&trace);
        return 1;
    }

    BACKTRACE_POP(trace);
    backtrace_destroy(&trace);
    return 0;
}

Frames

Index 0 is the oldest frame. backtrace_depth() - 1 is the newest frame. backtrace_frame reads one frame and does not change the trace.

backtrace_format writes the whole trace into a new string, newest frame first. Each line is index: function (at file:line). An empty trace is an empty string. The caller frees that string with free.

A push stores the file and function pointers it is given. It does not copy the text. Both strings must stay valid until that frame is popped. __FILE__ and __func__ do.

A line is stored as given, including 0 and a negative line.

Ownership

The trace owns its frame slots. The caller does not free the trace pointer. backtrace_destroy frees the slots and the trace, then sets *trace to NULL.

backtrace_format owns the string it writes until the caller frees it.

A pop removes the newest frame only when function matches that frame. A name further down the trace returns BACKTRACE_INVAL and leaves every frame in place. A full trace returns BACKTRACE_INVAL and leaves the list unchanged.

BACKTRACE_PUSH and BACKTRACE_POP record the current function. They do not check the return code.

Note: Callers serialise use of a given trace.

Return Codes

Every function that can fail returns an int.

Code Value Meaning
BACKTRACE_OK 0 The call succeeded.
BACKTRACE_NOMEM -1 Allocation failed. The trace is unchanged. Create sets *trace to NULL.
BACKTRACE_INVAL -2 The trace or out-pointer is missing, the name is empty, the trace is full, the pop does not match, or the index is past the last frame.

backtrace_destroy(NULL) returns BACKTRACE_INVAL. backtrace_destroy on a pointer that is already NULL returns BACKTRACE_OK and leaves it NULL.

A depth of 0 leaves *trace unchanged. A depth that cannot be allocated sets *trace to NULL.

Functions

typedef struct backtrace backtrace;

int backtrace_create(backtrace **trace, size_t depth);
int backtrace_destroy(backtrace **trace);
int backtrace_push(backtrace *trace, const char *file, int line,
                   const char *function);
int backtrace_pop(backtrace *trace, const char *function);
int backtrace_frame(const backtrace *trace, size_t index, const char **file,
                    int *line, const char **function);
size_t backtrace_depth(const backtrace *trace);
int backtrace_format(const backtrace *trace, char **text);
Name Purpose
backtrace_create Allocate a trace with room for depth frames.
backtrace_destroy Free the trace and clear *trace.
backtrace_push Record a file, line, and function name.
backtrace_pop Remove the newest frame when the function name matches.
backtrace_frame Read the frame at index. Index 0 is the oldest.
backtrace_depth Return how many frames are currently recorded.
backtrace_format Write the trace to a new string, newest frame first.

backtrace_depth(NULL) returns 0. backtrace_pop of an empty trace returns BACKTRACE_INVAL. backtrace_format of an empty trace returns an empty string.

Empty Trace

A new trace has a depth of 0. Destroying it returns BACKTRACE_OK and sets the caller's pointer to NULL. Destroying that NULL pointer again returns BACKTRACE_OK.

Version

The version text is the macro BACKTRACE_VERSION_STRING in include/backtrace.h. It is a dotted semantic version. .bumpversion.toml is the version source, and a bump updates that macro.

After make init, make version prints the version from .bumpversion.toml.

Development

make update
make lib
make check
make clean

make c-check syntax-checks every .c and .h file, then runs cppcheck. make check runs c-check and then the behaviour tests. make test runs only the behaviour tests.

The Lupaxa Project Footer

About

C library for a manual function call trace.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages