Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 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 | |
Hector Dearman | 2193ff4 | 2017-11-08 14:15:19 +0000 | [diff] [blame] | 17 | #include "ftrace_reader/format_parser.h" |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <iosfwd> |
| 22 | #include <iostream> |
| 23 | #include <memory> |
| 24 | #include <vector> |
| 25 | |
Hector Dearman | 2193ff4 | 2017-11-08 14:15:19 +0000 | [diff] [blame] | 26 | #include "ftrace_reader/ftrace_to_proto.h" |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 27 | #include "perfetto_base/utils.h" |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 28 | |
| 29 | namespace perfetto { |
| 30 | namespace { |
| 31 | |
| 32 | #define MAX_FIELD_LENGTH 127 |
| 33 | #define STRINGIFY(x) STRINGIFY2(x) |
| 34 | #define STRINGIFY2(x) #x |
| 35 | |
| 36 | const char* kCommonFieldPrefix = "common_"; |
| 37 | |
Hector Dearman | b9b41a5 | 2017-11-27 11:52:37 +0000 | [diff] [blame] | 38 | bool IsCommonFieldName(std::string name) { |
| 39 | return name.compare(0, strlen(kCommonFieldPrefix), kCommonFieldPrefix) == 0; |
| 40 | } |
| 41 | |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | bool ParseFtraceEvent(const std::string& input, FtraceEvent* output) { |
Hector Dearman | 798bcdf | 2017-10-31 16:36:46 +0000 | [diff] [blame] | 45 | std::unique_ptr<char[], base::FreeDeleter> input_copy(strdup(input.c_str())); |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 46 | char* s = input_copy.get(); |
| 47 | |
| 48 | char buffer[MAX_FIELD_LENGTH + 1]; |
| 49 | |
| 50 | bool has_id = false; |
| 51 | bool has_name = false; |
| 52 | |
| 53 | int id = 0; |
| 54 | std::string name; |
Hector Dearman | b9b41a5 | 2017-11-27 11:52:37 +0000 | [diff] [blame] | 55 | std::vector<FtraceEvent::Field> common_fields; |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 56 | std::vector<FtraceEvent::Field> fields; |
| 57 | |
| 58 | for (char* line = strtok(s, "\n"); line; line = strtok(nullptr, "\n")) { |
| 59 | if (!has_id && sscanf(line, "ID: %d", &id) == 1) { |
| 60 | has_id = true; |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | if (!has_name && |
| 65 | sscanf(line, "name: %" STRINGIFY(MAX_FIELD_LENGTH) "s", buffer) == 1) { |
| 66 | name = std::string(buffer); |
| 67 | has_name = true; |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | if (strcmp("format:", line) == 0) { |
| 72 | continue; |
| 73 | } |
| 74 | |
Hector Dearman | 7d8bbfa | 2017-11-09 11:36:04 +0000 | [diff] [blame] | 75 | size_t offset = 0; |
| 76 | size_t size = 0; |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 77 | int is_signed = 0; |
Hector Dearman | 7d8bbfa | 2017-11-09 11:36:04 +0000 | [diff] [blame] | 78 | if (sscanf(line, |
| 79 | "\tfield:%" STRINGIFY(MAX_FIELD_LENGTH) "[^;];\toffset: " |
| 80 | "%zu;\tsize: " |
| 81 | "%zu;\tsigned: %d;", |
| 82 | buffer, &offset, &size, &is_signed) == 4) { |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 83 | std::string type_and_name(buffer); |
| 84 | |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 85 | FtraceEvent::Field field{type_and_name, offset, size, is_signed == 1}; |
Hector Dearman | b9b41a5 | 2017-11-27 11:52:37 +0000 | [diff] [blame] | 86 | |
| 87 | if (IsCommonFieldName(GetNameFromTypeAndName(type_and_name))) { |
| 88 | common_fields.push_back(field); |
| 89 | } else { |
| 90 | fields.push_back(field); |
| 91 | } |
| 92 | |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 93 | continue; |
| 94 | } |
| 95 | |
| 96 | if (strncmp(line, "print fmt:", 10) == 0) { |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (output) |
| 101 | fprintf(stderr, "Cannot parse line: \"%s\"\n", line); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | if (!has_id || !has_name || fields.size() == 0) { |
| 106 | if (output) |
| 107 | fprintf(stderr, "Could not parse format file: %s.\n", |
| 108 | !has_id ? "no ID found" |
| 109 | : !has_name ? "no name found" : "no fields found"); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (!output) |
| 114 | return true; |
| 115 | |
| 116 | output->id = id; |
| 117 | output->name = name; |
| 118 | output->fields = std::move(fields); |
Hector Dearman | b9b41a5 | 2017-11-27 11:52:37 +0000 | [diff] [blame] | 119 | output->common_fields = std::move(common_fields); |
Hector Dearman | be61adf | 2017-10-18 15:58:46 +0100 | [diff] [blame] | 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | ::std::ostream& operator<<(::std::ostream& os, |
| 125 | const FtraceEvent::Field& field) { |
| 126 | PrintTo(field, &os); |
| 127 | return os; |
| 128 | } |
| 129 | |
| 130 | // Allow gtest to pretty print FtraceEvent::Field. |
| 131 | void PrintTo(const FtraceEvent::Field& field, ::std::ostream* os) { |
| 132 | *os << "FtraceEvent::Field(" << field.type_and_name << ", " << field.offset |
| 133 | << ", " << field.size << ", " << field.is_signed << ")"; |
| 134 | } |
| 135 | |
| 136 | } // namespace perfetto |