| 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"); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 320 | fprintf(out, "\n"); |
| 321 | |
| 322 | fprintf(out, "namespace android {\n"); |
| 323 | fprintf(out, "namespace util {\n"); |
| 324 | fprintf(out, "\n"); |
| 325 | fprintf(out, "/*\n"); |
| 326 | fprintf(out, " * API For logging statistics events.\n"); |
| 327 | fprintf(out, " */\n"); |
| 328 | fprintf(out, "\n"); |
| 329 | fprintf(out, "/**\n"); |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 330 | fprintf(out, " * Constants for atom codes.\n"); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 331 | fprintf(out, " */\n"); |
| 332 | fprintf(out, "enum {\n"); |
| 333 | |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 334 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 335 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 336 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 337 | size_t i = 0; |
| 338 | // Print constants |
| 339 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 340 | atom != atoms.decls.end(); atom++) { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 341 | string constant = make_constant_name(atom->name); |
| 342 | fprintf(out, "\n"); |
| 343 | fprintf(out, " /**\n"); |
| 344 | 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^] | 345 | write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl); |
| 346 | |
| 347 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 348 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 349 | write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second, |
| 350 | attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 351 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 352 | fprintf(out, " */\n"); |
| 353 | char const* const comma = (i == atoms.decls.size() - 1) ? "" : ","; |
| 354 | fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma); |
| Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 355 | if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) { |
| 356 | maxPushedAtomId = atom->code; |
| 357 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 358 | i++; |
| 359 | } |
| 360 | fprintf(out, "\n"); |
| 361 | fprintf(out, "};\n"); |
| 362 | fprintf(out, "\n"); |
| 363 | |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 364 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId); |
| Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 365 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 366 | // Print write methods |
| 367 | fprintf(out, "//\n"); |
| 368 | fprintf(out, "// Write methods\n"); |
| 369 | fprintf(out, "//\n"); |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 370 | write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl); |
| 371 | |
| 372 | fprintf(out, "//\n"); |
| 373 | fprintf(out, "// Write flattened methods\n"); |
| 374 | fprintf(out, "//\n"); |
| 375 | write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures, |
| 376 | attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 377 | |
| 378 | fprintf(out, "\n"); |
| 379 | fprintf(out, "} // namespace util\n"); |
| 380 | fprintf(out, "} // namespace android\n"); |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 385 | static void write_java_usage( |
| 386 | FILE* out, const string& method_name, const string& atom_code_name, |
| 387 | const AtomDecl& atom, const AtomDecl &attributionDecl) { |
| 388 | fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s", |
| 389 | method_name.c_str(), atom_code_name.c_str()); |
| 390 | for (vector<AtomField>::const_iterator field = atom.fields.begin(); |
| 391 | field != atom.fields.end(); field++) { |
| 392 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 393 | for (auto chainField : attributionDecl.fields) { |
| 394 | fprintf(out, ", %s[] %s", |
| 395 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 396 | } |
| 397 | } else { |
| 398 | fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str()); |
| 399 | } |
| 400 | } |
| 401 | fprintf(out, ");\n"); |
| 402 | } |
| 403 | |
| 404 | static void write_java_method( |
| 405 | FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures, |
| 406 | const AtomDecl &attributionDecl) { |
| 407 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 408 | signature != signatures.end(); signature++) { |
| 409 | fprintf(out, " public static native void %s(int code", method_name.c_str()); |
| 410 | int argIndex = 1; |
| 411 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 412 | arg != signature->end(); arg++) { |
| 413 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 414 | for (auto chainField : attributionDecl.fields) { |
| 415 | fprintf(out, ", %s[] %s", |
| 416 | java_type_name(chainField.javaType), chainField.name.c_str()); |
| 417 | } |
| 418 | } else { |
| 419 | fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex); |
| 420 | } |
| 421 | argIndex++; |
| 422 | } |
| 423 | fprintf(out, ");\n"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 428 | static int |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 429 | write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 430 | { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 431 | // Print prelude |
| 432 | fprintf(out, "// This file is autogenerated\n"); |
| 433 | fprintf(out, "\n"); |
| 434 | fprintf(out, "package android.util;\n"); |
| 435 | fprintf(out, "\n"); |
| 436 | fprintf(out, "\n"); |
| 437 | fprintf(out, "/**\n"); |
| 438 | fprintf(out, " * API For logging statistics events.\n"); |
| 439 | fprintf(out, " * @hide\n"); |
| 440 | fprintf(out, " */\n"); |
| David Chen | 0a368b2 | 2017-12-06 16:28:16 -0800 | [diff] [blame] | 441 | fprintf(out, "public class StatsLogInternal {\n"); |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 442 | fprintf(out, " // Constants for atom codes.\n"); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 443 | |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 444 | std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map; |
| 445 | build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map); |
| 446 | |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 447 | // Print constants for the atom codes. |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 448 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| 449 | atom != atoms.decls.end(); atom++) { |
| 450 | string constant = make_constant_name(atom->name); |
| 451 | fprintf(out, "\n"); |
| 452 | fprintf(out, " /**\n"); |
| 453 | 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^] | 454 | write_java_usage(out, "write", constant, *atom, attributionDecl); |
| 455 | auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code); |
| 456 | if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) { |
| 457 | write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second, |
| 458 | attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 459 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 460 | fprintf(out, " */\n"); |
| 461 | fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code); |
| 462 | } |
| 463 | fprintf(out, "\n"); |
| 464 | |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 465 | // Print constants for the enum values. |
| 466 | fprintf(out, " // Constants for enum values.\n\n"); |
| 467 | for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 468 | atom != atoms.decls.end(); atom++) { |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 469 | for (vector<AtomField>::const_iterator field = atom->fields.begin(); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 470 | field != atom->fields.end(); field++) { |
| 471 | if (field->javaType == JAVA_TYPE_ENUM) { |
| 472 | fprintf(out, " // Values for %s.%s\n", atom->message.c_str(), |
| 473 | field->name.c_str()); |
| 474 | for (map<int, string>::const_iterator value = field->enumValues.begin(); |
| 475 | value != field->enumValues.end(); value++) { |
| 476 | fprintf(out, " public static final int %s__%s__%s = %d;\n", |
| 477 | make_constant_name(atom->message).c_str(), |
| 478 | make_constant_name(field->name).c_str(), |
| 479 | make_constant_name(value->second).c_str(), |
| 480 | value->first); |
| 481 | } |
| 482 | fprintf(out, "\n"); |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 483 | } |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 487 | // Print write methods |
| 488 | fprintf(out, " // Write methods\n"); |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 489 | write_java_method(out, "write", atoms.signatures, attributionDecl); |
| 490 | write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 491 | |
| 492 | fprintf(out, "}\n"); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static const char* |
| 498 | jni_type_name(java_type_t type) |
| 499 | { |
| 500 | switch (type) { |
| 501 | case JAVA_TYPE_BOOLEAN: |
| 502 | return "jboolean"; |
| 503 | case JAVA_TYPE_INT: |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 504 | case JAVA_TYPE_ENUM: |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 505 | return "jint"; |
| 506 | case JAVA_TYPE_LONG: |
| 507 | return "jlong"; |
| 508 | case JAVA_TYPE_FLOAT: |
| 509 | return "jfloat"; |
| 510 | case JAVA_TYPE_DOUBLE: |
| 511 | return "jdouble"; |
| 512 | case JAVA_TYPE_STRING: |
| 513 | return "jstring"; |
| 514 | default: |
| 515 | return "UNKNOWN"; |
| 516 | } |
| 517 | } |
| 518 | |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 519 | static const char* |
| 520 | jni_array_type_name(java_type_t type) |
| 521 | { |
| 522 | switch (type) { |
| 523 | case JAVA_TYPE_INT: |
| 524 | return "jintArray"; |
| 525 | case JAVA_TYPE_STRING: |
| 526 | return "jobjectArray"; |
| 527 | default: |
| 528 | return "UNKNOWN"; |
| 529 | } |
| 530 | } |
| 531 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 532 | static string |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 533 | 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] | 534 | { |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 535 | string result("StatsLog_" + method_name); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 536 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 537 | arg != signature.end(); arg++) { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 538 | switch (*arg) { |
| 539 | case JAVA_TYPE_BOOLEAN: |
| 540 | result += "_boolean"; |
| 541 | break; |
| 542 | case JAVA_TYPE_INT: |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 543 | case JAVA_TYPE_ENUM: |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 544 | result += "_int"; |
| 545 | break; |
| 546 | case JAVA_TYPE_LONG: |
| 547 | result += "_long"; |
| 548 | break; |
| 549 | case JAVA_TYPE_FLOAT: |
| 550 | result += "_float"; |
| 551 | break; |
| 552 | case JAVA_TYPE_DOUBLE: |
| 553 | result += "_double"; |
| 554 | break; |
| 555 | case JAVA_TYPE_STRING: |
| 556 | result += "_String"; |
| 557 | break; |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 558 | case JAVA_TYPE_ATTRIBUTION_CHAIN: |
| 559 | result += "_AttributionChain"; |
| 560 | break; |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 561 | default: |
| 562 | result += "_UNKNOWN"; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | return result; |
| 567 | } |
| 568 | |
| 569 | static const char* |
| 570 | java_type_signature(java_type_t type) |
| 571 | { |
| 572 | switch (type) { |
| 573 | case JAVA_TYPE_BOOLEAN: |
| 574 | return "Z"; |
| 575 | case JAVA_TYPE_INT: |
| Stefan Lafon | 9478f35 | 2017-10-30 21:20:20 -0700 | [diff] [blame] | 576 | case JAVA_TYPE_ENUM: |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 577 | return "I"; |
| 578 | case JAVA_TYPE_LONG: |
| 579 | return "J"; |
| 580 | case JAVA_TYPE_FLOAT: |
| 581 | return "F"; |
| 582 | case JAVA_TYPE_DOUBLE: |
| 583 | return "D"; |
| 584 | case JAVA_TYPE_STRING: |
| 585 | return "Ljava/lang/String;"; |
| 586 | default: |
| 587 | return "UNKNOWN"; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | static string |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 592 | jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl) |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 593 | { |
| 594 | string result("(I"); |
| 595 | for (vector<java_type_t>::const_iterator arg = signature.begin(); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 596 | arg != signature.end(); arg++) { |
| 597 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 598 | for (auto chainField : attributionDecl.fields) { |
| 599 | result += "["; |
| 600 | result += java_type_signature(chainField.javaType); |
| 601 | } |
| 602 | } else { |
| 603 | result += java_type_signature(*arg); |
| 604 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 605 | } |
| 606 | result += ")V"; |
| 607 | return result; |
| 608 | } |
| 609 | |
| 610 | static int |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 611 | write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name, |
| 612 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 613 | { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 614 | // Print write methods |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 615 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 616 | signature != signatures.end(); signature++) { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 617 | int argIndex; |
| 618 | |
| 619 | fprintf(out, "static void\n"); |
| 620 | fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code", |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 621 | jni_function_name(java_method_name, *signature).c_str()); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 622 | argIndex = 1; |
| 623 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 624 | arg != signature->end(); arg++) { |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 625 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 626 | for (auto chainField : attributionDecl.fields) { |
| 627 | fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType), |
| 628 | chainField.name.c_str()); |
| 629 | } |
| 630 | } else { |
| 631 | fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex); |
| 632 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 633 | argIndex++; |
| 634 | } |
| 635 | fprintf(out, ")\n"); |
| 636 | |
| 637 | fprintf(out, "{\n"); |
| 638 | |
| 639 | // Prepare strings |
| 640 | argIndex = 1; |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 641 | bool hadStringOrChain = false; |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 642 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 643 | arg != signature->end(); arg++) { |
| 644 | if (*arg == JAVA_TYPE_STRING) { |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 645 | hadStringOrChain = true; |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 646 | fprintf(out, " const char* str%d;\n", argIndex); |
| 647 | fprintf(out, " if (arg%d != NULL) {\n", argIndex); |
| 648 | fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n", |
| 649 | argIndex, argIndex); |
| 650 | fprintf(out, " } else {\n"); |
| 651 | fprintf(out, " str%d = NULL;\n", argIndex); |
| 652 | fprintf(out, " }\n"); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 653 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 654 | hadStringOrChain = true; |
| 655 | for (auto chainField : attributionDecl.fields) { |
| 656 | fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n", |
| 657 | chainField.name.c_str(), chainField.name.c_str()); |
| 658 | if (chainField.name != attributionDecl.fields.front().name) { |
| 659 | fprintf(out, " if (%s_length != %s_length) {\n", |
| 660 | chainField.name.c_str(), |
| 661 | attributionDecl.fields.front().name.c_str()); |
| 662 | fprintf(out, " jniThrowException(env, " |
| 663 | "\"java/lang/IllegalArgumentException\", " |
| 664 | "\"invalid attribution field(%s) length.\");\n", |
| 665 | chainField.name.c_str()); |
| 666 | fprintf(out, " return;\n"); |
| 667 | fprintf(out, " }\n"); |
| 668 | } |
| 669 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 670 | fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n", |
| 671 | chainField.name.c_str(), chainField.name.c_str()); |
| 672 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 673 | fprintf(out, " std::vector<%s> %s_vec;\n", |
| 674 | cpp_type_name(chainField.javaType), chainField.name.c_str()); |
| 675 | fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n", |
| 676 | chainField.name.c_str()); |
| 677 | fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n", |
| 678 | chainField.name.c_str()); |
| 679 | fprintf(out, " jstring jstr = " |
| 680 | "(jstring)env->GetObjectArrayElement(%s, i);\n", |
| 681 | chainField.name.c_str()); |
| Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 682 | fprintf(out, " if (jstr == NULL) {\n"); |
| 683 | fprintf(out, " %s_vec.push_back(NULL);\n", |
| 684 | chainField.name.c_str()); |
| 685 | fprintf(out, " } else {\n"); |
| 686 | fprintf(out, " ScopedUtfChars* scoped_%s = " |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 687 | "new ScopedUtfChars(env, jstr);\n", |
| 688 | chainField.name.c_str()); |
| Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 689 | fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n", |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 690 | chainField.name.c_str(), chainField.name.c_str()); |
| Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 691 | fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n", |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 692 | chainField.name.c_str(), chainField.name.c_str()); |
| Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 693 | fprintf(out, " }\n"); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 694 | fprintf(out, " }\n"); |
| 695 | } |
| 696 | fprintf(out, "\n"); |
| 697 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 698 | } |
| 699 | argIndex++; |
| 700 | } |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 701 | // Emit this to quiet the unused parameter warning if there were no strings or attribution |
| 702 | // chains. |
| 703 | if (!hadStringOrChain) { |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 704 | fprintf(out, " (void)env;\n"); |
| 705 | } |
| 706 | |
| 707 | // stats_write call |
| 708 | argIndex = 1; |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 709 | fprintf(out, " android::util::%s(code", cpp_method_name.c_str()); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 710 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 711 | arg != signature->end(); arg++) { |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 712 | if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 713 | for (auto chainField : attributionDecl.fields) { |
| 714 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 715 | fprintf(out, ", (const %s*)%s_array, %s_length", |
| 716 | cpp_type_name(chainField.javaType), |
| 717 | chainField.name.c_str(), chainField.name.c_str()); |
| 718 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| 719 | fprintf(out, ", %s_vec", chainField.name.c_str()); |
| 720 | } |
| 721 | } |
| 722 | } else { |
| 723 | const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg"; |
| 724 | fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex); |
| 725 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 726 | argIndex++; |
| 727 | } |
| 728 | fprintf(out, ");\n"); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 729 | fprintf(out, "\n"); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 730 | |
| 731 | // Clean up strings |
| 732 | argIndex = 1; |
| 733 | for (vector<java_type_t>::const_iterator arg = signature->begin(); |
| 734 | arg != signature->end(); arg++) { |
| 735 | if (*arg == JAVA_TYPE_STRING) { |
| 736 | fprintf(out, " if (str%d != NULL) {\n", argIndex); |
| 737 | fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n", |
| 738 | argIndex, argIndex); |
| 739 | fprintf(out, " }\n"); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 740 | } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
| 741 | for (auto chainField : attributionDecl.fields) { |
| 742 | if (chainField.javaType == JAVA_TYPE_INT) { |
| 743 | fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n", |
| 744 | chainField.name.c_str(), chainField.name.c_str()); |
| 745 | } else if (chainField.javaType == JAVA_TYPE_STRING) { |
| Yangster-mac | 20ac944 | 2018-01-08 14:54:48 -0800 | [diff] [blame] | 746 | 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] | 747 | chainField.name.c_str()); |
| 748 | fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str()); |
| 749 | fprintf(out, " }\n"); |
| 750 | } |
| 751 | } |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 752 | } |
| 753 | argIndex++; |
| 754 | } |
| 755 | |
| 756 | fprintf(out, "}\n"); |
| 757 | fprintf(out, "\n"); |
| 758 | } |
| 759 | |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | void write_jni_registration(FILE* out, const string& java_method_name, |
| 765 | const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) { |
| 766 | for (set<vector<java_type_t>>::const_iterator signature = signatures.begin(); |
| 767 | signature != signatures.end(); signature++) { |
| 768 | fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n", |
| 769 | java_method_name.c_str(), |
| 770 | jni_function_signature(*signature, attributionDecl).c_str(), |
| 771 | jni_function_name(java_method_name, *signature).c_str()); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static int |
| 776 | write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl) |
| 777 | { |
| 778 | // Print prelude |
| 779 | fprintf(out, "// This file is autogenerated\n"); |
| 780 | fprintf(out, "\n"); |
| 781 | |
| 782 | fprintf(out, "#include <statslog.h>\n"); |
| 783 | fprintf(out, "\n"); |
| 784 | fprintf(out, "#include <nativehelper/JNIHelp.h>\n"); |
| 785 | fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n"); |
| 786 | fprintf(out, "#include <utils/Vector.h>\n"); |
| 787 | fprintf(out, "#include \"core_jni_helpers.h\"\n"); |
| 788 | fprintf(out, "#include \"jni.h\"\n"); |
| 789 | fprintf(out, "\n"); |
| 790 | fprintf(out, "#define UNUSED __attribute__((__unused__))\n"); |
| 791 | fprintf(out, "\n"); |
| 792 | |
| 793 | fprintf(out, "namespace android {\n"); |
| 794 | fprintf(out, "\n"); |
| 795 | |
| 796 | write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl); |
| 797 | write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained", |
| 798 | atoms.non_chained_signatures, attributionDecl); |
| 799 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 800 | // Print registration function table |
| 801 | fprintf(out, "/*\n"); |
| 802 | fprintf(out, " * JNI registration.\n"); |
| 803 | fprintf(out, " */\n"); |
| 804 | fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n"); |
| Yangster-mac | ba5b9e4 | 2018-01-10 21:31:59 -0800 | [diff] [blame^] | 805 | write_jni_registration(out, "write", atoms.signatures, attributionDecl); |
| 806 | write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 807 | fprintf(out, "};\n"); |
| 808 | fprintf(out, "\n"); |
| 809 | |
| 810 | // Print registration function |
| 811 | fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n"); |
| 812 | fprintf(out, " return RegisterMethodsOrDie(\n"); |
| 813 | fprintf(out, " env,\n"); |
| 814 | fprintf(out, " \"android/util/StatsLog\",\n"); |
| 815 | fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n"); |
| 816 | fprintf(out, "}\n"); |
| 817 | |
| 818 | fprintf(out, "\n"); |
| 819 | fprintf(out, "} // namespace android\n"); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 820 | return 0; |
| 821 | } |
| 822 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 823 | static void |
| 824 | print_usage() |
| 825 | { |
| 826 | fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n"); |
| 827 | fprintf(stderr, "\n"); |
| 828 | fprintf(stderr, "OPTIONS\n"); |
| 829 | fprintf(stderr, " --cpp FILENAME the header file to output\n"); |
| 830 | fprintf(stderr, " --header FILENAME the cpp file to output\n"); |
| 831 | fprintf(stderr, " --help this message\n"); |
| 832 | fprintf(stderr, " --java FILENAME the java file to output\n"); |
| 833 | fprintf(stderr, " --jni FILENAME the jni file to output\n"); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Do the argument parsing and execute the tasks. |
| 838 | */ |
| 839 | static int |
| 840 | run(int argc, char const*const* argv) |
| 841 | { |
| 842 | string cppFilename; |
| 843 | string headerFilename; |
| 844 | string javaFilename; |
| 845 | string jniFilename; |
| 846 | |
| 847 | int index = 1; |
| 848 | while (index < argc) { |
| 849 | if (0 == strcmp("--help", argv[index])) { |
| 850 | print_usage(); |
| 851 | return 0; |
| 852 | } else if (0 == strcmp("--cpp", argv[index])) { |
| 853 | index++; |
| 854 | if (index >= argc) { |
| 855 | print_usage(); |
| 856 | return 1; |
| 857 | } |
| 858 | cppFilename = argv[index]; |
| 859 | } else if (0 == strcmp("--header", argv[index])) { |
| 860 | index++; |
| 861 | if (index >= argc) { |
| 862 | print_usage(); |
| 863 | return 1; |
| 864 | } |
| 865 | headerFilename = argv[index]; |
| 866 | } else if (0 == strcmp("--java", argv[index])) { |
| 867 | index++; |
| 868 | if (index >= argc) { |
| 869 | print_usage(); |
| 870 | return 1; |
| 871 | } |
| 872 | javaFilename = argv[index]; |
| 873 | } else if (0 == strcmp("--jni", argv[index])) { |
| 874 | index++; |
| 875 | if (index >= argc) { |
| 876 | print_usage(); |
| 877 | return 1; |
| 878 | } |
| 879 | jniFilename = argv[index]; |
| 880 | } |
| 881 | index++; |
| 882 | } |
| 883 | |
| 884 | if (cppFilename.size() == 0 |
| 885 | && headerFilename.size() == 0 |
| 886 | && javaFilename.size() == 0 |
| 887 | && jniFilename.size() == 0) { |
| 888 | print_usage(); |
| 889 | return 1; |
| 890 | } |
| 891 | |
| 892 | // Collate the parameters |
| 893 | Atoms atoms; |
| Stefan Lafon | ae2df01 | 2017-11-14 09:17:21 -0800 | [diff] [blame] | 894 | int errorCount = collate_atoms(Atom::descriptor(), &atoms); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 895 | if (errorCount != 0) { |
| 896 | return 1; |
| 897 | } |
| 898 | |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 899 | AtomDecl attributionDecl; |
| 900 | vector<java_type_t> attributionSignature; |
| Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 901 | collate_atom(android::os::statsd::AttributionNode::descriptor(), |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 902 | &attributionDecl, &attributionSignature); |
| 903 | |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 904 | // Write the .cpp file |
| 905 | if (cppFilename.size() != 0) { |
| 906 | FILE* out = fopen(cppFilename.c_str(), "w"); |
| 907 | if (out == NULL) { |
| 908 | fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str()); |
| 909 | return 1; |
| 910 | } |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 911 | errorCount = android::stats_log_api_gen::write_stats_log_cpp( |
| 912 | out, atoms, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 913 | fclose(out); |
| 914 | } |
| 915 | |
| 916 | // Write the .h file |
| 917 | if (headerFilename.size() != 0) { |
| 918 | FILE* out = fopen(headerFilename.c_str(), "w"); |
| 919 | if (out == NULL) { |
| 920 | fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str()); |
| 921 | return 1; |
| 922 | } |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 923 | errorCount = android::stats_log_api_gen::write_stats_log_header( |
| 924 | out, atoms, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 925 | fclose(out); |
| 926 | } |
| 927 | |
| 928 | // Write the .java file |
| 929 | if (javaFilename.size() != 0) { |
| 930 | FILE* out = fopen(javaFilename.c_str(), "w"); |
| 931 | if (out == NULL) { |
| 932 | fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str()); |
| 933 | return 1; |
| 934 | } |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 935 | errorCount = android::stats_log_api_gen::write_stats_log_java( |
| 936 | out, atoms, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 937 | fclose(out); |
| 938 | } |
| 939 | |
| 940 | // Write the jni file |
| 941 | if (jniFilename.size() != 0) { |
| 942 | FILE* out = fopen(jniFilename.c_str(), "w"); |
| 943 | if (out == NULL) { |
| 944 | fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str()); |
| 945 | return 1; |
| 946 | } |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 947 | errorCount = android::stats_log_api_gen::write_stats_log_jni( |
| 948 | out, atoms, attributionDecl); |
| Yao Chen | d54f9dd | 2017-10-17 17:37:48 +0000 | [diff] [blame] | 949 | fclose(out); |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | /** |
| 959 | * Main. |
| 960 | */ |
| 961 | int |
| 962 | main(int argc, char const*const* argv) |
| 963 | { |
| 964 | GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 965 | |
| 966 | return android::stats_log_api_gen::run(argc, argv); |
| Yangster-mac | 7604aea | 2017-12-11 22:55:49 -0800 | [diff] [blame] | 967 | } |