Mark Salyzyn | 0175b07 | 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 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 18 | #include <poll.h> |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 19 | #include <sys/prctl.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 20 | #include <sys/socket.h> |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 21 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 22 | #include <cutils/sockets.h> |
| 23 | |
| 24 | #include "LogReader.h" |
| 25 | #include "FlushCommand.h" |
| 26 | |
| 27 | LogReader::LogReader(LogBuffer *logbuf) |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 28 | : SocketListener(getLogSocket(), true) |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 29 | , mLogbuf(*logbuf) |
| 30 | { } |
| 31 | |
| 32 | // When we are notified a new log entry is available, inform |
| 33 | // all of our listening sockets. |
| 34 | void LogReader::notifyNewLog() { |
| 35 | FlushCommand command(*this); |
| 36 | runOnEachSocket(&command); |
| 37 | } |
| 38 | |
| 39 | bool LogReader::onDataAvailable(SocketClient *cli) { |
Mark Salyzyn | 8daa9af | 2014-04-28 14:07:23 -0700 | [diff] [blame] | 40 | prctl(PR_SET_NAME, "logd.reader"); |
| 41 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 42 | char buffer[255]; |
| 43 | |
| 44 | int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1); |
| 45 | if (len <= 0) { |
| 46 | doSocketDelete(cli); |
| 47 | return false; |
| 48 | } |
| 49 | buffer[len] = '\0'; |
| 50 | |
| 51 | unsigned long tail = 0; |
| 52 | static const char _tail[] = " tail="; |
| 53 | char *cp = strstr(buffer, _tail); |
| 54 | if (cp) { |
| 55 | tail = atol(cp + sizeof(_tail) - 1); |
| 56 | } |
| 57 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 58 | log_time start(log_time::EPOCH); |
| 59 | static const char _start[] = " start="; |
| 60 | cp = strstr(buffer, _start); |
| 61 | if (cp) { |
| 62 | // Parse errors will result in current time |
| 63 | start.strptime(cp + sizeof(_start) - 1, "%s.%q"); |
| 64 | } |
| 65 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 66 | unsigned int logMask = -1; |
| 67 | static const char _logIds[] = " lids="; |
| 68 | cp = strstr(buffer, _logIds); |
| 69 | if (cp) { |
| 70 | logMask = 0; |
| 71 | cp += sizeof(_logIds) - 1; |
| 72 | while (*cp && *cp != '\0') { |
| 73 | int val = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 74 | while (isdigit(*cp)) { |
| 75 | val = val * 10 + *cp - '0'; |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 76 | ++cp; |
| 77 | } |
| 78 | logMask |= 1 << val; |
| 79 | if (*cp != ',') { |
| 80 | break; |
| 81 | } |
| 82 | ++cp; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pid_t pid = 0; |
| 87 | static const char _pid[] = " pid="; |
| 88 | cp = strstr(buffer, _pid); |
| 89 | if (cp) { |
| 90 | pid = atol(cp + sizeof(_pid) - 1); |
| 91 | } |
| 92 | |
| 93 | bool nonBlock = false; |
| 94 | if (strncmp(buffer, "dumpAndClose", 12) == 0) { |
Mark Salyzyn | f669acb | 2014-09-16 09:19:47 -0700 | [diff] [blame] | 95 | // Allow writer to get some cycles, and wait for pending notifications |
| 96 | sched_yield(); |
| 97 | LogTimeEntry::lock(); |
| 98 | LogTimeEntry::unlock(); |
| 99 | sched_yield(); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 100 | nonBlock = true; |
| 101 | } |
| 102 | |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 103 | // Convert realtime to monotonic time |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 104 | if (start == log_time::EPOCH) { |
| 105 | start = LogTimeEntry::EPOCH; |
Mark Salyzyn | a1c60cf | 2014-02-19 07:33:12 -0800 | [diff] [blame] | 106 | } else { |
| 107 | class LogFindStart { |
| 108 | const pid_t mPid; |
| 109 | const unsigned mLogMask; |
| 110 | bool startTimeSet; |
| 111 | log_time &start; |
| 112 | log_time last; |
| 113 | |
| 114 | public: |
| 115 | LogFindStart(unsigned logMask, pid_t pid, log_time &start) |
| 116 | : mPid(pid) |
| 117 | , mLogMask(logMask) |
| 118 | , startTimeSet(false) |
| 119 | , start(start) |
| 120 | , last(LogTimeEntry::EPOCH) |
| 121 | { } |
| 122 | |
| 123 | static bool callback(const LogBufferElement *element, void *obj) { |
| 124 | LogFindStart *me = reinterpret_cast<LogFindStart *>(obj); |
| 125 | if (!me->startTimeSet |
| 126 | && (!me->mPid || (me->mPid == element->getPid())) |
| 127 | && (me->mLogMask & (1 << element->getLogId()))) { |
| 128 | if (me->start == element->getRealTime()) { |
| 129 | me->start = element->getMonotonicTime(); |
| 130 | me->startTimeSet = true; |
| 131 | } else { |
| 132 | if (me->start < element->getRealTime()) { |
| 133 | me->start = me->last; |
| 134 | me->startTimeSet = true; |
| 135 | } |
| 136 | me->last = element->getMonotonicTime(); |
| 137 | } |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool found() { return startTimeSet; } |
| 143 | } logFindStart(logMask, pid, start); |
| 144 | |
| 145 | logbuf().flushTo(cli, LogTimeEntry::EPOCH, |
| 146 | FlushCommand::hasReadLogs(cli), |
| 147 | logFindStart.callback, &logFindStart); |
| 148 | |
| 149 | if (!logFindStart.found()) { |
| 150 | if (nonBlock) { |
| 151 | doSocketDelete(cli); |
| 152 | return false; |
| 153 | } |
| 154 | log_time now(CLOCK_MONOTONIC); |
| 155 | start = now; |
| 156 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | FlushCommand command(*this, nonBlock, tail, logMask, pid, start); |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 160 | command.runSocketCommand(cli); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | void LogReader::doSocketDelete(SocketClient *cli) { |
| 165 | LastLogTimes × = mLogbuf.mTimes; |
| 166 | LogTimeEntry::lock(); |
| 167 | LastLogTimes::iterator it = times.begin(); |
| 168 | while(it != times.end()) { |
| 169 | LogTimeEntry *entry = (*it); |
| 170 | if (entry->mClient == cli) { |
| 171 | times.erase(it); |
| 172 | entry->release_Locked(); |
| 173 | break; |
| 174 | } |
| 175 | it++; |
| 176 | } |
| 177 | LogTimeEntry::unlock(); |
| 178 | } |
Mark Salyzyn | dfc47e8 | 2014-03-24 10:26:47 -0700 | [diff] [blame] | 179 | |
| 180 | int LogReader::getLogSocket() { |
| 181 | static const char socketName[] = "logdr"; |
| 182 | int sock = android_get_control_socket(socketName); |
| 183 | |
| 184 | if (sock < 0) { |
| 185 | sock = socket_local_server(socketName, |
| 186 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
| 187 | SOCK_SEQPACKET); |
| 188 | } |
| 189 | |
| 190 | return sock; |
| 191 | } |