Skip to content

Commit 62c15c6

Browse files
committed
Add support for environment based debug
The following environment variables can be set: - LIBOCXL_INFO print information about the version of LibOCXL in use - LIBOCXL_VERBOSE_ERRORS_ALL force verbose error reporting - LIBOCXL_TRACE_ALL force all AFU tracing Also add ocxl_info() to return the build information as a string. Signed-off-by: Alastair D'Silva <alastair@d-silva.org>
1 parent 9f2fa2e commit 62c15c6

11 files changed

+122
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*~
22
*.orig
33
.*.swp
4+
src/libocxl_info.h
45
obj/*
56
testobj/*
67
sampleobj/*

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ else
3333
endif
3434
endif
3535

36+
src/libocxl_info.h: version.pl
37+
VERSION_MAJOR=${VERSION_MAJOR} VERSION_MINOR=${VERSION_MINOR} \
38+
CC="${CC}" CFLAGS="${CFLAGS}" \
39+
./version.pl > src/libocxl_info.h
40+
3641
obj:
3742
mkdir obj
3843

Makefile.rules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ define Q
1919
endef
2020
endif
2121

22-
obj/%.o : src/%.c src/include/libocxl.h src/libocxl_internal.h | obj
22+
obj/%.o : src/%.c src/include/libocxl.h src/libocxl_internal.h src/libocxl_info.h | obj
2323
$(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<, $@)
2424

25-
testobj/%.o : src/%.c src/include/libocxl.h src/libocxl_internal.h | testobj
25+
testobj/%.o : src/%.c src/include/libocxl.h src/libocxl_internal.h src/libocxl_info.h | testobj
2626
$(call Q,CC, $(CC) $(CPPFLAGS) $(TESTCFLAGS) -c -o $@ $<, $@)
2727

2828
testobj/%.o-test : unittests/%.c testobj/libocxl.a | testobj

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ A typical use of libocxl will follow this pattern:
7171
10. **Termination:** ocxl\_afu\_close() will free all resources associated with an AFU handle.
7272

7373
# Development
74+
The following environment variables may be set (to 1 or "YES") to assist with development:
75+
**LIBOCXL_INFO** Print information about the LibOCXL build to stderr. This should be included in any bug reports.
76+
**LIBOCXL_TRACE_ALL** Force AFU interaction trace messages to be emitted for all AFUs unless explicitly disabled.
77+
**LIBOCXL_VERBOSE_ERRORS_ALL** Force verbose errors to be emitted for any failed LibOCXL calls, unless explicitly disabled.
78+
7479
Patches may be submitted via Github pull requests. Please prepare your patches
7580
by running `make precommit` before committing your work, and addressing any warnings & errors reported.
7681
Patches must compile cleanly with the latest stable version of GCC to be accepted.

src/afu.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ static void afu_init(ocxl_afu *afu)
254254

255255
afu->pasid = UINT32_MAX;
256256

257-
afu->verbose_errors = false;
257+
afu->verbose_errors = verbose_errors_all;
258258
afu->error_handler = ocxl_default_afu_error_handler;
259259

260-
afu->tracing = false;
260+
afu->tracing = tracing_all;
261261

262262
afu->attached = false;
263263

@@ -540,6 +540,8 @@ static ocxl_err get_afu_by_path(const char *path, ocxl_afu_h *afu)
540540
*/
541541
ocxl_err ocxl_afu_open_from_dev(const char *path, ocxl_afu_h *afu)
542542
{
543+
libocxl_init();
544+
543545
ocxl_err rc = get_afu_by_path(path, afu);
544546

545547
if (rc != OCXL_OK) {
@@ -577,6 +579,8 @@ ocxl_err ocxl_afu_open_specific(const char *name, const char *physical_function,
577579
ocxl_err ret = OCXL_INTERNAL_ERROR;
578580
*afu = OCXL_INVALID_AFU;
579581

582+
libocxl_init();
583+
580584
if (afu_index == -1) {
581585
snprintf(pattern, sizeof(pattern), "%s/%s.%s.*",
582586
DEVICE_PATH, name,

src/include/libocxl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ typedef struct ocxl_event {
152152
void ocxl_enable_messages(uint64_t sources);
153153
void ocxl_set_error_message_handler(void (*handler)(ocxl_err error, const char *message));
154154
const char *ocxl_err_to_string(ocxl_err err);
155+
const char *ocxl_info();
155156

156157
/* afu.c */
157158
/* AFU getters */

src/internal.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "libocxl_internal.h"
18+
#include "libocxl_info.h"
1819
#include <stdio.h>
1920
#include <stdbool.h>
2021
#include <stdarg.h>
@@ -25,6 +26,7 @@
2526
#include <fcntl.h>
2627
#include <errno.h>
2728
#include <string.h>
29+
#include <strings.h>
2830
#include <stdlib.h>
2931

3032
/// The base sysfs path for OCXL devices
@@ -33,14 +35,58 @@ const char *sys_path = NULL;
3335
/// The base device path for OCXL devices
3436
const char *dev_path = NULL;
3537

38+
bool libocxl_inited = false;
39+
pthread_mutex_t libocxl_inited_mutex = PTHREAD_MUTEX_INITIALIZER;
40+
3641
bool verbose_errors = false;
42+
bool verbose_errors_all = false;
3743
bool tracing = false;
44+
bool tracing_all = false;
3845

3946
void ocxl_default_error_handler(ocxl_err error, const char *message);
4047
void (*error_handler)(ocxl_err error, const char *message) = ocxl_default_error_handler;
4148

4249
pthread_mutex_t stderr_mutex = PTHREAD_MUTEX_INITIALIZER;
4350

51+
/**
52+
* Executed on first afu_open
53+
* - Check the LIBOCXL_INFO environment variable and output the info string
54+
* - Check the LIBOCXL_TRACE_ALL environment variable and enable tracing_all
55+
* - Check the LIBOCXL_VERBOSE_ERRORS_ALL environment variable and enable verbose_errors_all
56+
*/
57+
void libocxl_init()
58+
{
59+
const char *val;
60+
61+
pthread_mutex_lock(&libocxl_inited_mutex);
62+
if (libocxl_inited) {
63+
pthread_mutex_unlock(&libocxl_inited_mutex);
64+
return;
65+
}
66+
67+
val = getenv("LIBOCXL_INFO");
68+
if (val && (!strcasecmp(val, "yes") || !strcmp(val, "1"))) {
69+
fprintf(stderr, "%s", libocxl_info);
70+
}
71+
72+
val = getenv("LIBOCXL_TRACE_ALL");
73+
if (val && (!strcasecmp(val, "yes") || !strcmp(val, "1"))) {
74+
tracing_all = true;
75+
tracing = true;
76+
}
77+
78+
val = getenv("LIBOCXL_VERBOSE_ERRORS_ALL");
79+
if (val && (!strcasecmp(val, "yes") || !strcmp(val, "1"))) {
80+
verbose_errors_all = true;
81+
verbose_errors = true;
82+
}
83+
84+
libocxl_inited = true;
85+
86+
pthread_mutex_unlock(&libocxl_inited_mutex);
87+
}
88+
89+
4490
/**
4591
* Output a trace message
4692
* @param file the source filename

src/libocxl_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ do {\
6161
} while (0)
6262

6363
extern bool verbose_errors;
64+
extern bool verbose_errors_all;
6465
extern bool tracing;
66+
extern bool tracing_all;
6567
extern void (*error_handler)(ocxl_err error, const char *message);
68+
extern const char *libocxl_info;
6669

6770
typedef struct ocxl_afu ocxl_afu;
6871

@@ -186,5 +189,6 @@ struct ocxl_afu {
186189
};
187190

188191
void irq_dealloc(ocxl_afu *afu, ocxl_irq *irq);
192+
void libocxl_init();
189193

190194
#endif /* _LIBOCXL_INTERNAL_H */

src/setup.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ const char *ocxl_err_to_string(ocxl_err err)
143143
}
144144
}
145145

146+
/**
147+
* Get version & compilation information about LibOCXL. This must be included with
148+
* any bug report.
149+
*
150+
* The format and ordering of lines in this string may change.
151+
*
152+
* @return containing the version information
153+
*/
154+
const char *ocxl_info()
155+
{
156+
return libocxl_info;
157+
}
158+
146159
/**
147160
* @}
148161
*/

symver.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LIBOCXL_0 {
77
ocxl_enable_messages;
88
ocxl_set_error_message_handler;
99
ocxl_err_to_string;
10+
ocxl_info;
1011
ocxl_afu_get_identifier;
1112
ocxl_afu_get_device_path;
1213
ocxl_afu_get_sysfs_path;

version.pl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env perl
2+
3+
use English;
4+
use strict;
5+
use warnings;
6+
7+
my $compilerText = `$ENV{CC} --version`;
8+
my @compilerLines = split /\n/, $compilerText;
9+
chomp @compilerLines;
10+
11+
my $git = `which git`;
12+
my $gitHash = '';
13+
if ($git && -d '.git') {
14+
chomp $git;
15+
my $hash = `$git rev-parse HEAD`;
16+
chomp $hash;
17+
18+
my $dirty = '';
19+
my @changed = `git diff-index --name-only HEAD`;
20+
if (@changed) {
21+
$dirty = '-dirty';
22+
}
23+
24+
$gitHash = "\"Git hash: $hash$dirty\\n\"";
25+
}
26+
27+
my $platform = `uname -a`;
28+
chomp $platform;
29+
30+
print <<"EOF";
31+
const char *libocxl_info =
32+
"LibOCXL Version: $ENV{VERSION_MAJOR}.$ENV{VERSION_MINOR}\\n"
33+
"CC: $ENV{CC}\\n"
34+
"Compiler Version: $compilerLines[0]\\n"
35+
"CFLAGS: $ENV{CFLAGS}\\n"
36+
$gitHash
37+
"Platform: $platform\\n\\n";
38+
EOF

0 commit comments

Comments
 (0)