1 /* 2 * Copyright (C) 2012-2013 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 <pthread.h> 20 #include <sys/socket.h> 21 #include <sys/types.h> 22 #include <time.h> 23 24 #include <chrono> 25 #include <condition_variable> 26 #include <list> 27 #include <memory> 28 29 #include <android-base/thread_annotations.h> 30 #include <log/log.h> 31 32 #include "LogBuffer.h" 33 #include "LogWriter.h" 34 #include "LogdLock.h" 35 36 struct PendingReaderThreadKey { 37 uid_t uid; 38 gid_t gid; 39 pid_t pid; 40 int32_t fd; 41 bool operator==(const PendingReaderThreadKey& rhs) const { 42 return uid == rhs.uid && gid == rhs.gid && pid == rhs.pid && fd == rhs.fd; 43 } 44 }; 45 46 class LogReaderList; 47 48 class LogReaderThread { 49 public: 50 LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list, 51 std::unique_ptr<LogWriter> writer, bool non_block, unsigned long tail, 52 LogMask log_mask, pid_t pid, log_time start_time, uint64_t sequence, 53 std::chrono::steady_clock::time_point deadline); TriggerReader()54 void TriggerReader() REQUIRES(logd_lock) { thread_triggered_condition_.notify_all(); } 55 TriggerSkip(log_id_t id,unsigned int skip)56 void TriggerSkip(log_id_t id, unsigned int skip) REQUIRES(logd_lock) { skip_ahead_[id] = skip; } CleanSkip()57 void CleanSkip() REQUIRES(logd_lock) { memset(skip_ahead_, 0, sizeof(skip_ahead_)); } 58 59 void Run() REQUIRES(logd_lock); Revoke()60 void Revoke() REQUIRES(logd_lock) { writer_->revoke(); } Release()61 void Release() REQUIRES(logd_lock) { 62 // gracefully shut down the socket. 63 writer_->Shutdown(); 64 release_ = true; 65 thread_triggered_condition_.notify_all(); 66 } 67 IsWatching(log_id_t id)68 bool IsWatching(log_id_t id) const REQUIRES(logd_lock) { 69 return flush_to_state_->log_mask() & (1 << id); 70 } IsWatchingMultiple(LogMask log_mask)71 bool IsWatchingMultiple(LogMask log_mask) const REQUIRES(logd_lock) { 72 return flush_to_state_->log_mask() & log_mask; 73 } 74 name()75 std::string name() const REQUIRES(logd_lock) { return writer_->name(); } start()76 uint64_t start() const REQUIRES(logd_lock) { return flush_to_state_->start(); } deadline()77 std::chrono::steady_clock::time_point deadline() const REQUIRES(logd_lock) { return deadline_; } flush_to_state()78 FlushToState& flush_to_state() REQUIRES(logd_lock) { return *flush_to_state_; } set_pending_reader_thread_key(uid_t uid,gid_t gid,pid_t pid,int32_t fd)79 void set_pending_reader_thread_key(uid_t uid, gid_t gid, pid_t pid, int32_t fd) 80 REQUIRES(logd_lock) { 81 pending_reader_thread_key_ = {uid, gid, pid, fd}; 82 } pending_reader_thread_key()83 const PendingReaderThreadKey& pending_reader_thread_key() const REQUIRES(logd_lock) { 84 return pending_reader_thread_key_; 85 } set_finish_flag()86 void set_finish_flag() REQUIRES(logd_lock) { finished_ = true; } finish_flag()87 bool finish_flag() REQUIRES(logd_lock) { return finished_; } set_track_flag()88 void set_track_flag() REQUIRES(logd_lock) { tracked_ = true; } track_flag()89 bool track_flag() REQUIRES(logd_lock) { return tracked_; } 90 91 private: 92 void ThreadFunction(); 93 // flushTo filter callbacks 94 FilterResult FilterFirstPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) 95 REQUIRES(logd_lock); 96 FilterResult FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime) 97 REQUIRES(logd_lock); 98 99 std::condition_variable thread_triggered_condition_; 100 LogBuffer* log_buffer_; 101 LogReaderList* reader_list_; 102 std::unique_ptr<LogWriter> writer_ GUARDED_BY(logd_lock); 103 104 PendingReaderThreadKey pending_reader_thread_key_ GUARDED_BY(logd_lock); 105 // Set to true to indicate the thread has finished. 106 bool finished_ GUARDED_BY(logd_lock) = false; 107 // Set to true to indicate the thread is tracked by AppOps. 108 bool tracked_ GUARDED_BY(logd_lock) = false; 109 110 // Set to true to cause the thread to end and the LogReaderThread to delete itself. 111 bool release_ GUARDED_BY(logd_lock) = false; 112 113 // If set to non-zero, only pids equal to this are read by the reader. 114 const pid_t pid_; 115 // When a reader is referencing (via start_) old elements in the log buffer, and the log 116 // buffer's size grows past its memory limit, the log buffer may request the reader to skip 117 // ahead a specified number of logs. 118 unsigned int skip_ahead_[LOG_ID_MAX] GUARDED_BY(logd_lock); 119 // LogBuffer::FlushTo() needs to store state across subsequent calls. 120 std::unique_ptr<FlushToState> flush_to_state_ GUARDED_BY(logd_lock); 121 122 // These next three variables are used for reading only the most recent lines aka `adb logcat 123 // -t` / `adb logcat -T`. 124 // tail_ is the number of most recent lines to print. 125 unsigned long tail_; 126 // count_ is the result of a first pass through the log buffer to determine how many total 127 // messages there are. 128 unsigned long count_; 129 // index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_) 130 // and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_. 131 unsigned long index_; 132 133 // When a reader requests logs starting from a given timestamp, its stored here for the first 134 // pass, such that logs before this time stamp that are accumulated in the buffer are ignored. 135 log_time start_time_; 136 // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs 137 // wrap, then wake up and send the logs to the reader anyway. 138 std::chrono::steady_clock::time_point deadline_ GUARDED_BY(logd_lock); 139 // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs. 140 const bool non_block_; 141 }; 142