Skip to content

Initial definition for TSK_TRACE_ERRORS #3095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions c/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
[1.1.4] - 2024-XX-XX
--------------------

**Changes**

- Added the TSK_TRACE_ERRORS macro to enable tracing of errors in the C library.
This is useful for debugging as errors will print to stderr when set.
(:user:`jeromekelleher`, :pr:`3095`).

--------------------
[1.1.3] - 2024-10-16
Expand Down
7 changes: 6 additions & 1 deletion c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ project('tskit', ['c', 'cpp'],
default_options: ['c_std=c99', 'cpp_std=c++11']
)

debug_c_args = []
if get_option('buildtype').startswith('debug')
debug_c_args = ['-DTSK_TRACE_ERRORS']
endif

kastore_proj = subproject('kastore')
kastore_dep = kastore_proj.get_variable('kastore_dep')
kastore_inc = kastore_proj.get_variable('kastore_inc')
Expand All @@ -16,7 +21,7 @@ extra_c_args = [
'-Wmissing-prototypes', '-Wstrict-prototypes',
'-Wconversion', '-Wshadow', '-Wpointer-arith', '-Wcast-align',
'-Wcast-qual', '-Wwrite-strings', '-Wnested-externs',
'-fshort-enums', '-fno-common']
'-fshort-enums', '-fno-common'] + debug_c_args

lib_sources = [
'tskit/core.c', 'tskit/tables.c', 'tskit/trees.c',
Expand Down
22 changes: 11 additions & 11 deletions c/tskit/convert.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2018-2021 Tskit Developers
* Copyright (c) 2018-2025 Tskit Developers
* Copyright (c) 2015-2017 University of Oxford
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -68,11 +68,11 @@
const char *label_format = ms_labels ? "%d" : "n%d";

if (root < 0 || root >= (tsk_id_t) self->tree->num_nodes) {
ret = TSK_ERR_NODE_OUT_OF_BOUNDS;
ret = tsk_trace_error(TSK_ERR_NODE_OUT_OF_BOUNDS);
goto out;
}
if (buffer == NULL) {
ret = TSK_ERR_BAD_PARAM_VALUE;
ret = tsk_trace_error(TSK_ERR_BAD_PARAM_VALUE);
goto out;
}
root_parent = tree->parent[root];
Expand All @@ -82,7 +82,7 @@
v = stack[stack_top];
if (tree->left_child[v] != TSK_NULL && v != u) {
if (s >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
ret = tsk_trace_error(TSK_ERR_BUFFER_OVERFLOW);
goto out;
}
buffer[s] = '(';
Expand All @@ -104,17 +104,17 @@
}
if (label != -1) {
if (s >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
ret = tsk_trace_error(TSK_ERR_BUFFER_OVERFLOW);
goto out;
}
r = snprintf(buffer + s, buffer_size - s, label_format, label);
if (r < 0) {
ret = TSK_ERR_IO;
ret = tsk_trace_error(TSK_ERR_IO);

Check warning on line 112 in c/tskit/convert.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/convert.c#L112

Added line #L112 was not covered by tests
goto out;
}
s += (size_t) r;
if (s >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
ret = tsk_trace_error(TSK_ERR_BUFFER_OVERFLOW);
goto out;
}
}
Expand All @@ -123,12 +123,12 @@
r = snprintf(buffer + s, buffer_size - s, ":%.*f", (int) self->precision,
branch_length);
if (r < 0) {
ret = TSK_ERR_IO;
ret = tsk_trace_error(TSK_ERR_IO);

Check warning on line 126 in c/tskit/convert.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/convert.c#L126

Added line #L126 was not covered by tests
goto out;
}
s += (size_t) r;
if (s >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
ret = tsk_trace_error(TSK_ERR_BUFFER_OVERFLOW);
goto out;
}
if (v == tree->right_child[u]) {
Expand All @@ -141,7 +141,7 @@
}
}
if ((s + 1) >= buffer_size) {
ret = TSK_ERR_BUFFER_OVERFLOW;
ret = tsk_trace_error(TSK_ERR_BUFFER_OVERFLOW);
goto out;
}
buffer[s] = ';';
Expand All @@ -164,7 +164,7 @@
self->traversal_stack
= tsk_malloc(tsk_tree_get_size_bound(tree) * sizeof(*self->traversal_stack));
if (self->traversal_stack == NULL) {
ret = TSK_ERR_NO_MEMORY;
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);

Check warning on line 167 in c/tskit/convert.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/convert.c#L167

Added line #L167 was not covered by tests
goto out;
}
out:
Expand Down
24 changes: 14 additions & 10 deletions c/tskit/core.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019-2024 Tskit Developers
* Copyright (c) 2019-2025 Tskit Developers
* Copyright (c) 2015-2018 University of Oxford
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -43,22 +43,24 @@
get_random_bytes(uint8_t *buf)
{
/* Based on CPython's code in bootstrap_hash.c */
int ret = TSK_ERR_GENERATE_UUID;
int ret = 0;
HCRYPTPROV hCryptProv = (HCRYPTPROV) NULL;

if (!CryptAcquireContext(
&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);
goto out;
}
if (!CryptGenRandom(hCryptProv, (DWORD) UUID_NUM_BYTES, buf)) {
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);
goto out;
}
if (!CryptReleaseContext(hCryptProv, 0)) {
hCryptProv = (HCRYPTPROV) NULL;
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);
goto out;
}
hCryptProv = (HCRYPTPROV) NULL;
ret = 0;
out:
if (hCryptProv != (HCRYPTPROV) NULL) {
CryptReleaseContext(hCryptProv, 0);
Expand All @@ -72,19 +74,21 @@
static int TSK_WARN_UNUSED
get_random_bytes(uint8_t *buf)
{
int ret = TSK_ERR_GENERATE_UUID;
int ret = 0;
FILE *f = fopen("/dev/urandom", "r");

if (f == NULL) {
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);

Check warning on line 81 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L81

Added line #L81 was not covered by tests
goto out;
}
if (fread(buf, UUID_NUM_BYTES, 1, f) != 1) {
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);

Check warning on line 85 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L85

Added line #L85 was not covered by tests
goto out;
}
if (fclose(f) != 0) {
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);

Check warning on line 89 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L89

Added line #L89 was not covered by tests
goto out;
}
ret = 0;
out:
return ret;
}
Expand All @@ -111,7 +115,7 @@
buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12],
buf[13], buf[14], buf[15])
< 0) {
ret = TSK_ERR_GENERATE_UUID;
ret = tsk_trace_error(TSK_ERR_GENERATE_UUID);

Check warning on line 118 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L118

Added line #L118 was not covered by tests
goto out;
}
out:
Expand Down Expand Up @@ -764,7 +768,7 @@

tsk_memset(self, 0, sizeof(tsk_blkalloc_t));
if (chunk_size < 1) {
ret = TSK_ERR_BAD_PARAM_VALUE;
ret = tsk_trace_error(TSK_ERR_BAD_PARAM_VALUE);
goto out;
}
self->chunk_size = chunk_size;
Expand All @@ -775,12 +779,12 @@
self->num_chunks = 0;
self->mem_chunks = malloc(sizeof(char *));
if (self->mem_chunks == NULL) {
ret = TSK_ERR_NO_MEMORY;
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);

Check warning on line 782 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L782

Added line #L782 was not covered by tests
goto out;
}
self->mem_chunks[0] = malloc(chunk_size);
if (self->mem_chunks[0] == NULL) {
ret = TSK_ERR_NO_MEMORY;
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);

Check warning on line 787 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L787

Added line #L787 was not covered by tests
goto out;
}
self->num_chunks = 1;
Expand Down Expand Up @@ -1256,7 +1260,7 @@
+ (num_bits % TSK_BIT_ARRAY_NUM_BITS ? 1 : 0);
self->data = tsk_calloc(self->size * length, sizeof(*self->data));
if (self->data == NULL) {
ret = TSK_ERR_NO_MEMORY;
ret = tsk_trace_error(TSK_ERR_NO_MEMORY);

Check warning on line 1263 in c/tskit/core.c

View check run for this annotation

Codecov / codecov/patch

c/tskit/core.c#L1263

Added line #L1263 was not covered by tests
goto out;
}
out:
Expand Down
17 changes: 16 additions & 1 deletion c/tskit/core.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2019-2024 Tskit Developers
* Copyright (c) 2019-2025 Tskit Developers
* Copyright (c) 2015-2018 University of Oxford
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -953,6 +953,21 @@ not be freed by client code.
*/
const char *tsk_strerror(int err);

#ifdef TSK_TRACE_ERRORS

static inline int
_tsk_trace_error(int err, int line, const char *file)
{
fprintf(stderr, "tskit-trace-error: %d='%s' at line %d in %s\n", err,
tsk_strerror(err), line, file);
return err;
}

#define tsk_trace_error(err) (_tsk_trace_error(err, __LINE__, __FILE__))
#else
#define tsk_trace_error(err) (err)
#endif

#ifndef TSK_BUG_ASSERT_MESSAGE
#define TSK_BUG_ASSERT_MESSAGE \
"If you are using tskit directly please open an issue on" \
Expand Down
Loading
Loading