blob: f828b6ec9f181559804045c4da0e2f74698a078e [file] [log] [blame]
Mark Salyzyn0175b072014-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 Cherry6ec71e92020-05-04 12:53:36 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
19#include <pthread.h>
Jintao Zhud3987a92018-12-20 23:10:41 +080020#include <sys/socket.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080021#include <sys/types.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080022#include <time.h>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070023
Tom Cherry68630a02020-05-11 16:29:29 -070024#include <chrono>
25#include <condition_variable>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070026#include <list>
Tom Cherry4f227862018-10-08 17:33:50 -070027#include <memory>
Mark Salyzyn98dca2d2015-08-19 13:10:14 -070028
Mark Salyzynaeaaf812016-09-30 13:30:33 -070029#include <log/log.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080030#include <sysutils/SocketClient.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080031
Tom Cherry68630a02020-05-11 16:29:29 -070032#include "LogBuffer.h"
33
Mark Salyzyn0175b072014-02-26 09:50:16 -080034class LogReader;
Mark Salyzyn317bfb92016-02-23 08:55:43 -080035class LogBufferElement;
Tom Cherryd5b38382020-05-12 13:16:41 -070036class LogReaderList;
Mark Salyzyn0175b072014-02-26 09:50:16 -080037
Tom Cherry6ec71e92020-05-04 12:53:36 -070038class LogReaderThread {
Tom Cherry10d086e2019-08-21 14:16:34 -070039 public:
Tom Cherry68630a02020-05-11 16:29:29 -070040 LogReaderThread(LogReader& reader, LogReaderList& reader_list, SocketClient* client,
41 bool non_block, unsigned long tail, unsigned int log_mask, pid_t pid,
42 log_time start_time, uint64_t sequence,
43 std::chrono::steady_clock::time_point deadline, bool privileged,
44 bool can_read_security_logs);
Mark Salyzyn0175b072014-02-26 09:50:16 -080045
Tom Cherry4f227862018-10-08 17:33:50 -070046 bool startReader_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080047
Tom Cherry68630a02020-05-11 16:29:29 -070048 void triggerReader_Locked() { thread_triggered_condition_.notify_all(); }
Mark Salyzyna16f7612014-08-07 08:16:52 -070049
Tom Cherrycef47bb2020-05-04 17:10:16 -070050 void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; }
Tom Cherry6ec71e92020-05-04 12:53:36 -070051 void cleanSkip_Locked();
Mark Salyzyn0175b072014-02-26 09:50:16 -080052
Tom Cherry6ec71e92020-05-04 12:53:36 -070053 void release_Locked() {
Jintao Zhud3987a92018-12-20 23:10:41 +080054 // gracefully shut down the socket.
Tom Cherrycef47bb2020-05-04 17:10:16 -070055 shutdown(client_->getSocket(), SHUT_RDWR);
56 release_ = true;
Tom Cherry68630a02020-05-11 16:29:29 -070057 thread_triggered_condition_.notify_all();
Mark Salyzyn0175b072014-02-26 09:50:16 -080058 }
59
Tom Cherrycef47bb2020-05-04 17:10:16 -070060 bool IsWatching(log_id_t id) const { return log_mask_ & (1 << id); }
61 bool IsWatchingMultiple(unsigned int log_mask) const { return log_mask_ & log_mask; }
62
63 const SocketClient* client() const { return client_; }
64 uint64_t start() const { return start_; }
Tom Cherry68630a02020-05-11 16:29:29 -070065 std::chrono::steady_clock::time_point deadline() const { return deadline_; }
Tom Cherry79d54f72020-05-04 11:13:55 -070066
67 private:
Tom Cherrycef47bb2020-05-04 17:10:16 -070068 void ThreadFunction();
69 // flushTo filter callbacks
Tom Cherry68630a02020-05-11 16:29:29 -070070 FlushToResult FilterFirstPass(const LogBufferElement* element);
71 FlushToResult FilterSecondPass(const LogBufferElement* element);
Tom Cherrycef47bb2020-05-04 17:10:16 -070072
73 // Set to true to cause the thread to end and the LogReaderThread to delete itself.
74 bool release_ = false;
75 // Indicates whether or not 'leading' (first logs seen starting from start_) 'dropped' (chatty)
76 // messages should be ignored.
77 bool leading_dropped_;
78
79 // Condition variable for waking the reader thread if there are messages pending for its client.
Tom Cherry68630a02020-05-11 16:29:29 -070080 std::condition_variable thread_triggered_condition_;
Tom Cherrycef47bb2020-05-04 17:10:16 -070081
82 // Reference to the parent thread that manages log reader sockets.
83 LogReader& reader_;
Tom Cherry68630a02020-05-11 16:29:29 -070084 // Reference to the parent list that shares its lock with each instance
85 LogReaderList& reader_list_;
Tom Cherrycef47bb2020-05-04 17:10:16 -070086 // A mask of the logs buffers that are read by this reader.
87 const unsigned int log_mask_;
88 // If set to non-zero, only pids equal to this are read by the reader.
89 const pid_t pid_;
90 // When a reader is referencing (via start_) old elements in the log buffer, and the log
91 // buffer's size grows past its memory limit, the log buffer may request the reader to skip
92 // ahead a specified number of logs.
93 unsigned int skip_ahead_[LOG_ID_MAX];
94 // Used for distinguishing 'dropped' messages for duplicate logs vs chatty drops
95 pid_t last_tid_[LOG_ID_MAX];
96
97 // These next three variables are used for reading only the most recent lines aka `adb logcat
98 // -t` / `adb logcat -T`.
99 // tail_ is the number of most recent lines to print.
100 unsigned long tail_;
101 // count_ is the result of a first pass through the log buffer to determine how many total
102 // messages there are.
103 unsigned long count_;
104 // index_ is used along with count_ to only start sending lines once index_ > (count_ - tail_)
105 // and to disconnect the reader (if it is dumpAndClose, `adb logcat -t`), when index_ >= count_.
106 unsigned long index_;
107
108 // A pointer to the socket for this reader.
109 SocketClient* client_;
110 // When a reader requests logs starting from a given timestamp, its stored here for the first
111 // pass, such that logs before this time stamp that are accumulated in the buffer are ignored.
112 log_time start_time_;
113 // The point from which the reader will read logs once awoken.
114 uint64_t start_;
Tom Cherry68630a02020-05-11 16:29:29 -0700115 // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs
Tom Cherrycef47bb2020-05-04 17:10:16 -0700116 // wrap, then wake up and send the logs to the reader anyway.
Tom Cherry68630a02020-05-11 16:29:29 -0700117 std::chrono::steady_clock::time_point deadline_;
Tom Cherrycef47bb2020-05-04 17:10:16 -0700118 // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs.
119 const bool non_block_;
120
121 // Whether or not this reader can read logs from all UIDs or only its own UID. See
122 // clientHasLogCredentials().
Tom Cherry79d54f72020-05-04 11:13:55 -0700123 bool privileged_;
Tom Cherrycef47bb2020-05-04 17:10:16 -0700124 // Whether or not this reader can read security logs. See CanReadSecurityLogs().
Tom Cherry79d54f72020-05-04 11:13:55 -0700125 bool can_read_security_logs_;
Mark Salyzyn0175b072014-02-26 09:50:16 -0800126};