blob: 717667ca8d01fba8a0087ee3141ca8708215deea [file] [log] [blame]
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes9e27e582017-03-23 17:42:49 -070029#include "linker_logger.h"
30
Dimitry Ivanovb996d602016-07-11 18:11:39 -070031#include <string.h>
32#include <sys/prctl.h>
Dimitry Ivanovb996d602016-07-11 18:11:39 -070033#include <unistd.h>
34
35#include <string>
36#include <vector>
37
38#include "android-base/strings.h"
Elliott Hughes9e27e582017-03-23 17:42:49 -070039#include "private/CachedProperty.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070040#include "private/libc_logging.h"
41
42LinkerLogger g_linker_logger;
Elliott Hughes9e27e582017-03-23 17:42:49 -070043bool g_greylist_disabled = false;
Dimitry Ivanovb996d602016-07-11 18:11:39 -070044
45static uint32_t ParseProperty(const std::string& value) {
46 if (value.empty()) {
47 return 0;
48 }
49
50 std::vector<std::string> options = android::base::Split(value, ",");
51
52 uint32_t flags = 0;
53
54 for (const auto& o : options) {
Elliott Hughes9e27e582017-03-23 17:42:49 -070055 if (o == "dlerror") {
Dimitry Ivanovb996d602016-07-11 18:11:39 -070056 flags |= kLogErrors;
Elliott Hughes9e27e582017-03-23 17:42:49 -070057 } else if (o == "dlopen") {
Dimitry Ivanovb996d602016-07-11 18:11:39 -070058 flags |= kLogDlopen;
Elliott Hughes9e27e582017-03-23 17:42:49 -070059 } else if (o == "dlsym") {
Dimitry Ivanovcf00d862016-12-14 14:30:37 -080060 flags |= kLogDlsym;
Dimitry Ivanovb996d602016-07-11 18:11:39 -070061 } else {
Elliott Hughes9e27e582017-03-23 17:42:49 -070062 __libc_format_log(ANDROID_LOG_WARN, "linker", "Ignoring unknown debug.ld option \"%s\"",
63 o.c_str());
Dimitry Ivanovb996d602016-07-11 18:11:39 -070064 }
65 }
66
67 return flags;
68}
69
Elliott Hughes9e27e582017-03-23 17:42:49 -070070static void GetAppSpecificProperty(char* buffer) {
Elliott Hughes30a36272017-02-22 17:31:41 -080071 // Get process basename.
Dimitry Ivanovfbe54c42017-02-17 14:41:30 -080072 const char* process_name_start = basename(g_argv[0]);
Elliott Hughes30a36272017-02-22 17:31:41 -080073
74 // Remove ':' and everything after it. This is the naming convention for
Dimitry Ivanovfbe54c42017-02-17 14:41:30 -080075 // services: https://developer.android.com/guide/components/services.html
76 const char* process_name_end = strchr(process_name_start, ':');
77
78 std::string process_name = (process_name_end != nullptr) ?
79 std::string(process_name_start, (process_name_end - process_name_start)) :
80 std::string(process_name_start);
Dimitry Ivanovb996d602016-07-11 18:11:39 -070081
Elliott Hughes9e27e582017-03-23 17:42:49 -070082 std::string property_name = std::string("debug.ld.app.") + process_name;
83 __system_property_get(property_name.c_str(), buffer);
84}
Dimitry Ivanovb996d602016-07-11 18:11:39 -070085
Elliott Hughes9e27e582017-03-23 17:42:49 -070086void LinkerLogger::ResetState() {
87 // The most likely scenario app is not debuggable and
88 // is running on a user build, in which case logging is disabled.
89 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) {
90 return;
91 }
92
93 // This is a convenient place to check whether the greylist should be disabled for testing.
94 static CachedProperty greylist_disabled("debug.ld.greylist_disabled");
95 bool old_value = g_greylist_disabled;
96 g_greylist_disabled = (strcmp(greylist_disabled.Get(), "true") == 0);
97 if (g_greylist_disabled != old_value) {
98 __libc_format_log(ANDROID_LOG_INFO, "linker", "%s greylist",
99 g_greylist_disabled ? "Disabling" : "Enabling");
100 }
101
102 flags_ = 0;
103
104 // For logging, check the flag applied to all processes first.
105 static CachedProperty debug_ld_all("debug.ld.all");
106 flags_ |= ParseProperty(debug_ld_all.Get());
107
108 // Ignore processes started without argv (http://b/33276926).
109 if (g_argv[0] == nullptr) {
110 return;
111 }
112
113 // Otherwise check the app-specific property too.
114 // We can't easily cache the property here because argv[0] changes.
115 char debug_ld_app[PROP_VALUE_MAX] = {};
116 GetAppSpecificProperty(debug_ld_app);
117 flags_ |= ParseProperty(debug_ld_app);
Dimitry Ivanovb996d602016-07-11 18:11:39 -0700118}
119
120void LinkerLogger::Log(uint32_t type, const char* format, ...) {
121 if ((flags_ & type) == 0) {
122 return;
123 }
124
125 va_list ap;
126 va_start(ap, format);
127 __libc_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap);
128 va_end(ap);
129}