Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 1 | |
| 2 | |
| 3 | #include "Collation.h" |
| 4 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 5 | #include "frameworks/base/cmds/statsd/src/atoms.pb.h" |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 6 | |
| 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <getopt.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | using namespace google::protobuf; |
| 16 | using namespace std; |
| 17 | |
| 18 | namespace android { |
| 19 | namespace stats_log_api_gen { |
| 20 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 21 | const int PULL_ATOM_START_ID = 1000; |
| 22 | |
| 23 | int maxPushedAtomId = 2; |
| 24 | |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 25 | using android::os::statsd::Atom; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 26 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Turn lower and camel case into upper case with underscores. |
| 29 | */ |
| 30 | static string |
| 31 | make_constant_name(const string& str) |
| 32 | { |
| 33 | string result; |
| 34 | const int N = str.size(); |
| 35 | bool underscore_next = false; |
| 36 | for (int i=0; i<N; i++) { |
| 37 | char c = str[i]; |
| 38 | if (c >= 'A' && c <= 'Z') { |
| 39 | if (underscore_next) { |
| 40 | result += '_'; |
| 41 | underscore_next = false; |
| 42 | } |
| 43 | } else if (c >= 'a' && c <= 'z') { |
| 44 | c = 'A' + c - 'a'; |
| 45 | underscore_next = true; |
| 46 | } else if (c == '_') { |
| 47 | underscore_next = false; |
| 48 | } |
| 49 | result += c; |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | static const char* |
| 55 | cpp_type_name(java_type_t type) |
| 56 | { |
| 57 | switch (type) { |
| 58 | case JAVA_TYPE_BOOLEAN: |
| 59 | return "bool"; |
| 60 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 61 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 62 | return "int32_t"; |
| 63 | case JAVA_TYPE_LONG: |
| 64 | return "int64_t"; |
| 65 | case JAVA_TYPE_FLOAT: |
| 66 | return "float"; |
| 67 | case JAVA_TYPE_DOUBLE: |
| 68 | return "double"; |
| 69 | case JAVA_TYPE_STRING: |
| 70 | return "char const*"; |
| 71 | default: |
| 72 | return "UNKNOWN"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static const char* |
| 77 | java_type_name(java_type_t type) |
| 78 | { |
| 79 | switch (type) { |
| 80 | case JAVA_TYPE_BOOLEAN: |
| 81 | return "boolean"; |
| 82 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 83 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 84 | return "int"; |
| 85 | case JAVA_TYPE_LONG: |
| 86 | return "long"; |
| 87 | case JAVA_TYPE_FLOAT: |
| 88 | return "float"; |
| 89 | case JAVA_TYPE_DOUBLE: |
| 90 | return "double"; |
| 91 | case JAVA_TYPE_STRING: |
| 92 | return "java.lang.String"; |
| 93 | default: |
| 94 | return "UNKNOWN"; |
| 95 | } |
| 96 | } |
| 97 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 98 | static int write_stats_log_cpp(FILE *out, const Atoms &atoms, |
| 99 | const AtomDecl &attributionDecl) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 100 | // Print prelude |
| 101 | fprintf(out, "// This file is autogenerated\n"); |
| 102 | fprintf(out, "\n"); |
| 103 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 104 | fprintf(out, "#include <exception>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 105 | fprintf(out, "#include <log/log_event_list.h>\n"); |
| 106 | fprintf(out, "#include <log/log.h>\n"); |
| 107 | fprintf(out, "#include <statslog.h>\n"); |
| 108 | fprintf(out, "\n"); |
| 109 | |
| 110 | fprintf(out, "namespace android {\n"); |
| 111 | fprintf(out, "namespace util {\n"); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 112 | fprintf(out, "// the single event tag id for all stats logs\n"); |
| 113 | fprintf(out, "const static int kStatsEventTag = 1937006964;\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 114 | |
| 115 | // Print write methods |
| 116 | fprintf(out, "\n"); |
| 117 | for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 118 | signature != atoms.signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 119 | int argIndex; |
| 120 | |
| 121 | fprintf(out, "void\n"); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 122 | fprintf(out, "stats_write(int32_t code"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 123 | argIndex = 1; |
| 124 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 125 | arg != signature->end(); arg++) { |
| 126 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 127 | for (auto chainField : attributionDecl.fields) { |
| 128 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 129 | fprintf(out, ", const std::vector<%s>& %s", |
| 130 | cpp_type_name(chainField.javaType), |
| 131 | chainField.name.c_str()); |
| 132 | } else { |
| 133 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 134 | cpp_type_name(chainField.javaType), |
| 135 | chainField.name.c_str(), chainField.name.c_str()); |
| 136 | } |
| 137 | } |
| 138 | } else { |
| 139 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 140 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 141 | argIndex++; |
| 142 | } |
| 143 | fprintf(out, ")\n"); |
| 144 | |
| 145 | fprintf(out, "{\n"); |
| 146 | argIndex = 1; |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 147 | fprintf(out, " android_log_event_list event(kStatsEventTag);\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 148 | fprintf(out, " event << code;\n\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 149 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 150 | arg != signature->end(); arg++) { |
| 151 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 152 | for (const auto &chainField : attributionDecl.fields) { |
| 153 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 154 | fprintf(out, " if (%s_length != %s.size()) {\n", |
| 155 | attributionDecl.fields.front().name.c_str(), chainField.name.c_str()); |
| 156 | fprintf(out, " throw std::invalid_argument(\"attribution fields with" |
| 157 | " diff length: %s vs %s\");\n", |
| 158 | attributionDecl.fields.front().name.c_str(), |
| 159 | chainField.name.c_str()); |
| 160 | fprintf(out, " return;\n"); |
| 161 | fprintf(out, " }\n"); |
| 162 | } |
| 163 | } |
| 164 | fprintf(out, "\n event.begin();\n"); |
| 165 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 166 | attributionDecl.fields.front().name.c_str()); |
| 167 | fprintf(out, " event.begin();\n"); |
| 168 | for (const auto &chainField : attributionDecl.fields) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 169 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 170 | fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str()); |
| 171 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 172 | fprintf(out, " } else {\n"); |
| 173 | fprintf(out, " event << \"\";\n"); |
| 174 | fprintf(out, " }\n"); |
| 175 | } else { |
| 176 | fprintf(out, " event << %s[i];\n", chainField.name.c_str()); |
| 177 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 178 | } |
| 179 | fprintf(out, " event.end();\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 180 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 181 | fprintf(out, " event.end();\n\n"); |
| 182 | } else { |
| 183 | if (*arg == JAVA_TYPE_STRING) { |
| 184 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 185 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 186 | fprintf(out, " }\n"); |
| 187 | } |
| 188 | fprintf(out, " event << arg%d;\n", argIndex); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 189 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 190 | argIndex++; |
| 191 | } |
| 192 | |
| 193 | fprintf(out, " event.write(LOG_ID_STATS);\n"); |
| 194 | fprintf(out, "}\n"); |
| 195 | fprintf(out, "\n"); |
| 196 | } |
| 197 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 198 | for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin(); |
| 199 | signature != atoms.non_chained_signatures.end(); signature++) { |
| 200 | int argIndex; |
| 201 | |
| 202 | fprintf(out, "void\n"); |
| 203 | fprintf(out, "stats_write_non_chained(int32_t code"); |
| 204 | argIndex = 1; |
| 205 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 206 | arg != signature->end(); arg++) { |
| 207 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 208 | argIndex++; |
| 209 | } |
| 210 | fprintf(out, ")\n"); |
| 211 | |
| 212 | fprintf(out, "{\n"); |
| 213 | argIndex = 1; |
| 214 | fprintf(out, " android_log_event_list event(kStatsEventTag);\n"); |
| 215 | fprintf(out, " event << code;\n\n"); |
| 216 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 217 | arg != signature->end(); arg++) { |
| 218 | if (argIndex == 1) { |
| 219 | fprintf(out, " event.begin();\n\n"); |
| 220 | fprintf(out, " event.begin();\n"); |
| 221 | } |
| 222 | if (*arg == JAVA_TYPE_STRING) { |
| 223 | fprintf(out, " if (arg%d == NULL) {\n", argIndex); |
| 224 | fprintf(out, " arg%d = \"\";\n", argIndex); |
| 225 | fprintf(out, " }\n"); |
| 226 | } |
| 227 | fprintf(out, " event << arg%d;\n", argIndex); |
| 228 | if (argIndex == 2) { |
| 229 | fprintf(out, " event.end();\n\n"); |
| 230 | fprintf(out, " event.end();\n\n"); |
| 231 | } |
| 232 | argIndex++; |
| 233 | } |
| 234 | |
| 235 | fprintf(out, " event.write(LOG_ID_STATS);\n"); |
| 236 | fprintf(out, "}\n"); |
| 237 | fprintf(out, "\n"); |
| 238 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 239 | // Print footer |
| 240 | fprintf(out, "\n"); |
| 241 | fprintf(out, "} // namespace util\n"); |
| 242 | fprintf(out, "} // namespace android\n"); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 247 | void build_non_chained_decl_map(const Atoms& atoms, |
| 248 | std::map<int, set<AtomDecl>::const_iterator>* decl_map){ |
| 249 | for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin(); |
| 250 | atom != atoms.non_chained_decls.end(); atom++) { |
| 251 | decl_map->insert(std::make_pair(atom->code, atom)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static void write_cpp_usage( |
| 256 | FILE* out, const string& method_name, const string& atom_code_name, |
| 257 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 258 | fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str()); |
| 259 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 260 | field != atom.fields.end(); field++) { |
| 261 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 262 | for (auto chainField : attributionDecl.fields) { |
| 263 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 264 | fprintf(out, ", const std::vector<%s>& %s", |
| 265 | cpp_type_name(chainField.javaType), |
| 266 | chainField.name.c_str()); |
| 267 | } else { |
| 268 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 269 | cpp_type_name(chainField.javaType), |
| 270 | chainField.name.c_str(), chainField.name.c_str()); |
| 271 | } |
| 272 | } |
| 273 | } else { |
| 274 | fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str()); |
| 275 | } |
| 276 | } |
| 277 | fprintf(out, ");\n"); |
| 278 | } |
| 279 | |
| 280 | static void write_cpp_method_header( |
| 281 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 282 | const AtomDecl &attributionDecl) { |
| 283 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 284 | signature != signatures.end(); signature++) { |
| 285 | fprintf(out, "void %s(int32_t code ", method_name.c_str()); |
| 286 | int argIndex = 1; |
| 287 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 288 | arg != signature->end(); arg++) { |
| 289 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 290 | for (auto chainField : attributionDecl.fields) { |
| 291 | if (chainField.javaType == JAVA_TYPE_STRING) { |
| 292 | fprintf(out, ", const std::vector<%s>& %s", |
| 293 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 294 | } else { |
| 295 | fprintf(out, ", const %s* %s, size_t %s_length", |
| 296 | cpp_type_name(chainField.javaType), |
| 297 | chainField.name.c_str(), chainField.name.c_str()); |
| 298 | } |
| 299 | } |
| 300 | } else { |
| 301 | fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex); |
| 302 | } |
| 303 | argIndex++; |
| 304 | } |
| 305 | fprintf(out, ");\n"); |
| 306 | |
| 307 | } |
| 308 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 309 | |
| 310 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 311 | write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 312 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 313 | // Print prelude |
| 314 | fprintf(out, "// This file is autogenerated\n"); |
| 315 | fprintf(out, "\n"); |
| 316 | fprintf(out, "#pragma once\n"); |
| 317 | fprintf(out, "\n"); |
| 318 | fprintf(out, "#include <stdint.h>\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 319 | fprintf(out, "#include <vector>\n"); |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 320 | fprintf(out, "#include <set>\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 321 | fprintf(out, "\n"); |
| 322 | |
| 323 | fprintf(out, "namespace android {\n"); |
| 324 | fprintf(out, "namespace util {\n"); |
| 325 | fprintf(out, "\n"); |
| 326 | fprintf(out, "/*\n"); |
| 327 | fprintf(out, " * API For logging statistics events.\n"); |
| 328 | fprintf(out, " */\n"); |
| 329 | fprintf(out, "\n"); |
| 330 | fprintf(out, "/**\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 331 | fprintf(out, " * Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 332 | fprintf(out, " */\n"); |
| 333 | fprintf(out, "enum {\n"); |
| 334 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 335 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 336 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 337 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 338 | size_t i = 0; |
| 339 | // Print constants |
| 340 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 341 | atom != atoms.decls.end(); atom++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 342 | string constant = make_constant_name(atom->name); |
| 343 | fprintf(out, "\n"); |
| 344 | fprintf(out, " /**\n"); |
| 345 | fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 346 | write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl); |
| 347 | |
| 348 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 349 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 350 | write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second, |
| 351 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 352 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 353 | fprintf(out, " */\n"); |
| 354 | char const* const comma = (i == atoms.decls.size() - 1) ? "" : ","; |
| 355 | fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 356 | if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) { |
| 357 | maxPushedAtomId = atom->code; |
| 358 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 359 | i++; |
| 360 | } |
| 361 | fprintf(out, "\n"); |
| 362 | fprintf(out, "};\n"); |
| 363 | fprintf(out, "\n"); |
| 364 | |
Yangster-mac | 6898580 | 2018-01-21 10:05:09 -0800 | [diff] [blame] | 365 | fprintf(out, "const static std::set<int> kAtomsWithUidField = {\n"); |
| 366 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 367 | atom != atoms.decls.end(); atom++) { |
| 368 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 369 | field != atom->fields.end(); field++) { |
| 370 | if (field->name == "uid") { |
| 371 | string constant = make_constant_name(atom->name); |
| 372 | fprintf(out, " %s,\n", constant.c_str()); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | fprintf(out, "};\n"); |
| 378 | fprintf(out, "\n"); |
| 379 | |
| 380 | fprintf(out, "const static std::set<int> kAtomsWithAttributionChain = {\n"); |
| 381 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 382 | atom != atoms.decls.end(); atom++) { |
| 383 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| 384 | field != atom->fields.end(); field++) { |
| 385 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 386 | string constant = make_constant_name(atom->name); |
| 387 | fprintf(out, " %s,\n", constant.c_str()); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | fprintf(out, "};\n"); |
| 393 | fprintf(out, "\n"); |
| 394 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 395 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 396 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 397 | // Print write methods |
| 398 | fprintf(out, "//\n"); |
| 399 | fprintf(out, "// Write methods\n"); |
| 400 | fprintf(out, "//\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 401 | write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl); |
| 402 | |
| 403 | fprintf(out, "//\n"); |
| 404 | fprintf(out, "// Write flattened methods\n"); |
| 405 | fprintf(out, "//\n"); |
| 406 | write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures, |
| 407 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 408 | |
| 409 | fprintf(out, "\n"); |
| 410 | fprintf(out, "} // namespace util\n"); |
| 411 | fprintf(out, "} // namespace android\n"); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 416 | static void write_java_usage( |
| 417 | FILE* out, const string& method_name, const string& atom_code_name, |
| 418 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 419 | fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", |
| 420 | method_name.c_str(), atom_code_name.c_str()); |
| 421 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 422 | field != atom.fields.end(); field++) { |
| 423 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 424 | for (auto chainField : attributionDecl.fields) { |
| 425 | fprintf(out, ", %s[] %s", |
| 426 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 427 | } |
| 428 | } else { |
| 429 | fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str()); |
| 430 | } |
| 431 | } |
| 432 | fprintf(out, ");\n"); |
| 433 | } |
| 434 | |
| 435 | static void write_java_method( |
| 436 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 437 | const AtomDecl &attributionDecl) { |
| 438 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 439 | signature != signatures.end(); signature++) { |
| 440 | fprintf(out, " public static native void %s(int code", method_name.c_str()); |
| 441 | int argIndex = 1; |
| 442 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 443 | arg != signature->end(); arg++) { |
| 444 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 445 | for (auto chainField : attributionDecl.fields) { |
| 446 | fprintf(out, ", %s[] %s", |
| 447 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 448 | } |
| 449 | } else { |
| 450 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 451 | } |
| 452 | argIndex++; |
| 453 | } |
| 454 | fprintf(out, ");\n"); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 459 | static int |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 460 | write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 461 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 462 | // Print prelude |
| 463 | fprintf(out, "// This file is autogenerated\n"); |
| 464 | fprintf(out, "\n"); |
| 465 | fprintf(out, "package android.util;\n"); |
| 466 | fprintf(out, "\n"); |
| 467 | fprintf(out, "\n"); |
| 468 | fprintf(out, "/**\n"); |
| 469 | fprintf(out, " * API For logging statistics events.\n"); |
| 470 | fprintf(out, " * @hide\n"); |
| 471 | fprintf(out, " */\n"); |
David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 472 | fprintf(out, "public class StatsLogInternal {\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 473 | fprintf(out, " // Constants for atom codes.\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 474 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 475 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 476 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 477 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 478 | // Print constants for the atom codes. |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 479 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 480 | atom != atoms.decls.end(); atom++) { |
| 481 | string constant = make_constant_name(atom->name); |
| 482 | fprintf(out, "\n"); |
| 483 | fprintf(out, " /**\n"); |
| 484 | fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str()); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 485 | write_java_usage(out, "write", constant, *atom, attributionDecl); |
| 486 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 487 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 488 | write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second, |
| 489 | attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 490 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 491 | fprintf(out, " */\n"); |
| 492 | fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code); |
| 493 | } |
| 494 | fprintf(out, "\n"); |
| 495 | |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 496 | // Print constants for the enum values. |
| 497 | fprintf(out, " // Constants for enum values.\n\n"); |
| 498 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 499 | atom != atoms.decls.end(); atom++) { |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 500 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 501 | field != atom->fields.end(); field++) { |
| 502 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 503 | fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), |
| 504 | field->name.c_str()); |
| 505 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 506 | value != field->enumValues.end(); value++) { |
| 507 | fprintf(out, " public static final int %s__%s__%s = %d;\n", |
| 508 | make_constant_name(atom->message).c_str(), |
| 509 | make_constant_name(field->name).c_str(), |
| 510 | make_constant_name(value->second).c_str(), |
| 511 | value->first); |
| 512 | } |
| 513 | fprintf(out, "\n"); |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 514 | } |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 518 | // Print write methods |
| 519 | fprintf(out, " // Write methods\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 520 | write_java_method(out, "write", atoms.signatures, attributionDecl); |
| 521 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 522 | |
| 523 | fprintf(out, "}\n"); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static const char* |
| 529 | jni_type_name(java_type_t type) |
| 530 | { |
| 531 | switch (type) { |
| 532 | case JAVA_TYPE_BOOLEAN: |
| 533 | return "jboolean"; |
| 534 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 535 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 536 | return "jint"; |
| 537 | case JAVA_TYPE_LONG: |
| 538 | return "jlong"; |
| 539 | case JAVA_TYPE_FLOAT: |
| 540 | return "jfloat"; |
| 541 | case JAVA_TYPE_DOUBLE: |
| 542 | return "jdouble"; |
| 543 | case JAVA_TYPE_STRING: |
| 544 | return "jstring"; |
| 545 | default: |
| 546 | return "UNKNOWN"; |
| 547 | } |
| 548 | } |
| 549 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 550 | static const char* |
| 551 | jni_array_type_name(java_type_t type) |
| 552 | { |
| 553 | switch (type) { |
| 554 | case JAVA_TYPE_INT: |
| 555 | return "jintArray"; |
| 556 | case JAVA_TYPE_STRING: |
| 557 | return "jobjectArray"; |
| 558 | default: |
| 559 | return "UNKNOWN"; |
| 560 | } |
| 561 | } |
| 562 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 563 | static string |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 564 | jni_function_name(const string& method_name, const vector<java_type_t>& signature) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 565 | { |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 566 | string result("StatsLog_" + method_name); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 567 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 568 | arg != signature.end(); arg++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 569 | switch (*arg) { |
| 570 | case JAVA_TYPE_BOOLEAN: |
| 571 | result += "_boolean"; |
| 572 | break; |
| 573 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 574 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 575 | result += "_int"; |
| 576 | break; |
| 577 | case JAVA_TYPE_LONG: |
| 578 | result += "_long"; |
| 579 | break; |
| 580 | case JAVA_TYPE_FLOAT: |
| 581 | result += "_float"; |
| 582 | break; |
| 583 | case JAVA_TYPE_DOUBLE: |
| 584 | result += "_double"; |
| 585 | break; |
| 586 | case JAVA_TYPE_STRING: |
| 587 | result += "_String"; |
| 588 | break; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 589 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 590 | result += "_AttributionChain"; |
| 591 | break; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 592 | default: |
| 593 | result += "_UNKNOWN"; |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | return result; |
| 598 | } |
| 599 | |
| 600 | static const char* |
| 601 | java_type_signature(java_type_t type) |
| 602 | { |
| 603 | switch (type) { |
| 604 | case JAVA_TYPE_BOOLEAN: |
| 605 | return "Z"; |
| 606 | case JAVA_TYPE_INT: |
Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 607 | case JAVA_TYPE_ENUM: |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 608 | return "I"; |
| 609 | case JAVA_TYPE_LONG: |
| 610 | return "J"; |
| 611 | case JAVA_TYPE_FLOAT: |
| 612 | return "F"; |
| 613 | case JAVA_TYPE_DOUBLE: |
| 614 | return "D"; |
| 615 | case JAVA_TYPE_STRING: |
| 616 | return "Ljava/lang/String;"; |
| 617 | default: |
| 618 | return "UNKNOWN"; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static string |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 623 | jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 624 | { |
| 625 | string result("(I"); |
| 626 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 627 | arg != signature.end(); arg++) { |
| 628 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 629 | for (auto chainField : attributionDecl.fields) { |
| 630 | result += "["; |
| 631 | result += java_type_signature(chainField.javaType); |
| 632 | } |
| 633 | } else { |
| 634 | result += java_type_signature(*arg); |
| 635 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 636 | } |
| 637 | result += ")V"; |
| 638 | return result; |
| 639 | } |
| 640 | |
| 641 | static int |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 642 | write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name, |
| 643 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 644 | { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 645 | // Print write methods |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 646 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 647 | signature != signatures.end(); signature++) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 648 | int argIndex; |
| 649 | |
| 650 | fprintf(out, "static void\n"); |
| 651 | fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code", |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 652 | jni_function_name(java_method_name, *signature).c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 653 | argIndex = 1; |
| 654 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 655 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 656 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 657 | for (auto chainField : attributionDecl.fields) { |
| 658 | fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType), |
| 659 | chainField.name.c_str()); |
| 660 | } |
| 661 | } else { |
| 662 | fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex); |
| 663 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 664 | argIndex++; |
| 665 | } |
| 666 | fprintf(out, ")\n"); |
| 667 | |
| 668 | fprintf(out, "{\n"); |
| 669 | |
| 670 | // Prepare strings |
| 671 | argIndex = 1; |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 672 | bool hadStringOrChain = false; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 673 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 674 | arg != signature->end(); arg++) { |
| 675 | if (*arg == JAVA_TYPE_STRING) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 676 | hadStringOrChain = true; |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 677 | fprintf(out, " const char* str%d;\n", argIndex); |
| 678 | fprintf(out, " if (arg%d != NULL) {\n", argIndex); |
| 679 | fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n", |
| 680 | argIndex, argIndex); |
| 681 | fprintf(out, " } else {\n"); |
| 682 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 683 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 684 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 685 | hadStringOrChain = true; |
| 686 | for (auto chainField : attributionDecl.fields) { |
| 687 | fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n", |
| 688 | chainField.name.c_str(), chainField.name.c_str()); |
| 689 | if (chainField.name != attributionDecl.fields.front().name) { |
| 690 | fprintf(out, " if (%s_length != %s_length) {\n", |
| 691 | chainField.name.c_str(), |
| 692 | attributionDecl.fields.front().name.c_str()); |
| 693 | fprintf(out, " jniThrowException(env, " |
| 694 | "\"java/lang/IllegalArgumentException\", " |
| 695 | "\"invalid attribution field(%s) length.\");\n", |
| 696 | chainField.name.c_str()); |
| 697 | fprintf(out, " return;\n"); |
| 698 | fprintf(out, " }\n"); |
| 699 | } |
| 700 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 701 | fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n", |
| 702 | chainField.name.c_str(), chainField.name.c_str()); |
| 703 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 704 | fprintf(out, " std::vector<%s> %s_vec;\n", |
| 705 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 706 | fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n", |
| 707 | chainField.name.c_str()); |
| 708 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 709 | chainField.name.c_str()); |
| 710 | fprintf(out, " jstring jstr = " |
| 711 | "(jstring)env->GetObjectArrayElement(%s, i);\n", |
| 712 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 713 | fprintf(out, " if (jstr == NULL) {\n"); |
| 714 | fprintf(out, " %s_vec.push_back(NULL);\n", |
| 715 | chainField.name.c_str()); |
| 716 | fprintf(out, " } else {\n"); |
| 717 | fprintf(out, " ScopedUtfChars* scoped_%s = " |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 718 | "new ScopedUtfChars(env, jstr);\n", |
| 719 | chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 720 | fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 721 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 722 | fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 723 | chainField.name.c_str(), chainField.name.c_str()); |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 724 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 725 | fprintf(out, " }\n"); |
| 726 | } |
| 727 | fprintf(out, "\n"); |
| 728 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 729 | } |
| 730 | argIndex++; |
| 731 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 732 | // Emit this to quiet the unused parameter warning if there were no strings or attribution |
| 733 | // chains. |
| 734 | if (!hadStringOrChain) { |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 735 | fprintf(out, " (void)env;\n"); |
| 736 | } |
| 737 | |
| 738 | // stats_write call |
| 739 | argIndex = 1; |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 740 | fprintf(out, " android::util::%s(code", cpp_method_name.c_str()); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 741 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 742 | arg != signature->end(); arg++) { |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 743 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 744 | for (auto chainField : attributionDecl.fields) { |
| 745 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 746 | fprintf(out, ", (const %s*)%s_array, %s_length", |
| 747 | cpp_type_name(chainField.javaType), |
| 748 | chainField.name.c_str(), chainField.name.c_str()); |
| 749 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 750 | fprintf(out, ", %s_vec", chainField.name.c_str()); |
| 751 | } |
| 752 | } |
| 753 | } else { |
| 754 | const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg"; |
| 755 | fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex); |
| 756 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 757 | argIndex++; |
| 758 | } |
| 759 | fprintf(out, ");\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 760 | fprintf(out, "\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 761 | |
| 762 | // Clean up strings |
| 763 | argIndex = 1; |
| 764 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 765 | arg != signature->end(); arg++) { |
| 766 | if (*arg == JAVA_TYPE_STRING) { |
| 767 | fprintf(out, " if (str%d != NULL) {\n", argIndex); |
| 768 | fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n", |
| 769 | argIndex, argIndex); |
| 770 | fprintf(out, " }\n"); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 771 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 772 | for (auto chainField : attributionDecl.fields) { |
| 773 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 774 | fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", |
| 775 | chainField.name.c_str(), chainField.name.c_str()); |
| 776 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 777 | fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n", |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 778 | chainField.name.c_str()); |
| 779 | fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); |
| 780 | fprintf(out, " }\n"); |
| 781 | } |
| 782 | } |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 783 | } |
| 784 | argIndex++; |
| 785 | } |
| 786 | |
| 787 | fprintf(out, "}\n"); |
| 788 | fprintf(out, "\n"); |
| 789 | } |
| 790 | |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | void write_jni_registration(FILE* out, const string& java_method_name, |
| 796 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) { |
| 797 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 798 | signature != signatures.end(); signature++) { |
| 799 | fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n", |
| 800 | java_method_name.c_str(), |
| 801 | jni_function_signature(*signature, attributionDecl).c_str(), |
| 802 | jni_function_name(java_method_name, *signature).c_str()); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static int |
| 807 | write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| 808 | { |
| 809 | // Print prelude |
| 810 | fprintf(out, "// This file is autogenerated\n"); |
| 811 | fprintf(out, "\n"); |
| 812 | |
| 813 | fprintf(out, "#include <statslog.h>\n"); |
| 814 | fprintf(out, "\n"); |
| 815 | fprintf(out, "#include <nativehelper/JNIHelp.h>\n"); |
| 816 | fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n"); |
| 817 | fprintf(out, "#include <utils/Vector.h>\n"); |
| 818 | fprintf(out, "#include \"core_jni_helpers.h\"\n"); |
| 819 | fprintf(out, "#include \"jni.h\"\n"); |
| 820 | fprintf(out, "\n"); |
| 821 | fprintf(out, "#define UNUSED __attribute__((__unused__))\n"); |
| 822 | fprintf(out, "\n"); |
| 823 | |
| 824 | fprintf(out, "namespace android {\n"); |
| 825 | fprintf(out, "\n"); |
| 826 | |
| 827 | write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl); |
| 828 | write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained", |
| 829 | atoms.non_chained_signatures, attributionDecl); |
| 830 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 831 | // Print registration function table |
| 832 | fprintf(out, "/*\n"); |
| 833 | fprintf(out, " * JNI registration.\n"); |
| 834 | fprintf(out, " */\n"); |
| 835 | fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n"); |
Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame] | 836 | write_jni_registration(out, "write", atoms.signatures, attributionDecl); |
| 837 | write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 838 | fprintf(out, "};\n"); |
| 839 | fprintf(out, "\n"); |
| 840 | |
| 841 | // Print registration function |
| 842 | fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n"); |
| 843 | fprintf(out, " return RegisterMethodsOrDie(\n"); |
| 844 | fprintf(out, " env,\n"); |
| 845 | fprintf(out, " \"android/util/StatsLog\",\n"); |
| 846 | fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n"); |
| 847 | fprintf(out, "}\n"); |
| 848 | |
| 849 | fprintf(out, "\n"); |
| 850 | fprintf(out, "} // namespace android\n"); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 854 | static void |
| 855 | print_usage() |
| 856 | { |
| 857 | fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n"); |
| 858 | fprintf(stderr, "\n"); |
| 859 | fprintf(stderr, "OPTIONS\n"); |
| 860 | fprintf(stderr, " --cpp FILENAME the header file to output\n"); |
| 861 | fprintf(stderr, " --header FILENAME the cpp file to output\n"); |
| 862 | fprintf(stderr, " --help this message\n"); |
| 863 | fprintf(stderr, " --java FILENAME the java file to output\n"); |
| 864 | fprintf(stderr, " --jni FILENAME the jni file to output\n"); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * Do the argument parsing and execute the tasks. |
| 869 | */ |
| 870 | static int |
| 871 | run(int argc, char const*const* argv) |
| 872 | { |
| 873 | string cppFilename; |
| 874 | string headerFilename; |
| 875 | string javaFilename; |
| 876 | string jniFilename; |
| 877 | |
| 878 | int index = 1; |
| 879 | while (index < argc) { |
| 880 | if (0 == strcmp("--help", argv[index])) { |
| 881 | print_usage(); |
| 882 | return 0; |
| 883 | } else if (0 == strcmp("--cpp", argv[index])) { |
| 884 | index++; |
| 885 | if (index >= argc) { |
| 886 | print_usage(); |
| 887 | return 1; |
| 888 | } |
| 889 | cppFilename = argv[index]; |
| 890 | } else if (0 == strcmp("--header", argv[index])) { |
| 891 | index++; |
| 892 | if (index >= argc) { |
| 893 | print_usage(); |
| 894 | return 1; |
| 895 | } |
| 896 | headerFilename = argv[index]; |
| 897 | } else if (0 == strcmp("--java", argv[index])) { |
| 898 | index++; |
| 899 | if (index >= argc) { |
| 900 | print_usage(); |
| 901 | return 1; |
| 902 | } |
| 903 | javaFilename = argv[index]; |
| 904 | } else if (0 == strcmp("--jni", argv[index])) { |
| 905 | index++; |
| 906 | if (index >= argc) { |
| 907 | print_usage(); |
| 908 | return 1; |
| 909 | } |
| 910 | jniFilename = argv[index]; |
| 911 | } |
| 912 | index++; |
| 913 | } |
| 914 | |
| 915 | if (cppFilename.size() == 0 |
| 916 | && headerFilename.size() == 0 |
| 917 | && javaFilename.size() == 0 |
| 918 | && jniFilename.size() == 0) { |
| 919 | print_usage(); |
| 920 | return 1; |
| 921 | } |
| 922 | |
| 923 | // Collate the parameters |
| 924 | Atoms atoms; |
Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 925 | int errorCount = collate_atoms(Atom::descriptor(), &atoms); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 926 | if (errorCount != 0) { |
| 927 | return 1; |
| 928 | } |
| 929 | |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 930 | AtomDecl attributionDecl; |
| 931 | vector<java_type_t> attributionSignature; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 932 | collate_atom(android::os::statsd::AttributionNode::descriptor(), |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 933 | &attributionDecl, &attributionSignature); |
| 934 | |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 935 | // Write the .cpp file |
| 936 | if (cppFilename.size() != 0) { |
| 937 | FILE* out = fopen(cppFilename.c_str(), "w"); |
| 938 | if (out == NULL) { |
| 939 | fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str()); |
| 940 | return 1; |
| 941 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 942 | errorCount = android::stats_log_api_gen::write_stats_log_cpp( |
| 943 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 944 | fclose(out); |
| 945 | } |
| 946 | |
| 947 | // Write the .h file |
| 948 | if (headerFilename.size() != 0) { |
| 949 | FILE* out = fopen(headerFilename.c_str(), "w"); |
| 950 | if (out == NULL) { |
| 951 | fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str()); |
| 952 | return 1; |
| 953 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 954 | errorCount = android::stats_log_api_gen::write_stats_log_header( |
| 955 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 956 | fclose(out); |
| 957 | } |
| 958 | |
| 959 | // Write the .java file |
| 960 | if (javaFilename.size() != 0) { |
| 961 | FILE* out = fopen(javaFilename.c_str(), "w"); |
| 962 | if (out == NULL) { |
| 963 | fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str()); |
| 964 | return 1; |
| 965 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 966 | errorCount = android::stats_log_api_gen::write_stats_log_java( |
| 967 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 968 | fclose(out); |
| 969 | } |
| 970 | |
| 971 | // Write the jni file |
| 972 | if (jniFilename.size() != 0) { |
| 973 | FILE* out = fopen(jniFilename.c_str(), "w"); |
| 974 | if (out == NULL) { |
| 975 | fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str()); |
| 976 | return 1; |
| 977 | } |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 978 | errorCount = android::stats_log_api_gen::write_stats_log_jni( |
| 979 | out, atoms, attributionDecl); |
Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 980 | fclose(out); |
| 981 | } |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * Main. |
| 991 | */ |
| 992 | int |
| 993 | main(int argc, char const*const* argv) |
| 994 | { |
| 995 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 996 | |
| 997 | return android::stats_log_api_gen::run(argc, argv); |
Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 998 | } |