Stefan Schuermans commited on 2020-05-17 10:22:00
Showing 11 changed files, with 200 additions and 0 deletions.
... | ... |
@@ -0,0 +1 @@ |
1 |
+/build/ |
... | ... |
@@ -0,0 +1,12 @@ |
1 |
+#include <liblwpttr/event.h> |
|
2 |
+ |
|
3 |
+#include "write.h" |
|
4 |
+ |
|
5 |
+#include <stdlib.h> |
|
6 |
+ |
|
7 |
+__attribute__((constructor)) static void constructor(void) { |
|
8 |
+ void *data = NULL; |
|
9 |
+ size_t size = 0; |
|
10 |
+ lwpttr_event_proc_begin(&data, &size); |
|
11 |
+ lwptpl_write(data, size); |
|
12 |
+} |
... | ... |
@@ -0,0 +1,33 @@ |
1 |
+#ifndef LWPTPL_WRITE_H |
|
2 |
+#define LWPTPL_WRITE_H |
|
3 |
+ |
|
4 |
+#include "write.h" |
|
5 |
+ |
|
6 |
+#include <fcntl.h> |
|
7 |
+#include <stdlib.h> |
|
8 |
+#include <string.h> |
|
9 |
+#include <sys/file.h> |
|
10 |
+#include <unistd.h> |
|
11 |
+ |
|
12 |
+void lwptpl_write(void const *data, size_t size) { |
|
13 |
+ if (! data || ! size) { |
|
14 |
+ return; |
|
15 |
+ } |
|
16 |
+ char const *filename = getenv("LWPTPL_OUTPUT"); |
|
17 |
+ if (! filename) { |
|
18 |
+ return; |
|
19 |
+ } |
|
20 |
+ int fd = open(filename, O_WRONLY | O_APPEND); |
|
21 |
+ if (fd == -1) { |
|
22 |
+ return; |
|
23 |
+ } |
|
24 |
+ if (flock(fd, LOCK_EX) == -1) { |
|
25 |
+ close(fd); |
|
26 |
+ return; |
|
27 |
+ } |
|
28 |
+ write(fd, data, size); |
|
29 |
+ flock(fd, LOCK_UN); |
|
30 |
+ close(fd); |
|
31 |
+} |
|
32 |
+ |
|
33 |
+#endif /* #ifndef LWPTPL_WRITE_H */ |
... | ... |
@@ -0,0 +1,34 @@ |
1 |
+add_custom_command( |
|
2 |
+ OUTPUT |
|
3 |
+ ${CMAKE_CURRENT_BINARY_DIR}/src/lwproctrace.pb-c.c |
|
4 |
+ ${CMAKE_CURRENT_BINARY_DIR}/src/lwproctrace.pb-c.h |
|
5 |
+ DEPENDS |
|
6 |
+ ${CMAKE_CURRENT_SOURCE_DIR}/lwproctrace.proto |
|
7 |
+ COMMAND |
|
8 |
+ protoc-c --proto_path ${CMAKE_CURRENT_SOURCE_DIR} |
|
9 |
+ --c_out ${CMAKE_CURRENT_BINARY_DIR}/src |
|
10 |
+ lwproctrace.proto |
|
11 |
+) |
|
12 |
+ |
|
13 |
+add_library( |
|
14 |
+ lwpttr |
|
15 |
+ STATIC |
|
16 |
+ include/liblwpttr/event.h |
|
17 |
+ src/event.c |
|
18 |
+ ${CMAKE_CURRENT_BINARY_DIR}/src/lwproctrace.pb-c.c |
|
19 |
+ ${CMAKE_CURRENT_BINARY_DIR}/src/lwproctrace.pb-c.h |
|
20 |
+) |
|
21 |
+ |
|
22 |
+target_include_directories( |
|
23 |
+ lwpttr |
|
24 |
+ PUBLIC |
|
25 |
+ include |
|
26 |
+ PRIVATE |
|
27 |
+ ${CMAKE_CURRENT_BINARY_DIR}/src |
|
28 |
+) |
|
29 |
+ |
|
30 |
+target_link_libraries( |
|
31 |
+ lwpttr |
|
32 |
+ PUBLIC |
|
33 |
+ -lprotobuf-c |
|
34 |
+) |
... | ... |
@@ -0,0 +1,15 @@ |
1 |
+#ifndef LWPTTR_EVENT_H |
|
2 |
+#define LWPTTR_EVENT_H |
|
3 |
+ |
|
4 |
+#include <stdlib.h> |
|
5 |
+ |
|
6 |
+/** |
|
7 |
+ * @brief make a process begin event |
|
8 |
+ * @param[out] *data pointer to event data (malloc-ed) |
|
9 |
+ * @param[out] *size size of data |
|
10 |
+ * @return 0 on success (*data, *size set), |
|
11 |
+ * -1 on error (*data = NULL, *size = 0) |
|
12 |
+ */ |
|
13 |
+int lwpttr_event_proc_begin(void **data, size_t *size); |
|
14 |
+ |
|
15 |
+#endif /* #ifndef LWPTTR_EVENT_H */ |
... | ... |
@@ -0,0 +1,31 @@ |
1 |
+syntax = "proto2"; |
|
2 |
+ |
|
3 |
+package lwproctrace; |
|
4 |
+ |
|
5 |
+message timespec { |
|
6 |
+ required int64 sec = 1; |
|
7 |
+ optional int32 nsec = 2; |
|
8 |
+} |
|
9 |
+ |
|
10 |
+message proc_begin { |
|
11 |
+ required int32 pid = 1; |
|
12 |
+ optional int32 ppid = 2; |
|
13 |
+ optional string exe = 3; |
|
14 |
+ optional string cwd = 4; |
|
15 |
+ repeated string cmdline = 5; |
|
16 |
+ repeated string environ = 6; |
|
17 |
+} |
|
18 |
+ |
|
19 |
+message proc_end { |
|
20 |
+ required int32 pid = 1; |
|
21 |
+} |
|
22 |
+ |
|
23 |
+message event { |
|
24 |
+ required timespec timestamp = 1; |
|
25 |
+ optional proc_begin proc_begin = 2; |
|
26 |
+ optional proc_end proc_end = 3; |
|
27 |
+} |
|
28 |
+ |
|
29 |
+message trace { |
|
30 |
+ repeated event event = 1; |
|
31 |
+} |
... | ... |
@@ -0,0 +1,35 @@ |
1 |
+#include <liblwpttr/event.h> |
|
2 |
+ |
|
3 |
+#include <lwproctrace.pb-c.h> |
|
4 |
+ |
|
5 |
+#include <stdlib.h> |
|
6 |
+#include <sys/types.h> |
|
7 |
+#include <time.h> |
|
8 |
+#include <unistd.h> |
|
9 |
+ |
|
10 |
+int lwpttr_event_proc_begin(void **data, size_t *size) { |
|
11 |
+ struct timespec now; |
|
12 |
+ clock_gettime(CLOCK_REALTIME, &now); |
|
13 |
+ struct _Lwproctrace__Timespec timestamp = LWPROCTRACE__TIMESPEC__INIT; |
|
14 |
+ timestamp.sec = now.tv_sec; |
|
15 |
+ timestamp.has_nsec = 1; |
|
16 |
+ timestamp.nsec = now.tv_nsec; |
|
17 |
+ |
|
18 |
+ struct _Lwproctrace__ProcBegin proc_begin = LWPROCTRACE__PROC_BEGIN__INIT; |
|
19 |
+ proc_begin.pid = getpid(); |
|
20 |
+ proc_begin.has_ppid = 1; |
|
21 |
+ proc_begin.ppid = getppid(); |
|
22 |
+ |
|
23 |
+ struct _Lwproctrace__Event event = LWPROCTRACE__EVENT__INIT; |
|
24 |
+ event.timestamp = ×tamp; |
|
25 |
+ event.proc_begin = &proc_begin; |
|
26 |
+ |
|
27 |
+ *size = lwproctrace__event__get_packed_size(&event); |
|
28 |
+ *data = malloc(*size); |
|
29 |
+ if (! data) { |
|
30 |
+ *size = 0; |
|
31 |
+ return -1; |
|
32 |
+ } |
|
33 |
+ lwproctrace__event__pack(&event, *data); |
|
34 |
+ return 0; |
|
35 |
+} |
... | ... |
@@ -0,0 +1,15 @@ |
1 |
+add_test( |
|
2 |
+ NAME |
|
3 |
+ first |
|
4 |
+ COMMAND |
|
5 |
+ bash -c |
|
6 |
+ " |
|
7 |
+ > out.proto |
|
8 |
+ LWPTPL_OUTPUT=out.proto \ |
|
9 |
+ LD_PRELOAD=${CMAKE_BINARY_DIR}/liblwptpl/liblwptpl.so \ |
|
10 |
+ /bin/true |
|
11 |
+ ls -l out.proto |
|
12 |
+ protoc --proto_path ${CMAKE_SOURCE_DIR}/liblwpttr lwproctrace.proto \ |
|
13 |
+ --decode lwproctrace.event < out.proto |
|
14 |
+ " |
|
15 |
+) |
|
0 | 16 |