1 //
2 // Copyright 2017 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 // clang-format off
18 // This needs to be included before Backtrace.h to avoid a redefinition
19 // of DISALLOW_COPY_AND_ASSIGN
20 #include "log.h"
21 // clang-format on
22
23 #include <client/linux/handler/exception_handler.h>
24 #include <gflags/gflags.h>
25 #include <unwindstack/AndroidUnwinder.h>
26
27 #include <fstream>
28 #include <future>
29 #include <optional>
30
31 #include "model/setup/async_manager.h"
32 #include "net/posix/posix_async_socket_connector.h"
33 #include "net/posix/posix_async_socket_server.h"
34 #include "test_environment.h"
35
36 using ::android::bluetooth::root_canal::TestEnvironment;
37 using ::android::net::PosixAsyncSocketConnector;
38 using ::android::net::PosixAsyncSocketServer;
39 using rootcanal::AsyncManager;
40
41 DEFINE_string(controller_properties_file, "", "deprecated");
42 DEFINE_string(configuration, "", "controller configuration (see config.proto)");
43 DEFINE_string(configuration_file, "",
44 "controller configuration file path (see config.proto)");
45 DEFINE_string(default_commands_file, "", "deprecated");
46 DEFINE_bool(enable_hci_sniffer, false, "enable hci sniffer");
47 DEFINE_bool(enable_baseband_sniffer, false, "enable baseband sniffer");
48 DEFINE_bool(enable_pcap_filter, false, "enable PCAP filter");
49 DEFINE_bool(disable_address_reuse, false,
50 "prevent rootcanal from reusing device addresses");
51 DEFINE_uint32(test_port, 6401, "test tcp port");
52 DEFINE_uint32(hci_port, 6402, "hci server tcp port");
53 DEFINE_uint32(link_port, 6403, "link server tcp port");
54 DEFINE_uint32(link_ble_port, 6404, "le link server tcp port");
55
__asan_default_options()56 extern "C" const char* __asan_default_options() {
57 return "detect_container_overflow=0";
58 }
59
crash_callback(const void * crash_context,size_t crash_context_size,void *)60 bool crash_callback(const void* crash_context, size_t crash_context_size,
61 void* /* context */) {
62 std::optional<pid_t> tid;
63 if (crash_context_size >=
64 sizeof(google_breakpad::ExceptionHandler::CrashContext)) {
65 auto* ctx =
66 static_cast<const google_breakpad::ExceptionHandler::CrashContext*>(
67 crash_context);
68 tid = ctx->tid;
69 int signal_number = ctx->siginfo.si_signo;
70 LOG_ERROR("Process crashed, signal: %s[%d], tid: %d",
71 strsignal(signal_number), signal_number, ctx->tid);
72 } else {
73 LOG_ERROR("Process crashed, signal: unknown, tid: unknown");
74 }
75 unwindstack::AndroidLocalUnwinder unwinder;
76 unwindstack::AndroidUnwinderData data;
77 if (!unwinder.Unwind(tid, data)) {
78 LOG_ERROR("Unwind failed");
79 return false;
80 }
81 LOG_ERROR("Backtrace:");
82 for (const auto& frame : data.frames) {
83 LOG_ERROR("%s", unwinder.FormatFrame(frame).c_str());
84 }
85 return true;
86 }
87
main(int argc,char ** argv)88 int main(int argc, char** argv) {
89 google_breakpad::MinidumpDescriptor descriptor(
90 google_breakpad::MinidumpDescriptor::kMicrodumpOnConsole);
91 google_breakpad::ExceptionHandler eh(descriptor, nullptr, nullptr, nullptr,
92 true, -1);
93 eh.set_crash_handler(crash_callback);
94
95 gflags::ParseCommandLineFlags(&argc, &argv, true);
96 android::base::InitLogging(argv);
97
98 LOG_INFO("main");
99
100 if (FLAGS_test_port > UINT16_MAX) {
101 LOG_ERROR("test_port out of range: %" PRIu32, FLAGS_test_port);
102 return -1;
103 }
104
105 if (FLAGS_hci_port > UINT16_MAX) {
106 LOG_ERROR("hci_port out of range: %" PRIu32, FLAGS_hci_port);
107 return -1;
108 }
109
110 if (FLAGS_link_port > UINT16_MAX) {
111 LOG_ERROR("link_port out of range: %" PRIu32, FLAGS_link_port);
112 return -1;
113 }
114
115 if (FLAGS_link_ble_port > UINT16_MAX) {
116 LOG_ERROR("link_ble_port out of range: %" PRIu32, FLAGS_link_ble_port);
117 return -1;
118 }
119
120 std::string configuration_str;
121 if (!FLAGS_configuration.empty()) {
122 configuration_str = FLAGS_configuration;
123 } else if (!FLAGS_configuration_file.empty()) {
124 std::ifstream file(FLAGS_configuration_file);
125 std::stringstream buffer;
126 buffer << file.rdbuf();
127 configuration_str.assign(buffer.str());
128 }
129
130 TestEnvironment root_canal(
131 [](AsyncManager* am, int port) {
132 return std::make_shared<PosixAsyncSocketServer>(port, am);
133 },
134 [](AsyncManager* am) {
135 return std::make_shared<PosixAsyncSocketConnector>(am);
136 },
137 static_cast<int>(FLAGS_test_port), static_cast<int>(FLAGS_hci_port),
138 static_cast<int>(FLAGS_link_port), static_cast<int>(FLAGS_link_ble_port),
139 configuration_str, FLAGS_enable_hci_sniffer,
140 FLAGS_enable_baseband_sniffer, FLAGS_enable_pcap_filter,
141 FLAGS_disable_address_reuse);
142
143 std::promise<void> barrier;
144 std::future<void> barrier_future = barrier.get_future();
145 root_canal.initialize(std::move(barrier));
146 barrier_future.wait();
147 root_canal.close();
148 return 0;
149 }
150