| 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"; | 
 | 48 |  | 
 | 49 | static std::string property_get(const char* name) { | 
 | 50 |   char value[PROP_VALUE_MAX] = {}; | 
 | 51 |   __system_property_get(name, value); | 
 | 52 |   return value; | 
 | 53 | } | 
 | 54 |  | 
 | 55 | static uint32_t ParseProperty(const std::string& value) { | 
 | 56 |   if (value.empty()) { | 
 | 57 |     return 0; | 
 | 58 |   } | 
 | 59 |  | 
 | 60 |   std::vector<std::string> options = android::base::Split(value, ","); | 
 | 61 |  | 
 | 62 |   uint32_t flags = 0; | 
 | 63 |  | 
 | 64 |   for (const auto& o : options) { | 
 | 65 |     if (o == kOptionErrors) { | 
 | 66 |       flags |= kLogErrors; | 
 | 67 |     } else if (o == kOptionDlopen){ | 
 | 68 |       flags |= kLogDlopen; | 
 | 69 |     } else { | 
 | 70 |       __libc_format_log(ANDROID_LOG_WARN, "linker", "Unknown debug.ld option \"%s\", will ignore.", o.c_str()); | 
 | 71 |     } | 
 | 72 |   } | 
 | 73 |  | 
 | 74 |   return flags; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | void LinkerLogger::ResetState() { | 
 | 78 |   // the most likely scenario app is not debuggable and | 
 | 79 |   // is running on user build - the logging is disabled. | 
 | 80 |   if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0) { | 
 | 81 |     return; | 
 | 82 |   } | 
 | 83 |  | 
 | 84 |   flags_ = 0; | 
 | 85 |   // check flag applied to all processes first | 
 | 86 |   std::string value = property_get(kSystemLdDebugProperty); | 
 | 87 |   flags_ |= ParseProperty(value); | 
 | 88 |  | 
 | 89 |   // get process basename | 
| Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 90 |   std::string process_name = basename(g_argv[0]); | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 91 |  | 
 | 92 |   std::string property_name = std::string(kLdDebugPropertyPrefix) + process_name; | 
 | 93 |  | 
 | 94 |   // Property names are limited to PROP_NAME_MAX. | 
 | 95 |  | 
 | 96 |   if (property_name.size() >= PROP_NAME_MAX) { | 
 | 97 |     size_t count = PROP_NAME_MAX - 1; | 
 | 98 |     // remove trailing dots... | 
 | 99 |     while (property_name[count-1] == '.') { | 
 | 100 |       --count; | 
 | 101 |     } | 
 | 102 |  | 
 | 103 |     property_name = property_name.substr(0, count); | 
 | 104 |   } | 
 | 105 |   value = property_get(property_name.c_str()); | 
 | 106 |   flags_ |= ParseProperty(value); | 
 | 107 | } | 
 | 108 |  | 
 | 109 | void LinkerLogger::Log(uint32_t type, const char* format, ...) { | 
 | 110 |   if ((flags_ & type) == 0) { | 
 | 111 |     return; | 
 | 112 |   } | 
 | 113 |  | 
 | 114 |   va_list ap; | 
 | 115 |   va_start(ap, format); | 
 | 116 |   __libc_format_log_va_list(ANDROID_LOG_DEBUG, "linker", format, ap); | 
 | 117 |   va_end(ap); | 
 | 118 | } | 
 | 119 |  |