Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 41 | LinkerLogger g_linker_logger; |
| 42 | |
| 43 | static const char* kSystemLdDebugProperty = "debug.ld.all"; |
Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 44 | static const char* kLdDebugPropertyPrefix = "debug.ld.app."; |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 45 | |
| 46 | static const char* kOptionErrors = "dlerror"; |
| 47 | static const char* kOptionDlopen = "dlopen"; |
Dimitry Ivanov | cf00d86 | 2016-12-14 14:30:37 -0800 | [diff] [blame] | 48 | static const char* kOptionDlsym = "dlsym"; |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 49 | |
| 50 | static std::string property_get(const char* name) { |
| 51 | char value[PROP_VALUE_MAX] = {}; |
| 52 | __system_property_get(name, value); |
| 53 | return value; |
| 54 | } |
| 55 | |
| 56 | static 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 Ivanov | cf00d86 | 2016-12-14 14:30:37 -0800 | [diff] [blame] | 70 | } else if (o == kOptionDlsym){ |
| 71 | flags |= kLogDlsym; |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 72 | } 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 | |
| 80 | void 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 Ivanov | fbe54c4 | 2017-02-17 14:41:30 -0800 | [diff] [blame] | 93 | const char* process_name_start = basename(g_argv[0]); |
| 94 | // remove ':' and everything after it. This is naming convention for |
| 95 | // services: https://developer.android.com/guide/components/services.html |
| 96 | const char* process_name_end = strchr(process_name_start, ':'); |
| 97 | |
| 98 | std::string process_name = (process_name_end != nullptr) ? |
| 99 | std::string(process_name_start, (process_name_end - process_name_start)) : |
| 100 | std::string(process_name_start); |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 101 | |
| 102 | std::string property_name = std::string(kLdDebugPropertyPrefix) + process_name; |
| 103 | |
Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 104 | value = property_get(property_name.c_str()); |
| 105 | flags_ |= ParseProperty(value); |
| 106 | } |
| 107 | |
| 108 | void LinkerLogger::Log(uint32_t type, const char* format, ...) { |
| 109 | if ((flags_ & type) == 0) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | va_list ap; |
| 114 | va_start(ap, format); |
| 115 | __libc_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap); |
| 116 | va_end(ap); |
| 117 | } |
| 118 | |