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