2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "common_test_environment.h"
18 #include "file_content_manipulations.h"
19 #include "test_sample1.h"
20 #include "test_sample2.h"
21 #include "test_sample4.h"
23 #include "symbol_searcher.h"
27 #include <linux/limits.h>
30 #include <experimental/filesystem>
32 namespace fs = std::experimental::filesystem;
34 extern std::unique_ptr<Trace> GlobalTrace;
38 char result[PATH_MAX] = {0};
39 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
40 return fs::path(result).parent_path();
46 struct SymbolSearcher : TestEnv
48 SymbolSearcher() : TestEnv("./symbol_searcher_test.log") {}
51 TEST_F(SymbolSearcher, should_find_symbol_in_linked_library)
53 ASSERT_TRUE((void *)funcDefinedOnlyInTestSample4 == findSymbol("funcDefinedOnlyInTestSample4"));
56 TEST_F(SymbolSearcher, should_find_symbol_in_library_which_have_been_loaded_in_runtime)
58 fs::path pathToTestLib = exePath() / "libtest_sample2.so";
59 void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);
62 ASSERT_TRUE(dlsym(handle, "funcDefinedOnlyInTestSample2") ==
63 findSymbol("funcDefinedOnlyInTestSample2"));
67 TEST_F(SymbolSearcher,
68 should_ignore_symbols_found_in_current_translation_unit_if_there_is_another_alternative)
70 fs::path pathToTestSample2 = exePath() / "libtest_sample2.so";
71 void *test_sample2_handle = dlopen(pathToTestSample2.c_str(), RTLD_NOW);
72 void *func_addr_in_test_sample2 =
73 dlsym(test_sample2_handle, "funcWhichCallFuncDefinedInTestSample3");
75 ASSERT_TRUE(test_sample2_handle);
76 ASSERT_TRUE((void *)funcDefinedInTestSample3_ButWrappedInTestSample1 !=
77 reinterpret_cast<void *(*)()>(func_addr_in_test_sample2)());
79 dlclose(test_sample2_handle);
82 TEST_F(SymbolSearcher, should_give_an_opportunity_do_not_log_its_internal_allocations)
85 fs::path pathToTestLib = exePath() / "libtest_sample2.so";
86 void *handle = dlopen(pathToTestLib.c_str(), RTLD_NOW);
88 GlobalTrace.reset(new Trace);
89 void *symbolAddress = findSymbol("funcDefinedOnlyInTestSample2");
92 ASSERT_STREQ(getContentOfFile("./symbol_searcher_test.log").c_str(),
93 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
94 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
99 } // namespace backstage