| 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 | |
| 10 | #include <assert.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include <tap/tap.h> |
| 17 | |
| 18 | #include <common/payload-view.h> |
| 19 | #include <common/payload.h> |
| 20 | #include <lttng/kernel-probe-internal.h> |
| 21 | #include <lttng/kernel-probe.h> |
| 22 | |
| 23 | /* For error.h */ |
| 24 | int lttng_opt_quiet = 1; |
| 25 | int lttng_opt_verbose; |
| 26 | int lttng_opt_mi; |
| 27 | |
| 28 | #define NUM_TESTS 24 |
| 29 | |
| 30 | static void test_kernel_probe_location_address(void) |
| 31 | { |
| 32 | struct lttng_kernel_probe_location *location = NULL; |
| 33 | struct lttng_kernel_probe_location *location_from_buffer = NULL; |
| 34 | enum lttng_kernel_probe_location_status status; |
| 35 | enum lttng_kernel_probe_location_type type; |
| 36 | uint64_t address = 50, _address; |
| 37 | struct lttng_payload payload; |
| 38 | |
| 39 | diag("Testing kernel probe location address"); |
| 40 | |
| 41 | lttng_payload_init(&payload); |
| 42 | |
| 43 | location = lttng_kernel_probe_location_address_create(address); |
| 44 | ok(location, "Location object"); |
| 45 | |
| 46 | type = lttng_kernel_probe_location_get_type(location); |
| 47 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type, |
| 48 | "Location type got %d expected %d", type, |
| 49 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS); |
| 50 | |
| 51 | status = lttng_kernel_probe_location_address_get_address( |
| 52 | location, &_address); |
| 53 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address"); |
| 54 | ok(address == _address, |
| 55 | "Address is equal. Got %" PRIu64 " expected %" PRIu64, |
| 56 | _address, address); |
| 57 | |
| 58 | ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, |
| 59 | "Serializing"); |
| 60 | { |
| 61 | struct lttng_payload_view view = |
| 62 | lttng_payload_view_from_payload( |
| 63 | &payload, 0, -1); |
| 64 | ok(lttng_kernel_probe_location_create_from_payload( |
| 65 | &view, &location_from_buffer) > 0, |
| 66 | "Deserializing"); |
| 67 | } |
| 68 | |
| 69 | type = lttng_kernel_probe_location_get_type(location_from_buffer); |
| 70 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS == type, |
| 71 | "Location from buffer type got %d expected %d", type, |
| 72 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS); |
| 73 | |
| 74 | status = lttng_kernel_probe_location_address_get_address( |
| 75 | location_from_buffer, &_address); |
| 76 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting address"); |
| 77 | ok(address == _address, |
| 78 | "Address from buffer is equal. Got %" PRIu64 |
| 79 | " expected %" PRIu64, |
| 80 | _address, address); |
| 81 | |
| 82 | ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer), |
| 83 | "serialized and from buffer are equal"); |
| 84 | |
| 85 | lttng_payload_reset(&payload); |
| 86 | lttng_kernel_probe_location_destroy(location); |
| 87 | lttng_kernel_probe_location_destroy(location_from_buffer); |
| 88 | } |
| 89 | |
| 90 | static void test_kernel_probe_location_symbol(void) |
| 91 | { |
| 92 | struct lttng_kernel_probe_location *location = NULL; |
| 93 | struct lttng_kernel_probe_location *location_from_buffer = NULL; |
| 94 | enum lttng_kernel_probe_location_status status; |
| 95 | enum lttng_kernel_probe_location_type type; |
| 96 | uint64_t offset = 50, _offset; |
| 97 | const char *symbol = "Une_bonne", *_symbol; |
| 98 | struct lttng_payload payload; |
| 99 | |
| 100 | diag("Testing kernel probe location symbol"); |
| 101 | |
| 102 | lttng_payload_init(&payload); |
| 103 | |
| 104 | location = lttng_kernel_probe_location_symbol_create(symbol, offset); |
| 105 | ok(location, "Location object"); |
| 106 | |
| 107 | type = lttng_kernel_probe_location_get_type(location); |
| 108 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type, |
| 109 | "Location type got %d expected %d", type, |
| 110 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET); |
| 111 | |
| 112 | _symbol = lttng_kernel_probe_location_symbol_get_name(location); |
| 113 | ok(_symbol, "Getting symbol name"); |
| 114 | ok(!strncmp(symbol, _symbol, strlen(symbol)), |
| 115 | "Symbol name is equal. Got %s, expected %s", _symbol, |
| 116 | symbol); |
| 117 | |
| 118 | status = lttng_kernel_probe_location_symbol_get_offset( |
| 119 | location, &_offset); |
| 120 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset"); |
| 121 | ok(offset == _offset, |
| 122 | "Offset is equal. Got %" PRIu64 " expected %" PRIu64, |
| 123 | _offset, offset); |
| 124 | |
| 125 | ok(lttng_kernel_probe_location_serialize(location, &payload) > 0, |
| 126 | "Serializing"); |
| 127 | { |
| 128 | struct lttng_payload_view view = |
| 129 | lttng_payload_view_from_payload( |
| 130 | &payload, 0, -1); |
| 131 | ok(lttng_kernel_probe_location_create_from_payload( |
| 132 | &view, &location_from_buffer) > 0, |
| 133 | "Deserializing"); |
| 134 | } |
| 135 | |
| 136 | type = lttng_kernel_probe_location_get_type(location_from_buffer); |
| 137 | ok(LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET == type, |
| 138 | "Location from buffer type got %d expected %d", type, |
| 139 | LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET); |
| 140 | |
| 141 | _symbol = lttng_kernel_probe_location_symbol_get_name( |
| 142 | location_from_buffer); |
| 143 | ok(_symbol, "Getting symbol name"); |
| 144 | ok(!strncmp(symbol, _symbol, strlen(symbol)), |
| 145 | "Symbol name is equal. Got %s, expected %s", _symbol, |
| 146 | symbol); |
| 147 | |
| 148 | status = lttng_kernel_probe_location_symbol_get_offset( |
| 149 | location_from_buffer, &_offset); |
| 150 | ok(status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK, "Getting offset"); |
| 151 | ok(offset == _offset, |
| 152 | "Offset is equal. Got %" PRIu64 " expected %" PRIu64, |
| 153 | _offset, offset); |
| 154 | |
| 155 | ok(lttng_kernel_probe_location_is_equal(location, location_from_buffer), |
| 156 | "serialized and from buffer are equal"); |
| 157 | |
| 158 | lttng_payload_reset(&payload); |
| 159 | lttng_kernel_probe_location_destroy(location); |
| 160 | lttng_kernel_probe_location_destroy(location_from_buffer); |
| 161 | } |
| 162 | |
| 163 | int main(int argc, const char *argv[]) |
| 164 | { |
| 165 | plan_tests(NUM_TESTS); |
| 166 | test_kernel_probe_location_address(); |
| 167 | test_kernel_probe_location_symbol(); |
| 168 | return exit_status(); |
| 169 | } |