Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 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 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 17 | #pragma once |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <pthread.h> |
Jintao Zhu | 1530e7c | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | 6505953 | 2017-03-10 14:31:54 -0800 | [diff] [blame] | 22 | #include <time.h> |
Mark Salyzyn | 129feb2 | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 23 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 24 | #include <chrono> |
| 25 | #include <condition_variable> |
Mark Salyzyn | 129feb2 | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 26 | #include <list> |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 27 | #include <memory> |
Mark Salyzyn | 129feb2 | 2015-08-19 13:10:14 -0700 | [diff] [blame] | 28 | |
Mark Salyzyn | 8b8bfd2 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 30 | #include <sysutils/SocketClient.h> |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 31 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 32 | #include "LogBuffer.h" |
| 33 | |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 34 | class LogReader; |
Mark Salyzyn | a92051d | 2016-02-23 08:55:43 -0800 | [diff] [blame] | 35 | class LogBufferElement; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 36 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 37 | class LogReaderThread { |
Tom Cherry | ac228f1 | 2019-08-21 14:16:34 -0700 | [diff] [blame] | 38 | public: |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 39 | 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 Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 44 | |
Tom Cherry | 06e478b | 2018-10-08 17:33:50 -0700 | [diff] [blame] | 45 | bool startReader_Locked(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 47 | void triggerReader_Locked() { thread_triggered_condition_.notify_all(); } |
Mark Salyzyn | 8aef259 | 2014-08-07 08:16:52 -0700 | [diff] [blame] | 48 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 49 | void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; } |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 50 | void cleanSkip_Locked(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 51 | |
Tom Cherry | 1647cd4 | 2020-05-04 12:53:36 -0700 | [diff] [blame] | 52 | void release_Locked() { |
Jintao Zhu | 1530e7c | 2018-12-20 23:10:41 +0800 | [diff] [blame] | 53 | // gracefully shut down the socket. |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 54 | shutdown(client_->getSocket(), SHUT_RDWR); |
| 55 | release_ = true; |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 56 | thread_triggered_condition_.notify_all(); |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 59 | 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 Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 64 | std::chrono::steady_clock::time_point deadline() const { return deadline_; } |
Tom Cherry | 3086c18 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 67 | void ThreadFunction(); |
| 68 | // flushTo filter callbacks |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 69 | FlushToResult FilterFirstPass(const LogBufferElement* element); |
| 70 | FlushToResult FilterSecondPass(const LogBufferElement* element); |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 71 | |
| 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 Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 79 | std::condition_variable thread_triggered_condition_; |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 80 | |
| 81 | // Reference to the parent thread that manages log reader sockets. |
| 82 | LogReader& reader_; |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 83 | // Reference to the parent list that shares its lock with each instance |
| 84 | LogReaderList& reader_list_; |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 85 | // 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 Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 114 | // CLOCK_MONOTONIC based deadline used for log wrapping. If this deadline expires before logs |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 115 | // wrap, then wake up and send the logs to the reader anyway. |
Tom Cherry | 5ecfbf0 | 2020-05-11 16:29:29 -0700 | [diff] [blame^] | 116 | std::chrono::steady_clock::time_point deadline_; |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 117 | // 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 Cherry | 3086c18 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 122 | bool privileged_; |
Tom Cherry | 7e3bc96 | 2020-05-04 17:10:16 -0700 | [diff] [blame] | 123 | // Whether or not this reader can read security logs. See CanReadSecurityLogs(). |
Tom Cherry | 3086c18 | 2020-05-04 11:13:55 -0700 | [diff] [blame] | 124 | bool can_read_security_logs_; |
Mark Salyzyn | 12bac90 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 125 | }; |