Flutter Engine
The Flutter Engine
native_symbol_posix.cc
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/globals.h"
6#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA) || \
7 defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
8
10#include "vm/native_symbol.h"
11
12#include <cxxabi.h> // NOLINT
13#include <dlfcn.h> // NOLINT
14
15namespace dart {
16
17// On Fuchsia, in lieu of the ELF dynamic symbol table consumed through dladdr,
18// we consumes symbols produced by //topaz/runtime/dart/profiler_symbols and
19// provided to the VM by the embedder through Dart_AddSymbols. They have the
20// format
21//
22// struct {
23// uint32_t num_entries;
24// struct {
25// uint32_t offset;
26// uint32_t size;
27// uint32_t string_table_offset;
28// } entries[num_entries];
29// const char* string_table;
30// }
31//
32// Entries are sorted by offset. String table entries are NUL-terminated.
33class NativeSymbols {
34 public:
35 NativeSymbols(const char* dso_name, void* buffer, size_t size)
36 : next_(nullptr), dso_name_(dso_name) {
37 num_entries_ = *reinterpret_cast<uint32_t*>(buffer);
38 entries_ =
39 reinterpret_cast<Entry*>(reinterpret_cast<uint32_t*>(buffer) + 1);
40 name_table_ = reinterpret_cast<const char*>(entries_ + num_entries_);
41 }
42
43 NativeSymbols* next() const { return next_; }
44 void set_next(NativeSymbols* symbols) { next_ = symbols; }
45
46 bool Lookup(const char* dso_name,
47 uword dso_offset,
48 uword* start_offset,
49 const char** name) {
50 if (strcmp(dso_name, dso_name_) != 0) {
51 return false;
52 }
53
54 intptr_t lo = 0;
55 intptr_t hi = num_entries_ - 1;
56 while (lo <= hi) {
57 intptr_t mid = (hi - lo + 1) / 2 + lo;
58 ASSERT(mid >= lo);
59 ASSERT(mid <= hi);
60 const Entry& entry = entries_[mid];
61 if (dso_offset < entry.offset) {
62 hi = mid - 1;
63 } else if (dso_offset >= (entry.offset + entry.size)) {
64 lo = mid + 1;
65 } else {
66 *start_offset = entry.offset;
67 *name = &name_table_[entry.name_offset];
68 return true;
69 }
70 }
71
72 return false;
73 }
74
75 private:
76 struct Entry {
77 uint32_t offset;
78 uint32_t size;
79 uint32_t name_offset;
80 };
81
82 NativeSymbols* next_;
83 const char* const dso_name_;
84 intptr_t num_entries_;
85 Entry* entries_;
86 const char* name_table_;
87
88 DISALLOW_COPY_AND_ASSIGN(NativeSymbols);
89};
90
91static NativeSymbols* symbols_ = nullptr;
92
94
96 NativeSymbols* symbols = symbols_;
97 symbols_ = nullptr;
98 while (symbols != nullptr) {
99 NativeSymbols* next = symbols->next();
100 delete symbols;
101 symbols = next;
102 }
103}
104
105const char* NativeSymbolResolver::LookupSymbolName(uword pc, uword* start) {
106 Dl_info info;
107 int r = dladdr(reinterpret_cast<void*>(pc), &info);
108 if (r == 0) {
109 return nullptr;
110 }
111
112 auto const dso_name = info.dli_fname;
113 const auto dso_base = reinterpret_cast<uword>(info.dli_fbase);
114 const auto dso_offset = pc - dso_base;
115
116 for (NativeSymbols* symbols = symbols_; symbols != nullptr;
117 symbols = symbols->next()) {
118 uword symbol_start_offset;
119 const char* symbol_name;
120 if (symbols->Lookup(dso_name, dso_offset, &symbol_start_offset,
121 &symbol_name)) {
122 if (start != nullptr) {
123 *start = symbol_start_offset + dso_base;
124 }
125 return symbol_name;
126 }
127 }
128
129#if !defined(DART_HOST_OS_FUCHSIA)
130 // Fallback for libc, etc.
131 if (info.dli_sname == nullptr) {
132 return nullptr;
133 }
134 if (start != nullptr) {
135 *start = reinterpret_cast<uword>(info.dli_saddr);
136 }
137 int status = 0;
138 size_t len = 0;
139 char* demangled = abi::__cxa_demangle(info.dli_sname, nullptr, &len, &status);
140 if (status == 0) {
141 return demangled;
142 }
143 return info.dli_sname;
144#else
145 // Never works on Fuchsia; avoid linking in cxa_demangle.
146 return nullptr;
147#endif
148}
149
151
153 uword* dso_base,
154 const char** dso_name) {
155 Dl_info info;
156 int r = dladdr(reinterpret_cast<void*>(pc), &info);
157 if (r == 0) {
158 return false;
159 }
160 if (dso_base != nullptr) {
161 *dso_base = reinterpret_cast<uword>(info.dli_fbase);
162 }
163 if (dso_name != nullptr) {
164 *dso_name = info.dli_fname;
165 }
166 return true;
167}
168
169void NativeSymbolResolver::AddSymbols(const char* dso_name,
170 void* buffer,
171 size_t size) {
172 NativeSymbols* symbols = new NativeSymbols(dso_name, buffer, size);
173 symbols->set_next(symbols_);
174 symbols_ = symbols;
175}
176
177} // namespace dart
178
179#endif // defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA) || \
180 // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static float next(float f)
static bool LookupSharedObject(uword pc, uword *dso_base=nullptr, const char **dso_name=nullptr)
static const char * LookupSymbolName(uword pc, uword *start)
static void AddSymbols(const char *dso_name, void *buffer, size_t size)
static void FreeSymbolName(const char *name)
#define ASSERT(E)
Definition: dart_vm.cc:33
const char *const name
uintptr_t uword
Definition: globals.h:501
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:581
SeparatedVector2 offset