blob: 60a35078024dfc1654e24d14b675364dc057f26f [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
Mark Salyzynfa3716b2014-02-14 16:05:05 -080017#include <ctype.h>
Mark Salyzyn0175b072014-02-26 09:50:16 -080018#include <poll.h>
19#include <sys/socket.h>
20#include <cutils/sockets.h>
21
22#include "LogReader.h"
23#include "FlushCommand.h"
24
25LogReader::LogReader(LogBuffer *logbuf)
26 : SocketListener("logdr", true)
27 , mLogbuf(*logbuf)
28{ }
29
30// When we are notified a new log entry is available, inform
31// all of our listening sockets.
32void LogReader::notifyNewLog() {
33 FlushCommand command(*this);
34 runOnEachSocket(&command);
35}
36
37bool LogReader::onDataAvailable(SocketClient *cli) {
38 char buffer[255];
39
40 int len = read(cli->getSocket(), buffer, sizeof(buffer) - 1);
41 if (len <= 0) {
42 doSocketDelete(cli);
43 return false;
44 }
45 buffer[len] = '\0';
46
47 unsigned long tail = 0;
48 static const char _tail[] = " tail=";
49 char *cp = strstr(buffer, _tail);
50 if (cp) {
51 tail = atol(cp + sizeof(_tail) - 1);
52 }
53
Mark Salyzynfa3716b2014-02-14 16:05:05 -080054 log_time start(log_time::EPOCH);
55 static const char _start[] = " start=";
56 cp = strstr(buffer, _start);
57 if (cp) {
58 // Parse errors will result in current time
59 start.strptime(cp + sizeof(_start) - 1, "%s.%q");
60 }
61
Mark Salyzyn0175b072014-02-26 09:50:16 -080062 unsigned int logMask = -1;
63 static const char _logIds[] = " lids=";
64 cp = strstr(buffer, _logIds);
65 if (cp) {
66 logMask = 0;
67 cp += sizeof(_logIds) - 1;
68 while (*cp && *cp != '\0') {
69 int val = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -080070 while (isdigit(*cp)) {
71 val = val * 10 + *cp - '0';
Mark Salyzyn0175b072014-02-26 09:50:16 -080072 ++cp;
73 }
74 logMask |= 1 << val;
75 if (*cp != ',') {
76 break;
77 }
78 ++cp;
79 }
80 }
81
82 pid_t pid = 0;
83 static const char _pid[] = " pid=";
84 cp = strstr(buffer, _pid);
85 if (cp) {
86 pid = atol(cp + sizeof(_pid) - 1);
87 }
88
89 bool nonBlock = false;
90 if (strncmp(buffer, "dumpAndClose", 12) == 0) {
91 nonBlock = true;
92 }
93
Mark Salyzynfa3716b2014-02-14 16:05:05 -080094 // Convert realtime to monotonic time
Mark Salyzynfa3716b2014-02-14 16:05:05 -080095 if (start == log_time::EPOCH) {
96 start = LogTimeEntry::EPOCH;
Mark Salyzyna1c60cf2014-02-19 07:33:12 -080097 } else {
98 class LogFindStart {
99 const pid_t mPid;
100 const unsigned mLogMask;
101 bool startTimeSet;
102 log_time &start;
103 log_time last;
104
105 public:
106 LogFindStart(unsigned logMask, pid_t pid, log_time &start)
107 : mPid(pid)
108 , mLogMask(logMask)
109 , startTimeSet(false)
110 , start(start)
111 , last(LogTimeEntry::EPOCH)
112 { }
113
114 static bool callback(const LogBufferElement *element, void *obj) {
115 LogFindStart *me = reinterpret_cast<LogFindStart *>(obj);
116 if (!me->startTimeSet
117 && (!me->mPid || (me->mPid == element->getPid()))
118 && (me->mLogMask & (1 << element->getLogId()))) {
119 if (me->start == element->getRealTime()) {
120 me->start = element->getMonotonicTime();
121 me->startTimeSet = true;
122 } else {
123 if (me->start < element->getRealTime()) {
124 me->start = me->last;
125 me->startTimeSet = true;
126 }
127 me->last = element->getMonotonicTime();
128 }
129 }
130 return false;
131 }
132
133 bool found() { return startTimeSet; }
134 } logFindStart(logMask, pid, start);
135
136 logbuf().flushTo(cli, LogTimeEntry::EPOCH,
137 FlushCommand::hasReadLogs(cli),
138 logFindStart.callback, &logFindStart);
139
140 if (!logFindStart.found()) {
141 if (nonBlock) {
142 doSocketDelete(cli);
143 return false;
144 }
145 log_time now(CLOCK_MONOTONIC);
146 start = now;
147 }
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800148 }
149
150 FlushCommand command(*this, nonBlock, tail, logMask, pid, start);
Mark Salyzyn0175b072014-02-26 09:50:16 -0800151 command.runSocketCommand(cli);
152 return true;
153}
154
155void LogReader::doSocketDelete(SocketClient *cli) {
156 LastLogTimes &times = mLogbuf.mTimes;
157 LogTimeEntry::lock();
158 LastLogTimes::iterator it = times.begin();
159 while(it != times.end()) {
160 LogTimeEntry *entry = (*it);
161 if (entry->mClient == cli) {
162 times.erase(it);
163 entry->release_Locked();
164 break;
165 }
166 it++;
167 }
168 LogTimeEntry::unlock();
169}