1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include "src/base/test/utils.h"
18
19 #include <stdlib.h>
20
21 #include <memory>
22
23 #include "perfetto/base/build_config.h"
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/file_utils.h"
26
27 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
28 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
29 PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) || \
30 PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
31 #include <limits.h>
32 #include <unistd.h>
33 #endif
34
35 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
36 #include <Windows.h>
37 #include <io.h>
38 #endif
39
40 #if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
41 #include <mach-o/dyld.h>
42 #endif
43
44 namespace perfetto {
45 namespace base {
46
GetCurExecutableDir()47 std::string GetCurExecutableDir() {
48 std::string self_path;
49 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
50 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
51 PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
52 char buf[PATH_MAX];
53 ssize_t size = readlink("/proc/self/exe", buf, sizeof(buf));
54 PERFETTO_CHECK(size != -1);
55 // readlink does not null terminate.
56 self_path = std::string(buf, static_cast<size_t>(size));
57 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
58 uint32_t size = 0;
59 PERFETTO_CHECK(_NSGetExecutablePath(nullptr, &size));
60 self_path.resize(size);
61 PERFETTO_CHECK(_NSGetExecutablePath(&self_path[0], &size) == 0);
62 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
63 char buf[MAX_PATH];
64 auto len = ::GetModuleFileNameA(nullptr /*current*/, buf, sizeof(buf));
65 self_path = std::string(buf, len);
66 self_path = self_path.substr(0, self_path.find_last_of("\\"));
67 #else
68 PERFETTO_FATAL(
69 "GetCurExecutableDir() not implemented on the current platform");
70 #endif
71 // Cut binary name.
72 return self_path.substr(0, self_path.find_last_of("/"));
73 }
74
GetTestDataPath(const std::string & path)75 std::string GetTestDataPath(const std::string& path) {
76 std::string self_path = GetCurExecutableDir();
77 std::string full_path = self_path + "/../../" + path;
78 if (FileExists(full_path))
79 return full_path;
80 full_path = self_path + "/" + path;
81 if (FileExists(full_path))
82 return full_path;
83 // Fall back to relative to root dir.
84 return path;
85 }
86
HexDump(const void * data_void,size_t len,size_t bytes_per_line)87 std::string HexDump(const void* data_void, size_t len, size_t bytes_per_line) {
88 const char* data = reinterpret_cast<const char*>(data_void);
89 std::string res;
90 static const size_t kPadding = bytes_per_line * 3 + 12;
91 std::unique_ptr<char[]> line(new char[bytes_per_line * 4 + 128]);
92 for (size_t i = 0; i < len; i += bytes_per_line) {
93 char* wptr = line.get();
94 wptr += sprintf(wptr, "%08zX: ", i);
95 for (size_t j = i; j < i + bytes_per_line && j < len; j++)
96 wptr += sprintf(wptr, "%02X ", static_cast<unsigned>(data[j]) & 0xFF);
97 for (size_t j = static_cast<size_t>(wptr - line.get()); j < kPadding; ++j)
98 *(wptr++) = ' ';
99 for (size_t j = i; j < i + bytes_per_line && j < len; j++) {
100 char c = data[j];
101 *(wptr++) = (c >= 32 && c < 127) ? c : '.';
102 }
103 *(wptr++) = '\n';
104 *(wptr++) = '\0';
105 res.append(line.get());
106 }
107 return res;
108 }
109
110 } // namespace base
111 } // namespace perfetto
112