1 /* 2 * Copyright (C) 2005-2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <sys/types.h> 20 21 /* deal with possible sys/cdefs.h conflict with fcntl.h */ 22 #ifdef __unused 23 #define __unused_defined __unused 24 #undef __unused 25 #endif 26 27 #include <fcntl.h> /* Pick up O_* macros */ 28 29 /* restore definitions from above */ 30 #ifdef __unused_defined 31 #define __unused __attribute__((__unused__)) 32 #endif 33 34 #include <stdint.h> 35 36 #include <log/log_id.h> 37 #include <log/log_time.h> 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 /* 44 * Native log reading interface section. See logcat for sample code. 45 * 46 * The preferred API is an exec of logcat. Likely uses of this interface 47 * are if native code suffers from exec or filtration being too costly, 48 * access to raw information, or parsing is an issue. 49 */ 50 51 struct logger_entry { 52 uint16_t len; /* length of the payload */ 53 uint16_t hdr_size; /* sizeof(struct logger_entry) */ 54 int32_t pid; /* generating process's pid */ 55 uint32_t tid; /* generating process's tid */ 56 uint32_t sec; /* seconds since Epoch */ 57 uint32_t nsec; /* nanoseconds */ 58 uint32_t lid; /* log id of the payload, bottom 4 bits currently */ 59 uint32_t uid; /* generating process's uid */ 60 }; 61 62 /* 63 * The maximum size of the log entry payload that can be 64 * written to the logger. An attempt to write more than 65 * this amount will result in a truncated log entry. 66 */ 67 #define LOGGER_ENTRY_MAX_PAYLOAD 4068 68 69 /* 70 * The maximum size of a log entry which can be read. 71 * An attempt to read less than this amount may result 72 * in read() returning EINVAL. 73 */ 74 #define LOGGER_ENTRY_MAX_LEN (5 * 1024) 75 76 struct log_msg { 77 union { 78 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1]; 79 struct logger_entry entry; 80 } __attribute__((aligned(4))); 81 #ifdef __cplusplus 82 /* Matching log_time operators */ 83 bool operator==(const log_msg& T) const { 84 return (entry.sec == T.entry.sec) && (entry.nsec == T.entry.nsec); 85 } 86 bool operator!=(const log_msg& T) const { 87 return !(*this == T); 88 } 89 bool operator<(const log_msg& T) const { 90 return (entry.sec < T.entry.sec) || 91 ((entry.sec == T.entry.sec) && (entry.nsec < T.entry.nsec)); 92 } 93 bool operator>=(const log_msg& T) const { 94 return !(*this < T); 95 } 96 bool operator>(const log_msg& T) const { 97 return (entry.sec > T.entry.sec) || 98 ((entry.sec == T.entry.sec) && (entry.nsec > T.entry.nsec)); 99 } 100 bool operator<=(const log_msg& T) const { 101 return !(*this > T); 102 } nseclog_msg103 uint64_t nsec() const { 104 return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec; 105 } 106 107 /* packet methods */ idlog_msg108 log_id_t id() { 109 return static_cast<log_id_t>(entry.lid); 110 } msglog_msg111 char* msg() { 112 unsigned short hdr_size = entry.hdr_size; 113 if (hdr_size >= sizeof(struct log_msg) - sizeof(entry)) { 114 return nullptr; 115 } 116 return reinterpret_cast<char*>(buf) + hdr_size; 117 } lenlog_msg118 unsigned int len() { return entry.hdr_size + entry.len; } 119 #endif 120 }; 121 122 struct logger; 123 124 log_id_t android_logger_get_id(struct logger* logger); 125 126 int android_logger_clear(struct logger* logger); 127 long android_logger_get_log_size(struct logger* logger); 128 int android_logger_set_log_size(struct logger* logger, unsigned long size); 129 long android_logger_get_log_readable_size(struct logger* logger); 130 int android_logger_get_log_version(struct logger* logger); 131 132 struct logger_list; 133 134 ssize_t android_logger_get_statistics(struct logger_list* logger_list, 135 char* buf, size_t len); 136 ssize_t android_logger_get_prune_list(struct logger_list* logger_list, 137 char* buf, size_t len); 138 int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len); 139 140 #define ANDROID_LOG_RDONLY O_RDONLY 141 #define ANDROID_LOG_WRONLY O_WRONLY 142 #define ANDROID_LOG_RDWR O_RDWR 143 #define ANDROID_LOG_ACCMODE O_ACCMODE 144 #ifndef O_NONBLOCK 145 #define ANDROID_LOG_NONBLOCK 0x00000800 146 #else 147 #define ANDROID_LOG_NONBLOCK O_NONBLOCK 148 #endif 149 #define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */ 150 #define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */ 151 #define ANDROID_LOG_PSTORE 0x80000000 152 153 struct logger_list* android_logger_list_alloc(int mode, unsigned int tail, 154 pid_t pid); 155 struct logger_list* android_logger_list_alloc_time(int mode, log_time start, 156 pid_t pid); 157 void android_logger_list_free(struct logger_list* logger_list); 158 /* In the purest sense, the following two are orthogonal interfaces */ 159 int android_logger_list_read(struct logger_list* logger_list, 160 struct log_msg* log_msg); 161 162 /* Multiple log_id_t opens */ 163 struct logger* android_logger_open(struct logger_list* logger_list, log_id_t id); 164 #define android_logger_close android_logger_free 165 /* Single log_id_t open */ 166 struct logger_list* android_logger_list_open(log_id_t id, int mode, 167 unsigned int tail, pid_t pid); 168 #define android_logger_list_close android_logger_list_free 169 170 #ifdef __cplusplus 171 } 172 #endif 173