blob: fb0c4c29543780e03eced285c4f6e31d8f30dcbd [file] [log] [blame]
Austin Schuhf2a50ba2016-12-24 16:16:26 -08001#include <dirent.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <mntent.h>
5#include <pwd.h>
brians343bc112013-02-10 01:53:46 +00006#include <stdio.h>
7#include <stdlib.h>
brians343bc112013-02-10 01:53:46 +00008#include <string.h>
brians343bc112013-02-10 01:53:46 +00009#include <sys/types.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -080010#include <time.h>
11#include <unistd.h>
12#include <string>
brians343bc112013-02-10 01:53:46 +000013
Austin Schuhf2a50ba2016-12-24 16:16:26 -080014#include <chrono>
brians343bc112013-02-10 01:53:46 +000015#include <map>
Brian Silverman88471dc2014-02-15 22:35:42 -080016#include <unordered_set>
brians343bc112013-02-10 01:53:46 +000017
John Park33858a32018-09-28 23:05:48 -070018#include "aos/die.h"
19#include "aos/logging/binary_log_file.h"
20#include "aos/logging/implementations.h"
21#include "aos/queue_types.h"
22#include "aos/time/time.h"
Austin Schuhf2a50ba2016-12-24 16:16:26 -080023#include "aos/linux_code/configuration.h"
24#include "aos/linux_code/init.h"
25#include "aos/linux_code/ipc_lib/queue.h"
brians343bc112013-02-10 01:53:46 +000026
Brian Silvermanf665d692013-02-17 22:11:39 -080027namespace aos {
28namespace logging {
Brian Silverman14fd0fb2014-01-14 21:42:01 -080029namespace linux_code {
Brian Silvermanf665d692013-02-17 22:11:39 -080030namespace {
brians343bc112013-02-10 01:53:46 +000031
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050032void CheckTypeWritten(uint32_t type_id, LogFileWriter *writer,
33 ::std::unordered_set<uint32_t> *written_type_ids) {
34 if (written_type_ids->count(type_id) > 0) return;
Brian Silverman88471dc2014-02-15 22:35:42 -080035 if (MessageType::IsPrimitive(type_id)) return;
36
37 const MessageType &type = type_cache::Get(type_id);
38 for (int i = 0; i < type.number_fields; ++i) {
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050039 CheckTypeWritten(type.fields[i]->type, writer, written_type_ids);
Brian Silverman88471dc2014-02-15 22:35:42 -080040 }
41
42 char buffer[1024];
43 ssize_t size = type.Serialize(buffer, sizeof(buffer));
44 if (size == -1) {
45 LOG(WARNING, "%zu-byte buffer is too small to serialize type %s\n",
46 sizeof(buffer), type.name.c_str());
47 return;
48 }
49 LogFileMessageHeader *const output =
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050050 writer->GetWritePosition(sizeof(LogFileMessageHeader) + size);
Brian Silverman88471dc2014-02-15 22:35:42 -080051
52 output->time_sec = output->time_nsec = 0;
53 output->source = getpid();
54 output->name_size = 0;
55 output->sequence = 0;
56 output->level = FATAL;
57
58 memcpy(output + 1, buffer, size);
59 output->message_size = size;
60
61 output->type = LogFileMessageHeader::MessageType::kStructType;
62 futex_set(&output->marker);
Brian Silvermanf7780312014-02-16 17:26:15 -080063
Brian Silvermanf5ca4d02015-03-01 16:52:24 -050064 written_type_ids->insert(type_id);
Brian Silverman88471dc2014-02-15 22:35:42 -080065}
66
Ben Fredricksona04c1752014-03-02 22:54:07 +000067void AllocateLogName(char **filename, const char *directory) {
68 int fileindex = 0;
Brian Silverman2adb1452014-05-13 08:43:38 -070069 DIR *const d = opendir(directory);
70 if (d == nullptr) {
71 PDie("could not open directory %s", directory);
72 }
73 int index = 0;
74 while (true) {
75 errno = 0;
76 struct dirent *const dir = readdir(d);
77 if (dir == nullptr) {
78 if (errno == 0) {
79 break;
80 } else {
81 PLOG(FATAL, "readdir(%p) failed", d);
82 }
83 } else {
Ben Fredricksona04c1752014-03-02 22:54:07 +000084 if (sscanf(dir->d_name, "aos_log-%d", &index) == 1) {
85 if (index >= fileindex) {
86 fileindex = index + 1;
87 }
88 }
89 }
Ben Fredricksona04c1752014-03-02 22:54:07 +000090 }
Brian Silverman2adb1452014-05-13 08:43:38 -070091 closedir(d);
Ben Fredricksona04c1752014-03-02 22:54:07 +000092
93 char previous[512];
Brian Silvermanf574e732014-03-02 17:35:04 -080094 ::std::string path = ::std::string(directory) + "/aos_log-current";
95 ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous));
Ben Fredricksona04c1752014-03-02 22:54:07 +000096 if (len != -1) {
97 previous[len] = '\0';
98 } else {
Brian Silvermanf574e732014-03-02 17:35:04 -080099 previous[0] = '\0';
100 LOG(INFO, "Could not find aos_log-current\n");
101 printf("Could not find aos_log-current\n");
Ben Fredricksona04c1752014-03-02 22:54:07 +0000102 }
Brian Silverman09e85ed2014-03-15 08:26:29 -0700103 if (asprintf(filename, "%s/aos_log-%03d", directory, fileindex) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700104 PDie("couldn't create final name");
Ben Fredricksona04c1752014-03-02 22:54:07 +0000105 }
Brian Silvermanf574e732014-03-02 17:35:04 -0800106 LOG(INFO, "Created log file (aos_log-%d) in directory (%s). Previous file "
107 "was (%s).\n",
108 fileindex, directory, previous);
109 printf("Created log file (aos_log-%d) in directory (%s). Previous file was "
110 "(%s).\n",
111 fileindex, directory, previous);
Ben Fredricksona04c1752014-03-02 22:54:07 +0000112}
113
Brian Silverman9f330492015-03-01 17:37:02 -0500114#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700115bool FoundThumbDrive(const char *path) {
116 FILE *mnt_fp = setmntent("/etc/mtab", "r");
117 if (mnt_fp == nullptr) {
118 Die("Could not open /etc/mtab");
119 }
120
121 bool found = false;
122 struct mntent mntbuf;
123 char buf[256];
124 while (!found) {
125 struct mntent *mount_list = getmntent_r(mnt_fp, &mntbuf, buf, sizeof(buf));
126 if (mount_list == nullptr) {
127 break;
128 }
129 if (strcmp(mount_list->mnt_dir, path) == 0) {
130 found = true;
131 }
132 }
133 endmntent(mnt_fp);
134 return found;
135}
136
137bool FindDevice(char *device, size_t device_size) {
138 char test_device[10];
139 for (char i = 'a'; i < 'z'; ++i) {
140 snprintf(test_device, sizeof(test_device), "/dev/sd%c", i);
141 LOG(INFO, "Trying to access %s\n", test_device);
142 if (access(test_device, F_OK) != -1) {
143 snprintf(device, device_size, "sd%c", i);
144 return true;
145 }
146 }
147 return false;
148}
149#endif
150
Brian Silvermanab6615c2013-03-05 20:29:29 -0800151int BinaryLogReaderMain() {
Brian Silvermanf665d692013-02-17 22:11:39 -0800152 InitNRT();
brians343bc112013-02-10 01:53:46 +0000153
Brian Silverman9f330492015-03-01 17:37:02 -0500154#ifdef AOS_ARCHITECTURE_arm_frc
Austin Schuhc5982cb2014-10-25 18:04:36 -0700155 char folder[128];
156
157 {
158 char dev_name[8];
159 while (!FindDevice(dev_name, sizeof(dev_name))) {
160 LOG(INFO, "Waiting for a device\n");
161 printf("Waiting for a device\n");
162 sleep(5);
163 }
164 snprintf(folder, sizeof(folder), "/media/%s1", dev_name);
165 while (!FoundThumbDrive(folder)) {
166 LOG(INFO, "Waiting for %s\n", folder);
167 printf("Waiting for %s\n", folder);
168 sleep(1);
169 }
170 snprintf(folder, sizeof(folder), "/media/%s1/", dev_name);
171 }
172
173 if (access(folder, F_OK) == -1) {
174#else
Brian Silvermanf665d692013-02-17 22:11:39 -0800175 const char *folder = configuration::GetLoggingDirectory();
brians343bc112013-02-10 01:53:46 +0000176 if (access(folder, R_OK | W_OK) == -1) {
Austin Schuhc5982cb2014-10-25 18:04:36 -0700177#endif
brians2fdfc072013-02-26 05:35:15 +0000178 LOG(FATAL, "folder '%s' does not exist. please create it\n", folder);
brians343bc112013-02-10 01:53:46 +0000179 }
180 LOG(INFO, "logging to folder '%s'\n", folder);
181
brians343bc112013-02-10 01:53:46 +0000182 char *tmp;
Ben Fredricksona04c1752014-03-02 22:54:07 +0000183 AllocateLogName(&tmp, folder);
brians343bc112013-02-10 01:53:46 +0000184 char *tmp2;
185 if (asprintf(&tmp2, "%s/aos_log-current", folder) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700186 PLOG(WARNING, "couldn't create current symlink name");
brians343bc112013-02-10 01:53:46 +0000187 } else {
188 if (unlink(tmp2) == -1 && (errno != EROFS && errno != ENOENT)) {
Brian Silverman01be0002014-05-10 15:44:38 -0700189 LOG(WARNING, "unlink('%s') failed", tmp2);
brians343bc112013-02-10 01:53:46 +0000190 }
191 if (symlink(tmp, tmp2) == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700192 PLOG(WARNING, "symlink('%s', '%s') failed", tmp, tmp2);
brians343bc112013-02-10 01:53:46 +0000193 }
194 free(tmp2);
195 }
196 int fd = open(tmp, O_SYNC | O_APPEND | O_RDWR | O_CREAT,
197 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
198 free(tmp);
199 if (fd == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700200 PLOG(FATAL, "opening file '%s' failed", tmp);
brians343bc112013-02-10 01:53:46 +0000201 }
Brian Silvermanab5ba472014-04-18 15:26:14 -0700202 LogFileWriter writer(fd);
brians343bc112013-02-10 01:53:46 +0000203
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500204 RawQueue *queue = GetLoggingQueue();
205
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500206 ::std::unordered_set<uint32_t> written_type_ids;
207 off_t clear_type_ids_cookie = 0;
208
brians343bc112013-02-10 01:53:46 +0000209 while (true) {
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500210 const LogMessage *const msg =
Brian Silverman18c2c362016-01-02 14:18:32 -0800211 static_cast<const LogMessage *>(queue->ReadMessage(RawQueue::kNonBlock));
212 if (msg == NULL) {
213 // If we've emptied the queue, then wait for a bit before starting to read
214 // again so the queue can buffer up some logs. This avoids lots of context
215 // switches and mutex contention which happens if we're constantly reading
216 // new messages as they come in.
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800217 ::std::this_thread::sleep_for(::std::chrono::milliseconds(100));
Brian Silverman18c2c362016-01-02 14:18:32 -0800218 continue;
219 }
brians343bc112013-02-10 01:53:46 +0000220
Brian Silverman664db1a2014-03-20 17:06:29 -0700221 const size_t raw_output_length =
Brian Silverman88471dc2014-02-15 22:35:42 -0800222 sizeof(LogFileMessageHeader) + msg->name_length + msg->message_length;
Brian Silverman664db1a2014-03-20 17:06:29 -0700223 size_t output_length = raw_output_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800224 if (msg->type == LogMessage::Type::kStruct) {
225 output_length += sizeof(msg->structure.type_id) + sizeof(uint32_t) +
226 msg->structure.string_length;
Brian Silvermanf5ca4d02015-03-01 16:52:24 -0500227 if (writer.ShouldClearSeekableData(&clear_type_ids_cookie,
228 output_length)) {
229 writer.ForceNewPage();
230 written_type_ids.clear();
231 }
232 CheckTypeWritten(msg->structure.type_id, &writer, &written_type_ids);
Brian Silverman664db1a2014-03-20 17:06:29 -0700233 } else if (msg->type == LogMessage::Type::kMatrix) {
234 output_length +=
235 sizeof(msg->matrix.type) + sizeof(uint32_t) + sizeof(uint16_t) +
236 sizeof(uint16_t) + msg->matrix.string_length;
Brian Silvermanab5ba472014-04-18 15:26:14 -0700237 CHECK(MessageType::IsPrimitive(msg->matrix.type));
Brian Silverman88471dc2014-02-15 22:35:42 -0800238 }
Brian Silverman91660632014-03-21 20:52:03 -0700239 LogFileMessageHeader *const output = writer.GetWritePosition(output_length);
brians343bc112013-02-10 01:53:46 +0000240 char *output_strings = reinterpret_cast<char *>(output) + sizeof(*output);
Brian Silverman88471dc2014-02-15 22:35:42 -0800241 output->name_size = msg->name_length;
242 output->message_size = msg->message_length;
brians343bc112013-02-10 01:53:46 +0000243 output->source = msg->source;
Brian Silvermanf665d692013-02-17 22:11:39 -0800244 output->level = msg->level;
245 output->time_sec = msg->seconds;
246 output->time_nsec = msg->nseconds;
247 output->sequence = msg->sequence;
Brian Silverman88471dc2014-02-15 22:35:42 -0800248 memcpy(output_strings, msg->name, msg->name_length);
249
250 switch (msg->type) {
251 case LogMessage::Type::kString:
252 memcpy(output_strings + msg->name_length, msg->message,
253 msg->message_length);
254 output->type = LogFileMessageHeader::MessageType::kString;
255 break;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700256 case LogMessage::Type::kStruct: {
Brian Silverman88471dc2014-02-15 22:35:42 -0800257 char *position = output_strings + msg->name_length;
258
Brian Silverman4dd06242014-04-08 19:10:17 -0700259 memcpy(position, &msg->structure.type_id,
260 sizeof(msg->structure.type_id));
Brian Silverman88471dc2014-02-15 22:35:42 -0800261 position += sizeof(msg->structure.type_id);
262 output->message_size += sizeof(msg->structure.type_id);
263
Brian Silvermana7234c62014-03-24 20:23:25 -0700264 const uint32_t length = msg->structure.string_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800265 memcpy(position, &length, sizeof(length));
266 position += sizeof(length);
Brian Silverman4dd06242014-04-08 19:10:17 -0700267 memcpy(position, msg->structure.serialized,
268 length + msg->message_length);
Brian Silvermana7234c62014-03-24 20:23:25 -0700269 position += length + msg->message_length;
Brian Silverman88471dc2014-02-15 22:35:42 -0800270 output->message_size += sizeof(length) + length;
271
Brian Silverman88471dc2014-02-15 22:35:42 -0800272 output->type = LogFileMessageHeader::MessageType::kStruct;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700273 } break;
274 case LogMessage::Type::kMatrix: {
275 char *position = output_strings + msg->name_length;
276
277 memcpy(position, &msg->matrix.type, sizeof(msg->matrix.type));
278 position += sizeof(msg->matrix.type);
279 output->message_size += sizeof(msg->matrix.type);
280
281 uint32_t length = msg->matrix.string_length;
282 memcpy(position, &length, sizeof(length));
283 position += sizeof(length);
Brian Silverman664db1a2014-03-20 17:06:29 -0700284 output->message_size += sizeof(length);
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700285
286 uint16_t rows = msg->matrix.rows, cols = msg->matrix.cols;
287 memcpy(position, &rows, sizeof(rows));
288 position += sizeof(rows);
289 memcpy(position, &cols, sizeof(cols));
290 position += sizeof(cols);
291 output->message_size += sizeof(rows) + sizeof(cols);
292 CHECK_EQ(msg->message_length,
293 MessageType::Sizeof(msg->matrix.type) * rows * cols);
294
Brian Silverman664db1a2014-03-20 17:06:29 -0700295 memcpy(position, msg->matrix.data, msg->message_length + length);
296 output->message_size += length;
297
298 output->type = LogFileMessageHeader::MessageType::kMatrix;
Brian Silvermanff12c9f2014-03-19 17:53:29 -0700299 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800300 }
301
Brian Silverman664db1a2014-03-20 17:06:29 -0700302 if (output->message_size - msg->message_length !=
303 output_length - raw_output_length) {
304 LOG(FATAL, "%zu != %zu\n", output->message_size - msg->message_length,
305 output_length - raw_output_length);
306 }
307
Brian Silvermanaf221b82013-09-01 13:57:50 -0700308 futex_set(&output->marker);
brians343bc112013-02-10 01:53:46 +0000309
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500310 queue->FreeMessage(msg);
brians343bc112013-02-10 01:53:46 +0000311 }
312
Brian Silvermanf665d692013-02-17 22:11:39 -0800313 Cleanup();
314 return 0;
315}
316
317} // namespace
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800318} // namespace linux_code
Brian Silvermanf665d692013-02-17 22:11:39 -0800319} // namespace logging
320} // namespace aos
321
322int main() {
Brian Silverman14fd0fb2014-01-14 21:42:01 -0800323 return ::aos::logging::linux_code::BinaryLogReaderMain();
brians343bc112013-02-10 01:53:46 +0000324}