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