split event and proc_begin
Stefan Schuermans

Stefan Schuermans commited on 2020-05-17 17:06:23
Showing 6 changed files, with 93 additions and 52 deletions.

... ...
@@ -1,4 +1,4 @@
1
-#include <liblwpttr/event.h>
1
+#include <liblwpttr/proc_begin.h>
2 2
 
3 3
 #include "write.h"
4 4
 
... ...
@@ -7,6 +7,6 @@
7 7
 __attribute__((constructor)) static void constructor(void) {
8 8
   void *data = NULL;
9 9
   size_t size = 0;
10
-  lwpttr_event_proc_begin(&data, &size);
10
+  lwpttr_proc_begin(&data, &size);
11 11
   lwptpl_write(data, size);
12 12
 }
... ...
@@ -13,11 +13,13 @@ add_custom_command(
13 13
 add_library(
14 14
   lwpttr
15 15
   STATIC
16
-  include/liblwpttr/event.h
16
+  include/liblwpttr/proc_begin.h
17 17
   src/cleaner.c
18 18
   src/cleaner.h
19 19
   src/event.c
20
+  src/event.h
20 21
   src/macros.h
22
+  src/proc_begin.c
21 23
   src/stringlist.c
22 24
   src/stringlist.h
23 25
   src/symlink.c
... ...
@@ -1,5 +1,5 @@
1
-#ifndef LWPTTR_EVENT_H
2
-#define LWPTTR_EVENT_H
1
+#ifndef LWPTTR_PROC_BEGIN_H
2
+#define LWPTTR_PROC_BEGIN_H
3 3
 
4 4
 #include <stdlib.h>
5 5
 
... ...
@@ -10,6 +10,6 @@
10 10
  * @return 0 on success (*data, *size set),
11 11
  *         -1 on error (*data = NULL, *size = 0)
12 12
  */
13
-int lwpttr_event_proc_begin(void **data, size_t *size);
13
+int lwpttr_proc_begin(void **data, size_t *size);
14 14
 
15
-#endif /* #ifndef LWPTTR_EVENT_H */
15
+#endif /* #ifndef LWPTTR_PROC_BEGIN_H */
... ...
@@ -1,16 +1,20 @@
1
-#include <liblwpttr/event.h>
1
+#include "event.h"
2 2
 #include "cleaner.h"
3
-#include "stringlist.h"
4
-#include "symlink.h"
5
-#include "timestamp.h"
6 3
 
7 4
 #include <lwproctrace.pb-c.h>
8 5
 
9 6
 #include <stdlib.h>
10
-#include <sys/types.h>
11
-#include <unistd.h>
12 7
 
13
-static int lwpttr_event_pack(struct _Lwproctrace__Event *event,
8
+/**
9
+ * @brief pack event to a buffer
10
+ * @param[in] event the event to pack to a buffer
11
+ * @param[out] *data pointer to event data (malloc-ed)
12
+ * @param[out] *size size of data
13
+ * @param[in] cleaner cleaned up after building data buffer (also on error)
14
+ * @return 0 on success (*data, *size set),
15
+ *         -1 on error (*data = NULL, *size = 0)
16
+ */
17
+int lwpttr_event_pack(struct _Lwproctrace__Event *event,
14 18
                       void **data, size_t *size,
15 19
                       lwpttr_cleaner_t *cleaner) {
16 20
   *size = lwproctrace__event__get_packed_size(event);
... ...
@@ -24,41 +28,3 @@ static int lwpttr_event_pack(struct _Lwproctrace__Event *event,
24 28
   lwpttr_cleaner_cleanup(cleaner);
25 29
   return 0;
26 30
 }
27
-
28
-int lwpttr_event_proc_begin(void **data, size_t *size) {
29
-  *data = NULL;
30
-  *size = 0;
31
-
32
-  lwpttr_cleaner_t *cleaner = lwpttr_cleaner_new();
33
-  if (! cleaner) {
34
-    return -1;
35
-  }
36
-
37
-  struct _Lwproctrace__Timespec timestamp = LWPROCTRACE__TIMESPEC__INIT;
38
-  lwpttr_event_get_timestamp(&timestamp);
39
-
40
-  struct _Lwproctrace__ProcBegin proc_begin = LWPROCTRACE__PROC_BEGIN__INIT;
41
-  proc_begin.pid = getpid();
42
-  proc_begin.has_ppid = 1;
43
-  proc_begin.ppid = getppid();
44
-  if (symlink_read("/proc/self/exe", &proc_begin.exe, cleaner) != 0) {
45
-    return -1;
46
-  }
47
-  if (symlink_read("/proc/self/cwd", &proc_begin.cwd, cleaner) != 0) {
48
-    return -1;
49
-  }
50
-  if (stringlist_read("/proc/self/cmdline", &proc_begin.n_cmdline,
51
-                      &proc_begin.cmdline, cleaner) != 0) {
52
-    return -1;
53
-  }
54
-  if (stringlist_read("/proc/self/environ", &proc_begin.n_environ,
55
-                      &proc_begin.environ, cleaner) != 0) {
56
-    return -1;
57
-  }
58
-
59
-  struct _Lwproctrace__Event event = LWPROCTRACE__EVENT__INIT;
60
-  event.timestamp = &timestamp;
61
-  event.proc_begin = &proc_begin;
62
-
63
-  return lwpttr_event_pack(&event, data, size, cleaner);
64
-}
... ...
@@ -0,0 +1,23 @@
1
+#ifndef LWPTTR_EVENT_H
2
+#define LWPTTR_EVENT_H
3
+
4
+#include "cleaner.h"
5
+
6
+#include <lwproctrace.pb-c.h>
7
+
8
+#include <stdlib.h>
9
+
10
+/**
11
+ * @brief pack event to a buffer
12
+ * @param[in] event the event to pack to a buffer
13
+ * @param[out] *data pointer to event data (malloc-ed)
14
+ * @param[out] *size size of data
15
+ * @param[in] cleaner cleaned up after building data buffer (also on error)
16
+ * @return 0 on success (*data, *size set),
17
+ *         -1 on error (*data = NULL, *size = 0)
18
+ */
19
+int lwpttr_event_pack(struct _Lwproctrace__Event *event,
20
+                      void **data, size_t *size,
21
+                      lwpttr_cleaner_t *cleaner);
22
+
23
+#endif /* #ifndef LWPTTR_EVENT_H */
... ...
@@ -0,0 +1,50 @@
1
+#include <liblwpttr/proc_begin.h>
2
+#include "cleaner.h"
3
+#include "event.h"
4
+#include "stringlist.h"
5
+#include "symlink.h"
6
+#include "timestamp.h"
7
+
8
+#include <lwproctrace.pb-c.h>
9
+
10
+#include <stdlib.h>
11
+#include <sys/types.h>
12
+#include <unistd.h>
13
+
14
+int lwpttr_proc_begin(void **data, size_t *size) {
15
+  *data = NULL;
16
+  *size = 0;
17
+
18
+  lwpttr_cleaner_t *cleaner = lwpttr_cleaner_new();
19
+  if (! cleaner) {
20
+    return -1;
21
+  }
22
+
23
+  struct _Lwproctrace__Timespec timestamp = LWPROCTRACE__TIMESPEC__INIT;
24
+  lwpttr_event_get_timestamp(&timestamp);
25
+
26
+  struct _Lwproctrace__ProcBegin proc_begin = LWPROCTRACE__PROC_BEGIN__INIT;
27
+  proc_begin.pid = getpid();
28
+  proc_begin.has_ppid = 1;
29
+  proc_begin.ppid = getppid();
30
+  if (symlink_read("/proc/self/exe", &proc_begin.exe, cleaner) != 0) {
31
+    return -1;
32
+  }
33
+  if (symlink_read("/proc/self/cwd", &proc_begin.cwd, cleaner) != 0) {
34
+    return -1;
35
+  }
36
+  if (stringlist_read("/proc/self/cmdline", &proc_begin.n_cmdline,
37
+                      &proc_begin.cmdline, cleaner) != 0) {
38
+    return -1;
39
+  }
40
+  if (stringlist_read("/proc/self/environ", &proc_begin.n_environ,
41
+                      &proc_begin.environ, cleaner) != 0) {
42
+    return -1;
43
+  }
44
+
45
+  struct _Lwproctrace__Event event = LWPROCTRACE__EVENT__INIT;
46
+  event.timestamp = &timestamp;
47
+  event.proc_begin = &proc_begin;
48
+
49
+  return lwpttr_event_pack(&event, data, size, cleaner);
50
+}
0 51