blob: 8190cc94fa504cacafe6bafaaa3d5e417f2701a2 [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
29#include <string.h>
30#include <sys/prctl.h>
31#include <sys/system_properties.h>
32#include <unistd.h>
33
34#include <string>
35#include <vector>
36
37#include "android-base/strings.h"
38#include "linker_logger.h"
39#include "private/libc_logging.h"
40
41LinkerLogger g_linker_logger;
42
43static const char* kSystemLdDebugProperty = "debug.ld.all";
Dimitry Ivanovf8572112016-07-13 10:24:06 -070044static const char* kLdDebugPropertyPrefix = "debug.ld.app.";
Dimitry Ivanovb996d602016-07-11 18:11:39 -070045
46static const char* kOptionErrors = "dlerror";
47static const char* kOptionDlopen = "dlopen";
Dimitry Ivanovcf00d862016-12-14 14:30:37 -080048static const char* kOptionDlsym = "dlsym";
Dimitry Ivanovb996d602016-07-11 18:11:39 -070049
50static std::string property_get(const char* name) {
51 char value[PROP_VALUE_MAX] = {};
52 __system_property_get(name, value);
53 return value;
54}
55
56static uint32_t ParseProperty(const std::string& value) {
57 if (value.empty()) {
58 return 0;
59 }
60
61 std::vector<std::string> options = android::base::Split(value, ",");
62
63 uint32_t flags = 0;
64
65 for (const auto& o : options) {
66 if (o == kOptionErrors) {
67 flags |= kLogErrors;
68 } else if (o == kOptionDlopen){
69 flags |= kLogDlopen;
Dimitry Ivanovcf00d862016-12-14 14:30:37 -080070 } else if (o == kOptionDlsym){
71 flags |= kLogDlsym;
Dimitry Ivanovb996d602016-07-11 18:11:39 -070072 } else {
73 __libc_format_log(ANDROID_LOG_WARN, "linker", "Unknown debug.ld option \"%s\", will ignore.", o.c_str());
74 }
75 }
76
77 return flags;
78}
79
80void LinkerLogger::ResetState() {
81 // the most likely scenario app is not debuggable and
82 // is running on user build - the logging is disabled.
83 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) {
84 return;
85 }
86
87 flags_ = 0;
88 // check flag applied to all processes first
89 std::string value = property_get(kSystemLdDebugProperty);
90 flags_ |= ParseProperty(value);
91
92 // get process basename
Dimitry Ivanov55437462016-07-20 15:33:07 -070093 std::string process_name = basename(g_argv[0]);
Dimitry Ivanovb996d602016-07-11 18:11:39 -070094
95 std::string property_name = std::string(kLdDebugPropertyPrefix) + process_name;
96
97 // Property names are limited to PROP_NAME_MAX.
98
99 if (property_name.size() >= PROP_NAME_MAX) {
100 size_t count = PROP_NAME_MAX - 1;
101 // remove trailing dots...
102 while (property_name[count-1] == '.') {
103 --count;
104 }
105
106 property_name = property_name.substr(0, count);
107 }
108 value = property_get(property_name.c_str());
109 flags_ |= ParseProperty(value);
110}
111
112void LinkerLogger::Log(uint32_t type, const char* format, ...) {
113 if ((flags_ & type) == 0) {
114 return;
115 }
116
117 va_list ap;
118 va_start(ap, format);
119 __libc_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap);
120 va_end(ap);
121}
122