Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / tests / unit / test_kernel_probe.cpp
CommitLineData
808cb744
JR
1/*
2 * Unit tests for the kernel probe location API.
3 *
4 * Copyright (C) 2020 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only
7 *
8 */
9
c9e313bc
SM
10#include <common/payload-view.hpp>
11#include <common/payload.hpp>
28ab034a 12
c9e313bc 13#include <lttng/kernel-probe-internal.hpp>
808cb744
JR
14#include <lttng/kernel-probe.h>
15
28ab034a
JG
16#include <inttypes.h>
17#include <stdio.h>
18#include <string.h>
19#include <tap/tap.h>
20#include <unistd.h>
21
808cb744
JR
22/* For error.h */
23int lttng_opt_quiet = 1;
24int lttng_opt_verbose;
25int lttng_opt_mi;
26
27#define NUM_TESTS 24
28
cd9adb8b 29static void test_kernel_probe_location_address()
808cb744 30{
cd9adb8b
JG
31 struct lttng_kernel_probe_location *location = nullptr;
32 struct lttng_kernel_probe_location *location_from_buffer = nullptr;
808cb744
JR
33 enum lttng_kernel_probe_location_status status;
34 enum lttng_kernel_probe_location_type type;
35 uint64_t address = 50, _address;
36 struct lttng_payload payload;
37
38 diag("Testing kernel probe location address");
39
40 lttng_payload_init(&payload);
41
42 location = lttng_kernel_probe_location_address_create(address);
43 ok(location, "Location object");
44
45 type = lttng_kernel_probe_location_get_type(location);
46 ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type,
28ab034a
JG
47 "Location type got %d expected %d",
48 type,
49 LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS);
808cb744 50
28ab034a 51 status = lttng_kernel_probe_location_address_get_address(location, &_address);
808cb744
JR
52 ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address");
53 ok(address == _address,
28ab034a
JG
54 "Address is equal. Got %" PRIu64 " expected %" PRIu64,
55 _address,
56 address);
808cb744 57
28ab034a 58 ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, "Serializing");
808cb744 59 {
28ab034a
JG
60 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
61 ok(lttng_kernel_probe_location_create_from_payload(&view, &location_from_buffer) >
62 0,
63 "Deserializing");
808cb744
JR
64 }
65
66 type = lttng_kernel_probe_location_get_type(location_from_buffer);
67 ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type,
28ab034a
JG
68 "Location from buffer type got %d expected %d",
69 type,
70 LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS);
808cb744 71
28ab034a 72 status = lttng_kernel_probe_location_address_get_address(location_from_buffer, &_address);
808cb744
JR
73 ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address");
74 ok(address == _address,
28ab034a
JG
75 "Address from buffer is equal. Got %" PRIu64 " expected %" PRIu64,
76 _address,
77 address);
808cb744
JR
78
79 ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer),
28ab034a 80 "serialized and from buffer are equal");
808cb744
JR
81
82 lttng_payload_reset(&payload);
83 lttng_kernel_probe_location_destroy(location);
84 lttng_kernel_probe_location_destroy(location_from_buffer);
85}
86
cd9adb8b 87static void test_kernel_probe_location_symbol()
808cb744 88{
cd9adb8b
JG
89 struct lttng_kernel_probe_location *location = nullptr;
90 struct lttng_kernel_probe_location *location_from_buffer = nullptr;
808cb744
JR
91 enum lttng_kernel_probe_location_status status;
92 enum lttng_kernel_probe_location_type type;
93 uint64_t offset = 50, _offset;
94 const char *symbol = "Une_bonne", *_symbol;
95 struct lttng_payload payload;
96
97 diag("Testing kernel probe location symbol");
98
99 lttng_payload_init(&payload);
100
101 location = lttng_kernel_probe_location_symbol_create(symbol, offset);
102 ok(location, "Location object");
103
104 type = lttng_kernel_probe_location_get_type(location);
105 ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type,
28ab034a
JG
106 "Location type got %d expected %d",
107 type,
108 LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET);
808cb744
JR
109
110 _symbol = lttng_kernel_probe_location_symbol_get_name(location);
111 ok(_symbol, "Getting symbol name");
112 ok(!strncmp(symbol, _symbol, strlen(symbol)),
28ab034a
JG
113 "Symbol name is equal. Got %s, expected %s",
114 _symbol,
115 symbol);
808cb744 116
28ab034a 117 status = lttng_kernel_probe_location_symbol_get_offset(location, &_offset);
808cb744 118 ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset");
28ab034a 119 ok(offset == _offset, "Offset is equal. Got %" PRIu64 " expected %" PRIu64, _offset, offset);
808cb744 120
28ab034a 121 ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, "Serializing");
808cb744 122 {
28ab034a
JG
123 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
124 ok(lttng_kernel_probe_location_create_from_payload(&view, &location_from_buffer) >
125 0,
126 "Deserializing");
808cb744
JR
127 }
128
129 type = lttng_kernel_probe_location_get_type(location_from_buffer);
130 ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type,
28ab034a
JG
131 "Location from buffer type got %d expected %d",
132 type,
133 LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET);
808cb744 134
28ab034a 135 _symbol = lttng_kernel_probe_location_symbol_get_name(location_from_buffer);
808cb744
JR
136 ok(_symbol, "Getting symbol name");
137 ok(!strncmp(symbol, _symbol, strlen(symbol)),
28ab034a
JG
138 "Symbol name is equal. Got %s, expected %s",
139 _symbol,
140 symbol);
808cb744 141
28ab034a 142 status = lttng_kernel_probe_location_symbol_get_offset(location_from_buffer, &_offset);
808cb744 143 ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset");
28ab034a 144 ok(offset == _offset, "Offset is equal. Got %" PRIu64 " expected %" PRIu64, _offset, offset);
808cb744
JR
145
146 ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer),
28ab034a 147 "serialized and from buffer are equal");
808cb744
JR
148
149 lttng_payload_reset(&payload);
150 lttng_kernel_probe_location_destroy(location);
151 lttng_kernel_probe_location_destroy(location_from_buffer);
152}
153
cd9adb8b 154int main()
808cb744
JR
155{
156 plan_tests(NUM_TESTS);
157 test_kernel_probe_location_address();
158 test_kernel_probe_location_symbol();
159 return exit_status();
160}
This page took 0.05715 seconds and 4 git commands to generate.