blob: 3dbb50306cc63d60ae23f5f1e6ba90054a054dc8 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001
2
3#include "Collation.h"
4
Stefan Lafonae2df012017-11-14 09:17:21 -08005#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +00006
7#include <set>
8#include <vector>
9
10#include <getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15using namespace google::protobuf;
16using namespace std;
17
18namespace android {
19namespace stats_log_api_gen {
20
Yao Chenb3561512017-11-21 18:07:17 -080021const int PULL_ATOM_START_ID = 1000;
22
23int maxPushedAtomId = 2;
24
Stefan Lafonae2df012017-11-14 09:17:21 -080025using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000026
Yao Chend54f9dd2017-10-17 17:37:48 +000027/**
28 * Turn lower and camel case into upper case with underscores.
29 */
30static string
31make_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
54static const char*
55cpp_type_name(java_type_t type)
56{
57 switch (type) {
58 case JAVA_TYPE_BOOLEAN:
59 return "bool";
60 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070061 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000062 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
76static const char*
77java_type_name(java_type_t type)
78{
79 switch (type) {
80 case JAVA_TYPE_BOOLEAN:
81 return "boolean";
82 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070083 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000084 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-mac7604aea2017-12-11 22:55:49 -080098static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
99 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000100 // Print prelude
101 fprintf(out, "// This file is autogenerated\n");
102 fprintf(out, "\n");
103
Yangster-mac7604aea2017-12-11 22:55:49 -0800104 fprintf(out, "#include <exception>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000105 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 Chen80235402017-11-13 20:42:25 -0800112 fprintf(out, "// the single event tag id for all stats logs\n");
113 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000114
115 // Print write methods
116 fprintf(out, "\n");
117 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800118 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000119 int argIndex;
120
121 fprintf(out, "void\n");
Yao Chen80235402017-11-13 20:42:25 -0800122 fprintf(out, "stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000123 argIndex = 1;
124 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800125 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 Chend54f9dd2017-10-17 17:37:48 +0000141 argIndex++;
142 }
143 fprintf(out, ")\n");
144
145 fprintf(out, "{\n");
146 argIndex = 1;
Yao Chen80235402017-11-13 20:42:25 -0800147 fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800148 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000149 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800150 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-mac20ac9442018-01-08 14:54:48 -0800169 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-mac7604aea2017-12-11 22:55:49 -0800178 }
179 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000180 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800181 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 Chend54f9dd2017-10-17 17:37:48 +0000189 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000190 argIndex++;
191 }
192
193 fprintf(out, " event.write(LOG_ID_STATS);\n");
194 fprintf(out, "}\n");
195 fprintf(out, "\n");
196 }
197
Yangster-macba5b9e42018-01-10 21:31:59 -0800198 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 Chend54f9dd2017-10-17 17:37:48 +0000239 // 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-macba5b9e42018-01-10 21:31:59 -0800247void 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
255static 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
280static 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 Chend54f9dd2017-10-17 17:37:48 +0000309
310static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800311write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000312{
Yao Chend54f9dd2017-10-17 17:37:48 +0000313 // 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-mac7604aea2017-12-11 22:55:49 -0800319 fprintf(out, "#include <vector>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800320 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000321 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 Lafon9478f352017-10-30 21:20:20 -0700331 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000332 fprintf(out, " */\n");
333 fprintf(out, "enum {\n");
334
Yangster-macba5b9e42018-01-10 21:31:59 -0800335 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 Chend54f9dd2017-10-17 17:37:48 +0000338 size_t i = 0;
339 // Print constants
340 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800341 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000342 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-macba5b9e42018-01-10 21:31:59 -0800346 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 Chend54f9dd2017-10-17 17:37:48 +0000352 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000353 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 Chenb3561512017-11-21 18:07:17 -0800356 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
357 maxPushedAtomId = atom->code;
358 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000359 i++;
360 }
361 fprintf(out, "\n");
362 fprintf(out, "};\n");
363 fprintf(out, "\n");
364
Yangster-mac68985802018-01-21 10:05:09 -0800365 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-mac7604aea2017-12-11 22:55:49 -0800395 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", maxPushedAtomId);
Yao Chenb3561512017-11-21 18:07:17 -0800396
Yao Chend54f9dd2017-10-17 17:37:48 +0000397 // Print write methods
398 fprintf(out, "//\n");
399 fprintf(out, "// Write methods\n");
400 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800401 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 Chend54f9dd2017-10-17 17:37:48 +0000408
409 fprintf(out, "\n");
410 fprintf(out, "} // namespace util\n");
411 fprintf(out, "} // namespace android\n");
412
413 return 0;
414}
415
Yangster-macba5b9e42018-01-10 21:31:59 -0800416static 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
435static 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 Chend54f9dd2017-10-17 17:37:48 +0000459static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800460write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000461{
Yao Chend54f9dd2017-10-17 17:37:48 +0000462 // 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 Chen0a368b22017-12-06 16:28:16 -0800472 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700473 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000474
Yangster-macba5b9e42018-01-10 21:31:59 -0800475 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 Lafon9478f352017-10-30 21:20:20 -0700478 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000479 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-macba5b9e42018-01-10 21:31:59 -0800485 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 Chend54f9dd2017-10-17 17:37:48 +0000490 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000491 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 Lafon9478f352017-10-30 21:20:20 -0700496 // 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-mac7604aea2017-12-11 22:55:49 -0800499 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700500 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800501 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 Lafon9478f352017-10-30 21:20:20 -0700514 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700515 }
516 }
517
Yao Chend54f9dd2017-10-17 17:37:48 +0000518 // Print write methods
519 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800520 write_java_method(out, "write", atoms.signatures, attributionDecl);
521 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000522
523 fprintf(out, "}\n");
524
525 return 0;
526}
527
528static const char*
529jni_type_name(java_type_t type)
530{
531 switch (type) {
532 case JAVA_TYPE_BOOLEAN:
533 return "jboolean";
534 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700535 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000536 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-mac7604aea2017-12-11 22:55:49 -0800550static const char*
551jni_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 Chend54f9dd2017-10-17 17:37:48 +0000563static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800564jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000565{
Yangster-macba5b9e42018-01-10 21:31:59 -0800566 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000567 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800568 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000569 switch (*arg) {
570 case JAVA_TYPE_BOOLEAN:
571 result += "_boolean";
572 break;
573 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700574 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000575 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-mac7604aea2017-12-11 22:55:49 -0800589 case JAVA_TYPE_ATTRIBUTION_CHAIN:
590 result += "_AttributionChain";
591 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000592 default:
593 result += "_UNKNOWN";
594 break;
595 }
596 }
597 return result;
598}
599
600static const char*
601java_type_signature(java_type_t type)
602{
603 switch (type) {
604 case JAVA_TYPE_BOOLEAN:
605 return "Z";
606 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700607 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000608 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
622static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800623jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000624{
625 string result("(I");
626 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800627 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 Chend54f9dd2017-10-17 17:37:48 +0000636 }
637 result += ")V";
638 return result;
639}
640
641static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800642write_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 Chend54f9dd2017-10-17 17:37:48 +0000644{
Yao Chend54f9dd2017-10-17 17:37:48 +0000645 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -0800646 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
647 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000648 int argIndex;
649
650 fprintf(out, "static void\n");
651 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -0800652 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000653 argIndex = 1;
654 for (vector<java_type_t>::const_iterator arg = signature->begin();
655 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800656 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 Chend54f9dd2017-10-17 17:37:48 +0000664 argIndex++;
665 }
666 fprintf(out, ")\n");
667
668 fprintf(out, "{\n");
669
670 // Prepare strings
671 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -0800672 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000673 for (vector<java_type_t>::const_iterator arg = signature->begin();
674 arg != signature->end(); arg++) {
675 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800676 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000677 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-mac7604aea2017-12-11 22:55:49 -0800684 } 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-mac20ac9442018-01-08 14:54:48 -0800713 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-mac7604aea2017-12-11 22:55:49 -0800718 "new ScopedUtfChars(env, jstr);\n",
719 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800720 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800721 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800722 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800723 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800724 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800725 fprintf(out, " }\n");
726 }
727 fprintf(out, "\n");
728 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000729 }
730 argIndex++;
731 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800732 // Emit this to quiet the unused parameter warning if there were no strings or attribution
733 // chains.
734 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000735 fprintf(out, " (void)env;\n");
736 }
737
738 // stats_write call
739 argIndex = 1;
Yangster-macba5b9e42018-01-10 21:31:59 -0800740 fprintf(out, " android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000741 for (vector<java_type_t>::const_iterator arg = signature->begin();
742 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800743 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 Chend54f9dd2017-10-17 17:37:48 +0000757 argIndex++;
758 }
759 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800760 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000761
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-mac7604aea2017-12-11 22:55:49 -0800771 } 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-mac20ac9442018-01-08 14:54:48 -0800777 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800778 chainField.name.c_str());
779 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
780 fprintf(out, " }\n");
781 }
782 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000783 }
784 argIndex++;
785 }
786
787 fprintf(out, "}\n");
788 fprintf(out, "\n");
789 }
790
Yangster-macba5b9e42018-01-10 21:31:59 -0800791
792 return 0;
793}
794
795void 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
806static int
807write_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 Chend54f9dd2017-10-17 17:37:48 +0000831 // 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-macba5b9e42018-01-10 21:31:59 -0800836 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
837 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000838 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 Chend54f9dd2017-10-17 17:37:48 +0000851 return 0;
852}
853
Yao Chend54f9dd2017-10-17 17:37:48 +0000854static void
855print_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 */
870static int
871run(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 Lafonae2df012017-11-14 09:17:21 -0800925 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +0000926 if (errorCount != 0) {
927 return 1;
928 }
929
Yangster-mac7604aea2017-12-11 22:55:49 -0800930 AtomDecl attributionDecl;
931 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -0800932 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -0800933 &attributionDecl, &attributionSignature);
934
Yao Chend54f9dd2017-10-17 17:37:48 +0000935 // 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-mac7604aea2017-12-11 22:55:49 -0800942 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
943 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000944 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-mac7604aea2017-12-11 22:55:49 -0800954 errorCount = android::stats_log_api_gen::write_stats_log_header(
955 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000956 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-mac7604aea2017-12-11 22:55:49 -0800966 errorCount = android::stats_log_api_gen::write_stats_log_java(
967 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000968 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-mac7604aea2017-12-11 22:55:49 -0800978 errorCount = android::stats_log_api_gen::write_stats_log_jni(
979 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000980 fclose(out);
981 }
982
983 return 0;
984}
985
986}
987}
988
989/**
990 * Main.
991 */
992int
993main(int argc, char const*const* argv)
994{
995 GOOGLE_PROTOBUF_VERIFY_VERSION;
996
997 return android::stats_log_api_gen::run(argc, argv);
Yangster-mac7604aea2017-12-11 22:55:49 -0800998}