blob: c08f24e890ba057e6c87d84b6d2ab95197277774 [file] [log] [blame]
Mark Salyzyn12bac902014-02-26 09:50:16 -08001/*
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
Tom Cherry1647cd42020-05-04 12:53:36 -070017#pragma once
Mark Salyzyn12bac902014-02-26 09:50:16 -080018
19#include <pthread.h>
Jintao Zhu1530e7c2018-12-20 23:10:41 +080020#include <sys/socket.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080021#include <sys/types.h>
Mark Salyzyn65059532017-03-10 14:31:54 -080022#include <time.h>
Mark Salyzyn129feb22015-08-19 13:10:14 -070023
Tom Cherry5ecfbf02020-05-11 16:29:29 -070024#include <chrono>
25#include <condition_variable>
Mark Salyzyn129feb22015-08-19 13:10:14 -070026#include <list>
Tom Cherry06e478b2018-10-08 17:33:50 -070027#include <memory>
Mark Salyzyn129feb22015-08-19 13:10:14 -070028
Tom Cherryc5c9eba2020-10-06 10:22:35 -070029#include <android-base/thread_annotations.h>
Mark Salyzyn8b8bfd22016-09-30 13:30:33 -070030#include <log/log.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080031
Tom Cherry5ecfbf02020-05-11 16:29:29 -070032#include "LogBuffer.h"
Tom Cherryadf2e442020-05-14 19:25:05 -070033#include "LogWriter.h"
Tom Cherryc5c9eba2020-10-06 10:22:35 -070034#include "LogdLock.h"
Tom Cherry5ecfbf02020-05-11 16:29:29 -070035
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -080036struct 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
Tom Cherry0281b202020-05-12 13:16:41 -070046class LogReaderList;
Mark Salyzyn12bac902014-02-26 09:50:16 -080047
Tom Cherry1647cd42020-05-04 12:53:36 -070048class LogReaderThread {
Tom Cherryac228f12019-08-21 14:16:34 -070049 public:
Tom Cherryadf2e442020-05-14 19:25:05 -070050 LogReaderThread(LogBuffer* log_buffer, LogReaderList* reader_list,
51 std::unique_ptr<LogWriter> writer, bool non_block, unsigned long tail,
Tom Cherryd444ab42020-05-28 12:38:21 -070052 LogMask log_mask, pid_t pid, log_time start_time, uint64_t sequence,
Tom Cherryadf2e442020-05-14 19:25:05 -070053 std::chrono::steady_clock::time_point deadline);
Tom Cherryc5c9eba2020-10-06 10:22:35 -070054 void TriggerReader() REQUIRES(logd_lock) { thread_triggered_condition_.notify_all(); }
Mark Salyzyn8aef2592014-08-07 08:16:52 -070055
Tom Cherryc5c9eba2020-10-06 10:22:35 -070056 void TriggerSkip(log_id_t id, unsigned int skip) REQUIRES(logd_lock) { skip_ahead_[id] = skip; }
57 void CleanSkip() REQUIRES(logd_lock) { memset(skip_ahead_, 0, sizeof(skip_ahead_)); }
Mark Salyzyn12bac902014-02-26 09:50:16 -080058
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -080059 void Run() REQUIRES(logd_lock);
60 void Revoke() REQUIRES(logd_lock) { writer_->revoke(); }
Tom Cherryc5c9eba2020-10-06 10:22:35 -070061 void Release() REQUIRES(logd_lock) {
Jintao Zhu1530e7c2018-12-20 23:10:41 +080062 // gracefully shut down the socket.
Tom Cherryadf2e442020-05-14 19:25:05 -070063 writer_->Shutdown();
Tom Cherry7e3bc962020-05-04 17:10:16 -070064 release_ = true;
Tom Cherry5ecfbf02020-05-11 16:29:29 -070065 thread_triggered_condition_.notify_all();
Mark Salyzyn12bac902014-02-26 09:50:16 -080066 }
67
Tom Cherryc5c9eba2020-10-06 10:22:35 -070068 bool IsWatching(log_id_t id) const REQUIRES(logd_lock) {
69 return flush_to_state_->log_mask() & (1 << id);
70 }
71 bool IsWatchingMultiple(LogMask log_mask) const REQUIRES(logd_lock) {
72 return flush_to_state_->log_mask() & log_mask;
Tom Cherryd444ab42020-05-28 12:38:21 -070073 }
Tom Cherry7e3bc962020-05-04 17:10:16 -070074
Tom Cherryc5c9eba2020-10-06 10:22:35 -070075 std::string name() const REQUIRES(logd_lock) { return writer_->name(); }
76 uint64_t start() const REQUIRES(logd_lock) { return flush_to_state_->start(); }
77 std::chrono::steady_clock::time_point deadline() const REQUIRES(logd_lock) { return deadline_; }
78 FlushToState& flush_to_state() REQUIRES(logd_lock) { return *flush_to_state_; }
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -080079 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 }
83 const PendingReaderThreadKey& pending_reader_thread_key() const REQUIRES(logd_lock) {
84 return pending_reader_thread_key_;
85 }
86 void set_finish_flag() REQUIRES(logd_lock) { finished_ = true; }
87 bool finish_flag() REQUIRES(logd_lock) { return finished_; }
88 void set_track_flag() REQUIRES(logd_lock) { tracked_ = true; }
89 bool track_flag() REQUIRES(logd_lock) { return tracked_; }
Tom Cherry3086c182020-05-04 11:13:55 -070090
91 private:
Tom Cherry7e3bc962020-05-04 17:10:16 -070092 void ThreadFunction();
93 // flushTo filter callbacks
Tom Cherryc5c9eba2020-10-06 10:22:35 -070094 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);
Tom Cherry7e3bc962020-05-04 17:10:16 -070098
Tom Cherryadf2e442020-05-14 19:25:05 -070099 std::condition_variable thread_triggered_condition_;
100 LogBuffer* log_buffer_;
101 LogReaderList* reader_list_;
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700102 std::unique_ptr<LogWriter> writer_ GUARDED_BY(logd_lock);
Tom Cherryadf2e442020-05-14 19:25:05 -0700103
Wenhao Wangb1f6c6c2021-11-19 16:42:43 -0800104 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
Tom Cherry7e3bc962020-05-04 17:10:16 -0700110 // Set to true to cause the thread to end and the LogReaderThread to delete itself.
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700111 bool release_ GUARDED_BY(logd_lock) = false;
Tom Cherry7e3bc962020-05-04 17:10:16 -0700112
Tom Cherry7e3bc962020-05-04 17:10:16 -0700113 // 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.
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700118 unsigned int skip_ahead_[LOG_ID_MAX] GUARDED_BY(logd_lock);
Tom Cherryd444ab42020-05-28 12:38:21 -0700119 // LogBuffer::FlushTo() needs to store state across subsequent calls.
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700120 std::unique_ptr<FlushToState> flush_to_state_ GUARDED_BY(logd_lock);
Tom Cherry7e3bc962020-05-04 17:10:16 -0700121
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
Tom Cherry7e3bc962020-05-04 17:10:16 -0700133 // 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_;
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700136 // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs
Tom Cherry7e3bc962020-05-04 17:10:16 -0700137 // wrap, then wake up and send the logs to the reader anyway.
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700138 std::chrono::steady_clock::time_point deadline_ GUARDED_BY(logd_lock);
Tom Cherry7e3bc962020-05-04 17:10:16 -0700139 // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs.
140 const bool non_block_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800141};