commit 7b5bb3a65ee46f683b5f86f2815e9fa019aa91d1
parent 1c3d3cddd6063221ca620aa9a6434631f1a1c7c4
Author: Andreas Gruhler <in0rdr@noreply.codeberg.org>
Date: Sat, 7 May 2022 22:12:19 +0200
Merge pull request 'Add LTTng Instrumentation' (#9) from feat/lttng into master
Reviewed-on: https://codeberg.org/in0rdr/diary/pulls/9
Diffstat:
7 files changed, 137 insertions(+), 14 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,4 +1,5 @@
diary
+*.so
*.o
*.gch
*.swp
diff --git a/Makefile b/Makefile
@@ -15,15 +15,15 @@ CFLAGS = -Wall \
UNAME = ${shell uname}
ifeq ($(UNAME),FreeBSD)
- LIBS = -lncurses -lcurl -pthread
+ LIBS = -lncurses -lcurl -ldl -pthread
endif
ifeq ($(UNAME),Linux)
- LIBS = -lncursesw -lcurl -pthread
+ LIBS = -lncursesw -lcurl -ldl -pthread
endif
ifeq ($(UNAME),Darwin)
- LIBS = -lncurses -lcurl -pthread -framework CoreFoundation
+ LIBS = -lncurses -lcurl -ldl -pthread -framework CoreFoundation
endif
@@ -32,11 +32,18 @@ default: $(TARGET)
$(TARGET): $(SRC)
$(CC) $(SRC) -o $(TARGET) $(CFLAGS) $(LIBS)
-debug: $(SRC)
+debug: $(SRC) lttng
$(CC) $(SRC) -o $(TARGET) $(CFLAGS) -g $(LIBS)
+# The tracepoint provider package shared object can be
+# preloaded before the instrumented application starts.
+# https://lttng.org/docs/v2.13/#doc-building-tracepoint-providers-and-user-application
+lttng: $(SRC)
+ $(CC) -I $(SRCDIR) -fpic -c src/diary-tp.c
+ $(CC) -shared -o libtpp.so diary-tp.o -llttng-ust -ldl
+
clean:
- rm -f $(TARGET)
+ rm -f $(TARGET) libtpp.so diary-tp.o
install: $(TARGET)
cp $(TARGET) $(BINDIR)/$(TARGET)
diff --git a/docs/TESTING.md b/docs/TESTING.md
@@ -2,6 +2,13 @@
This file holds notes for testing purposes.
+## Compile with Debug Symbols
+
+To make a build with debug symbols (for example, required to run [Valgrind](#valgrind) below) use the `debug` target:
+```bash
+make debug
+```
+
## Valgrind
Use Valgrind on a build with debug symbols to discover memory issues:
@@ -16,24 +23,69 @@ https://developers.redhat.com/blog/2021/04/23/valgrind-memcheck-different-ways-t
Don't use `--show-leak-kinds=all` with Ncurses:
https://invisible-island.net/ncurses/ncurses.faq.html#config_leaks
-## Compile with Debug Symbols
+## Compile with Clang Compiler
+
+As pointed out in [#82](https://github.com/in0rdr/diary/issues/82) the clang compiler is a lot more useful when it comes to debugging.
+
+To compile the program with clang:
+```
+make CC=clang
+```
+
+## Linux Trace Toolkit (LTTng)
+
+Logs from the debug build can be recording in a [LTTng tracing session](https://lttng.org/docs/v2.13/#doc-start-sessiond).
+
+To start a new session use:
+```bash
+lttng create
+```
+
+Create channel to capture the traces. The recording event rule below matches all user space tracepoint events of which the name starts with `diary:*`:
+```bash
+lttng enable-event --userspace 'diary:*'
+```
+
+To only show traces at least as severe as `--loglevel` or for a specific log level only:
+```bash
+lttng enable-event --userspace 'diary:*' --loglevel=WARNING
+lttng enable-event --userspace 'diary:*' --loglevel-only=INFO
+```
-To make a build with debug symbols use the `debug` target:
+Build the debug diary with the [tracepoint provider package (tpp) shared object](https://lttng.org/docs/v2.13/#doc-building-tracepoint-providers-and-user-application):
```bash
make debug
+...
+gcc -I src/ -fpic -c src/diary-tp.c
+gcc -shared -o libtpp.so diary-tp.o -llttng-ust -ldl
```
-## Compile with Clang Compiler
+Start recording session:
+```bash
+lttng start
+```
-As pointed out in [#82](https://github.com/in0rdr/diary/issues/82) the clang compiler is a lot more useful when it comes to debugging.
+Preload the tpp shared object and start diary:
+```bash
+LD_PRELOAD=./libtpp.so ./diary
+```
-To compile the program with clang:
+Stop the session and observe traces:
+```bash
+lttng stop # stop session
+lttng view # observe traces
+lttng clear # if needed, clear traces
```
-make CC=clang
+
+Cleanup the session daemon once done:
+```bash
+killall lttng-sessiond
```
## Send stderr to File
+> :information_source: For proper tracing and logging, start diary with the LTTng tracepoint provider package (tpp) shared object file, see [LTTng](#linux-trace-toolkit-lttng).
+
Send stderr to a file for debugging:
```bash
diary 2>log.txt
diff --git a/src/diary-tp.c b/src/diary-tp.c
@@ -0,0 +1,4 @@
+#define LTTNG_UST_TRACEPOINT_CREATE_PROBES
+#define LTTNG_UST_TRACEPOINT_DEFINE
+
+#include "diary-tp.h"
diff --git a/src/diary-tp.h b/src/diary-tp.h
@@ -0,0 +1,56 @@
+#undef LTTNG_UST_TRACEPOINT_PROVIDER
+#define LTTNG_UST_TRACEPOINT_PROVIDER diary
+
+#undef LTTNG_UST_TRACEPOINT_INCLUDE
+#define LTTNG_UST_TRACEPOINT_INCLUDE "diary-tp.h"
+
+#if !defined(_DIARY_TP_H) || defined(LTTNG_UST_TRACEPOINT_HEADER_MULTI_READ)
+#define _DIARY_TP_H
+
+#include <lttng/tracepoint.h>
+
+// Instrument a C user application
+// https://lttng.org/docs/v2.13/#doc-c-application
+LTTNG_UST_TRACEPOINT_EVENT(
+ /* Tracepoint provider name */
+ diary,
+
+ /* Tracepoint name */
+ debug,
+
+ /* Input arguments */
+ LTTNG_UST_TP_ARGS(
+ //int, my_integer_arg,
+ char *, msg_arg
+ ),
+
+ /* Output event fields */
+ LTTNG_UST_TP_FIELDS(
+ lttng_ust_field_string(msg, msg_arg)
+ //lttng_ust_field_integer(int, my_integer_field, my_integer_arg)
+ )
+)
+
+LTTNG_UST_TRACEPOINT_EVENT(
+ diary,
+ warning,
+ LTTNG_UST_TP_ARGS(
+ char *, msg_arg,
+ char *, arg1
+ ),
+ LTTNG_UST_TP_FIELDS(
+ lttng_ust_field_string(msg, msg_arg)
+ lttng_ust_field_string(arg1, arg1)
+ )
+)
+
+/* Log level assignment */
+// LTTNG_UST_TRACEPOINT_LOGLEVEL_ERR
+// LTTNG_UST_TRACEPOINT_LOGLEVEL_WARNING
+// LTTNG_UST_TRACEPOINT_LOGLEVEL_INFO
+// LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_LINE (default)
+LTTNG_UST_TRACEPOINT_LOGLEVEL(diary, warning, LTTNG_UST_TRACEPOINT_LOGLEVEL_WARNING)
+
+#endif /* _DIARY_TP_H */
+
+#include <lttng/tracepoint-event.h>
diff --git a/src/diary.c b/src/diary.c
@@ -298,7 +298,7 @@ bool read_config(const char* file_path) {
// check if config file is readable
if( access( config_file_path, R_OK ) != 0 ) {
- fprintf(stderr, "Warning - Config file '%s' missing or not readable, skipping\n", config_file_path);
+ lttng_ust_tracepoint(diary, warning, "Config file missing or not readable, skipping", config_file_path);
return false;
}
@@ -921,4 +921,4 @@ int main(int argc, char** argv) {
endwin();
system("clear");
return 0;
-}
-\ No newline at end of file
+}
diff --git a/src/diary.h b/src/diary.h
@@ -26,6 +26,10 @@
#include "export.h"
#include "caldav.h"
+#define LTTNG_UST_TRACEPOINT_DEFINE
+#define LTTNG_UST_TRACEPOINT_PROBE_DYNAMIC_LINKAGE
+#include "diary-tp.h"
+
#define XDG_CONFIG_HOME_FALLBACK "~/.config"
#define CONFIG_FILE_PATH "diary/diary.cfg"
#define DIARY_VERSION "0.7-unstable"