blob: ce59cbbe2ae5ce04e33fe8f1e5cf7c19ccf6e893 [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
Mark Salyzyn8b8bfd22016-09-30 13:30:33 -070029#include <log/log.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080030#include <sysutils/SocketClient.h>
Mark Salyzyn12bac902014-02-26 09:50:16 -080031
Tom Cherry5ecfbf02020-05-11 16:29:29 -070032#include "LogBuffer.h"
33
Mark Salyzyn12bac902014-02-26 09:50:16 -080034class LogReader;
Mark Salyzyna92051d2016-02-23 08:55:43 -080035class LogBufferElement;
Mark Salyzyn12bac902014-02-26 09:50:16 -080036
Tom Cherry1647cd42020-05-04 12:53:36 -070037class LogReaderThread {
Tom Cherryac228f12019-08-21 14:16:34 -070038 public:
Tom Cherry5ecfbf02020-05-11 16:29:29 -070039 LogReaderThread(LogReader& reader, LogReaderList& reader_list, SocketClient* client,
40 bool non_block, unsigned long tail, unsigned int log_mask, pid_t pid,
41 log_time start_time, uint64_t sequence,
42 std::chrono::steady_clock::time_point deadline, bool privileged,
43 bool can_read_security_logs);
Mark Salyzyn12bac902014-02-26 09:50:16 -080044
Tom Cherry06e478b2018-10-08 17:33:50 -070045 bool startReader_Locked();
Mark Salyzyn12bac902014-02-26 09:50:16 -080046
Tom Cherry5ecfbf02020-05-11 16:29:29 -070047 void triggerReader_Locked() { thread_triggered_condition_.notify_all(); }
Mark Salyzyn8aef2592014-08-07 08:16:52 -070048
Tom Cherry7e3bc962020-05-04 17:10:16 -070049 void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; }
Tom Cherry1647cd42020-05-04 12:53:36 -070050 void cleanSkip_Locked();
Mark Salyzyn12bac902014-02-26 09:50:16 -080051
Tom Cherry1647cd42020-05-04 12:53:36 -070052 void release_Locked() {
Jintao Zhu1530e7c2018-12-20 23:10:41 +080053 // gracefully shut down the socket.
Tom Cherry7e3bc962020-05-04 17:10:16 -070054 shutdown(client_->getSocket(), SHUT_RDWR);
55 release_ = true;
Tom Cherry5ecfbf02020-05-11 16:29:29 -070056 thread_triggered_condition_.notify_all();
Mark Salyzyn12bac902014-02-26 09:50:16 -080057 }
58
Tom Cherry7e3bc962020-05-04 17:10:16 -070059 bool IsWatching(log_id_t id) const { return log_mask_ & (1 << id); }
60 bool IsWatchingMultiple(unsigned int log_mask) const { return log_mask_ & log_mask; }
61
62 const SocketClient* client() const { return client_; }
63 uint64_t start() const { return start_; }
Tom Cherry5ecfbf02020-05-11 16:29:29 -070064 std::chrono::steady_clock::time_point deadline() const { return deadline_; }
Tom Cherry3086c182020-05-04 11:13:55 -070065
66 private:
Tom Cherry7e3bc962020-05-04 17:10:16 -070067 void ThreadFunction();
68 // flushTo filter callbacks
Tom Cherry5ecfbf02020-05-11 16:29:29 -070069 FlushToResult FilterFirstPass(const LogBufferElement* element);
70 FlushToResult FilterSecondPass(const LogBufferElement* element);
Tom Cherry7e3bc962020-05-04 17:10:16 -070071
72 // Set to true to cause the thread to end and the LogReaderThread to delete itself.
73 bool release_ = false;
74 // Indicates whether or not 'leading' (first logs seen starting from start_) 'dropped' (chatty)
75 // messages should be ignored.
76 bool leading_dropped_;
77
78 // Condition variable for waking the reader thread if there are messages pending for its client.
Tom Cherry5ecfbf02020-05-11 16:29:29 -070079 std::condition_variable thread_triggered_condition_;
Tom Cherry7e3bc962020-05-04 17:10:16 -070080
81 // Reference to the parent thread that manages log reader sockets.
82 LogReader& reader_;
Tom Cherry5ecfbf02020-05-11 16:29:29 -070083 // Reference to the parent list that shares its lock with each instance
84 LogReaderList& reader_list_;
Tom Cherry7e3bc962020-05-04 17:10:16 -070085 // A mask of the logs buffers that are read by this reader.
86 const unsigned int log_mask_;
87 // If set to non-zero, only pids equal to this are read by the reader.
88 const pid_t pid_;
89 // When a reader is referencing (via start_) old elements in the log buffer, and the log
90 // buffer's size grows past its memory limit, the log buffer may request the reader to skip
91 // ahead a specified number of logs.
92 unsigned int skip_ahead_[LOG_ID_MAX];
93 // Used for distinguishing 'dropped' messages for duplicate logs vs chatty drops
94 pid_t last_tid_[LOG_ID_MAX];
95
96 // These next three variables are used for reading only the most recent lines aka `adb logcat
97 // -t` / `adb logcat -T`.
98 // tail_ is the number of most recent lines to print.
99 unsigned long tail_;
100 // count_ is the result of a first pass through the log buffer to determine how many total
101 // messages there are.
102 unsigned long count_;
103 // index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_)
104 // and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_.
105 unsigned long index_;
106
107 // A pointer to the socket for this reader.
108 SocketClient* client_;
109 // When a reader requests logs starting from a given timestamp, its stored here for the first
110 // pass, such that logs before this time stamp that are accumulated in the buffer are ignored.
111 log_time start_time_;
112 // The point from which the reader will read logs once awoken.
113 uint64_t start_;
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700114 // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs
Tom Cherry7e3bc962020-05-04 17:10:16 -0700115 // wrap, then wake up and send the logs to the reader anyway.
Tom Cherry5ecfbf02020-05-11 16:29:29 -0700116 std::chrono::steady_clock::time_point deadline_;
Tom Cherry7e3bc962020-05-04 17:10:16 -0700117 // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs.
118 const bool non_block_;
119
120 // Whether or not this reader can read logs from all UIDs or only its own UID. See
121 // clientHasLogCredentials().
Tom Cherry3086c182020-05-04 11:13:55 -0700122 bool privileged_;
Tom Cherry7e3bc962020-05-04 17:10:16 -0700123 // Whether or not this reader can read security logs. See CanReadSecurityLogs().
Tom Cherry3086c182020-05-04 11:13:55 -0700124 bool can_read_security_logs_;
Mark Salyzyn12bac902014-02-26 09:50:16 -0800125};