1 // Copyright 2012 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include "client/linux/log/log.h"
34
35 #if defined(__ANDROID__)
36 #include <android/log.h>
37 #include <dlfcn.h>
38 #else
39 #include "third_party/lss/linux_syscall_support.h"
40 #endif
41
42 namespace logger {
43
44 #if defined(__ANDROID__)
45 namespace {
46
47 // __android_log_buf_write() is not exported in the NDK and is being used by
48 // dynamic runtime linking. Its declaration is taken from Android's
49 // system/core/include/log/log.h.
50 using AndroidLogBufferWriteFunc = int (*)(int bufID, int prio, const char* tag,
51 const char* text);
52 const int kAndroidCrashLogId = 4; // From LOG_ID_CRASH in log.h.
53 const char kAndroidLogTag[] = "google-breakpad";
54
55 bool g_crash_log_initialized = false;
56 AndroidLogBufferWriteFunc g_android_log_buf_write = nullptr;
57
58 } // namespace
59
initializeCrashLogWriter()60 void initializeCrashLogWriter() {
61 if (g_crash_log_initialized)
62 return;
63 g_android_log_buf_write = reinterpret_cast<AndroidLogBufferWriteFunc>(
64 dlsym(RTLD_DEFAULT, "__android_log_buf_write"));
65 g_crash_log_initialized = true;
66 }
67
writeToCrashLog(const char * buf)68 int writeToCrashLog(const char* buf) {
69 // Try writing to the crash log ring buffer. If not available, fall back to
70 // the standard log buffer.
71 if (g_android_log_buf_write) {
72 return g_android_log_buf_write(kAndroidCrashLogId, ANDROID_LOG_FATAL,
73 kAndroidLogTag, buf);
74 }
75 return __android_log_write(ANDROID_LOG_FATAL, kAndroidLogTag, buf);
76 }
77 #endif
78
write(const char * buf,size_t nbytes)79 int write(const char* buf, size_t nbytes) {
80 #if defined(__ANDROID__)
81 return __android_log_write(ANDROID_LOG_WARN, kAndroidLogTag, buf);
82 #else
83 return sys_write(2, buf, nbytes);
84 #endif
85 }
86
87 } // namespace logger
88