summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2016-09-12 16:51:54 -0500
committerMark Salyzyn2017-02-02 16:24:18 -0600
commit61e9ce6709a12cf39a9471476da3d50339efe466 (patch)
treec461a90ad53a38bf2baab5a3d9ec922aded478ea /logd/LogTags.h
parent3dfe40129657165f4cfe6c6cf0a5b6c8e8e6d4da (diff)
downloadplatform-system-core-61e9ce6709a12cf39a9471476da3d50339efe466.tar.gz
platform-system-core-61e9ce6709a12cf39a9471476da3d50339efe466.tar.xz
platform-system-core-61e9ce6709a12cf39a9471476da3d50339efe466.zip
logd: add getEventTag command and service
Will register a new event tag by name and format, and return an event-log-tags format response with the newly allocated tag. If format is not specified, then nothing will be recorded, but a pre-existing named entry will be listed. If name and format are not specified, list all dynamic entries. If name=* list all event log tag entries. Stickiness through logd crash will be managed with the tmpfs file /dev/event-log-tags and through a reboot with add_tag entries in the pmsg last logcat event log. On debug builds we retain a /data/misc/logd/event-log-tags file that aids stickiness and that can be picked up by the bugreport. If we detect truncation damage to /dev/event-log-tags, or to /data/misc/logd/event-log-tags, rebuild file with a new first line signature incorporating the time so mmap'd readers of the file can detect the possible change in shape and order. Manual testing: Make sure nc (netcat) is built for the target platform on the host: $ m nc Then the following can be used to issue a request on the platform: $ echo -n 'getEventTag name=<name> format="<format>"\0EXIT\0' | > nc -U /dev/socket/logd Test: gTest logd-unit-test --gtest_filter=getEventTag* Bug: 31456426 Change-Id: I5dacc5f84a24d52dae09cca5ee1a3a9f9207f06d
Diffstat (limited to 'logd/LogTags.h')
-rw-r--r--logd/LogTags.h118
1 files changed, 118 insertions, 0 deletions
diff --git a/logd/LogTags.h b/logd/LogTags.h
new file mode 100644
index 000000000..37a6d9632
--- /dev/null
+++ b/logd/LogTags.h
@@ -0,0 +1,118 @@
1/*
2 * Copyright (C) 2017 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
17#ifndef _LOGD_LOG_TAGS_H__
18#define _LOGD_LOG_TAGS_H__
19
20#include <unordered_map>
21#include <unordered_set>
22#include <string>
23
24#include <utils/RWLock.h>
25
26class LogTags {
27 // This lock protects all the unordered_map accesses below. It
28 // is a reader/writer lock so that contentions are kept to a
29 // minimum since writes are rare, even administratably when
30 // reads are extended. Resist the temptation to use the writer
31 // lock to protect anything outside the following unordered_maps
32 // as that would increase the reader contentions. Use a separate
33 // mutex to protect the other entities.
34 android::RWLock rwlock;
35
36 // key is Name + "+" + Format
37 std::unordered_map<std::string, uint32_t> key2tag;
38 typedef std::unordered_map<std::string, uint32_t>::const_iterator key2tag_const_iterator;
39
40 // Allows us to manage access permissions based on uid registrants
41 // Global entries are specifically erased.
42 typedef std::unordered_set<uid_t> uid_list;
43 std::unordered_map<uint32_t, uid_list> tag2uid;
44 typedef std::unordered_map<uint32_t, uid_list>::const_iterator tag2uid_const_iterator;
45
46 std::unordered_map<uint32_t, std::string> tag2name;
47 typedef std::unordered_map<uint32_t, std::string>::const_iterator tag2name_const_iterator;
48
49 std::unordered_map<uint32_t, std::string> tag2format;
50 typedef std::unordered_map<uint32_t, std::string>::const_iterator tag2format_const_iterator;
51
52 static const size_t max_per_uid = 256; // Put a cap on the tags per uid
53 std::unordered_map<uid_t, size_t> uid2count;
54 typedef std::unordered_map<uid_t, size_t>::const_iterator uid2count_const_iterator;
55
56 // Dynamic entries are assigned
57 std::unordered_map<uint32_t, size_t> tag2total;
58 typedef std::unordered_map<uint32_t, size_t>::const_iterator tag2total_const_iterator;
59
60 // emplace unique tag
61 uint32_t nameToTag(uid_t uid, const char* name, const char* format);
62 // find unique or associated tag
63 uint32_t nameToTag_locked(const std::string& name, const char* format, bool &unique);
64
65 // Record expected file watermarks to detect corruption.
66 std::unordered_map<std::string, size_t> file2watermark;
67 typedef std::unordered_map<std::string, size_t>::const_iterator file2watermark_const_iterator;
68
69 void ReadPersistEventLogTags();
70
71 // format helpers
72 // format a single entry, does not need object data
73 static std::string formatEntry(uint32_t tag, uid_t uid,
74 const char* name, const char* format);
75 // caller locks, database lookup, authenticate against uid
76 std::string formatEntry_locked(uint32_t tag, uid_t uid,
77 bool authenticate = true);
78
79 bool RebuildFileEventLogTags(const char* filename, bool warn = true);
80
81 void AddEventLogTags(uint32_t tag, uid_t uid,
82 const std::string& Name, const std::string& Format,
83 const char* source = NULL, bool warn = false);
84
85 void WriteDynamicEventLogTags(uint32_t tag, uid_t uid);
86 void WriteDebugEventLogTags(uint32_t tag, uid_t uid);
87 // push tag details to persistent storage
88 void WritePersistEventLogTags(uint32_t tag,
89 uid_t uid = AID_ROOT,
90 const char* source = NULL);
91
92 static const uint32_t emptyTag = uint32_t(-1);
93
94public:
95
96 static const char system_event_log_tags[];
97 static const char dynamic_event_log_tags[];
98 // Only for userdebug and eng
99 static const char debug_event_log_tags[];
100
101 LogTags();
102
103 void WritePmsgEventLogTags(uint32_t tag, uid_t uid = AID_ROOT);
104 void ReadFileEventLogTags(const char* filename, bool warn = true);
105
106 // reverse lookup from tag
107 const char* tagToName(uint32_t tag) const;
108 const char* tagToFormat(uint32_t tag) const;
109 // find associated tag
110 uint32_t nameToTag(const char* name) const;
111
112 // emplace tag if necessary, provide event-log-tag formated output in string
113 std::string formatGetEventTag(uid_t uid,
114 const char* name,
115 const char* format);
116};
117
118#endif // _LOGD_LOG_TAGS_H__