-
Notifications
You must be signed in to change notification settings - Fork 20
/
binder_tester.c
1123 lines (931 loc) · 24 KB
/
binder_tester.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <signal.h>
#define __USE_GNU
#include <sched.h>
#ifdef INLINE_TRANSACTION_DATA
#define RBUF_SIZE 4096
#else
#define RBUF_SIZE 128
#endif
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
#include "binder.h"
#define INST_MAX_ENTRIES 32
#define SVC_MAGIC 0x696e7374
#define SVC_BINDER ((void *)SVC_MAGIC)
#define SVC_COOKIE ((void *)SVC_MAGIC)
#define ALIGN(n) (((n) + 3) & ~3)
typedef struct binder_write_read bwr_t;
typedef struct flat_binder_object obj_t;
typedef struct binder_transaction_data tdata_t;
typedef struct {
uint32_t cmd;
tdata_t tdata;
} bcmd_txn_t;
typedef union {
struct timeval tv;
char label[8];
} inst_entry_t;
typedef struct {
uint32_t magic;
uint32_t seq;
uint32_t max_entries;
uint32_t next_entry;
inst_entry_t entries[INST_MAX_ENTRIES];
} inst_buf_t;
uint16_t svcmgr_id[] = { 'a','n','d','r','o','i','d','.','o','s','.',
'I','S','e','r','v','i','c','e','M','a','n','a','g','e','r' };
static uint16_t service[] = { 'i', 'n', 's', 't' };
static int clients = 1;
static int time_ref = 1;
static int inst_kernel = 1;
static int share_cpus = 1;
static int iterations = 1000;
static char *output_file;
static int id;
static unsigned int ioctl_read, ioctl_write, ioctl_buffer;
void hexdump(const void *buf, unsigned long size)
{
int col = 0, off = 0;
unsigned char *p = (unsigned char *)buf;
while (size--) {
if (!col)
printf("\t%08x:", off);
printf(" %02x", *p++);
off++;
col++;
if (!(col % 16)) {
printf("\n");
col = 0;
} else if (!(col % 4))
printf(" ");
}
if (col % 16)
printf("\n\n");
else
printf("\n");
}
inline void INST_INIT(inst_buf_t *inst)
{
inst->magic = inst_kernel ? SVC_MAGIC : ~SVC_MAGIC;
inst->seq = 0;
inst->max_entries = INST_MAX_ENTRIES;
}
inline void INST_BEGIN(inst_buf_t *inst)
{
inst->next_entry = 0;
}
inline void INST_RECORD(inst_entry_t *entry)
{
gettimeofday(&entry->tv, NULL);
}
inline void INST_ENTRY_COPY(inst_buf_t *inst, char *label, inst_entry_t *copy)
{
if (inst->next_entry < inst->max_entries) {
inst_entry_t *entry = inst->entries + inst->next_entry++;
if (inst->seq)
*entry = *copy;
else {
strncpy(entry->label, label, 8);
entry->label[7] = '\0';
}
}
}
inline void INST_ENTRY(inst_buf_t *inst, char *label)
{
inst_entry_t copy;
gettimeofday(©.tv, NULL);
INST_ENTRY_COPY(inst, label, ©);
}
inline void INST_END(inst_buf_t *inst, unsigned char **buf)
{
if (buf) {
int size = inst->next_entry * sizeof(inst_entry_t);
memcpy(*buf, inst->entries, size);
*buf += size;
}
inst->seq++;
}
bcmd_txn_t *create_transaction(int reply,
void *binder, void *cookie, unsigned int code,
unsigned char *data, unsigned int data_size, unsigned char *offsets, unsigned int offsets_size)
{
bcmd_txn_t *txn;
tdata_t *tdata;
unsigned char *p;
unsigned int size, data_off = 0, offsets_off = 0;
size = ALIGN(sizeof(*txn));
if (data_size > 0) {
data_off = size;
size += ALIGN(data_size);
if (offsets_size > 0) {
offsets_off = size;
size += ALIGN(offsets_size);
}
}
p = malloc(size);
if (!p)
return NULL;
txn = (bcmd_txn_t *)p;
txn->cmd = reply ? BC_REPLY : BC_TRANSACTION;
tdata = &txn->tdata;
memset(tdata, 0, sizeof(*tdata));
tdata->target.ptr = binder;
tdata->cookie = cookie;
tdata->code = code;
if (data_size > 0) {
if (data)
memcpy(p + data_off, data, data_size);
tdata->data.ptr.buffer = p + data_off;
tdata->data_size = data_size;
if (offsets_size > 0) {
if (offsets)
memcpy(p + offsets_off, offsets, offsets_size);
tdata->data.ptr.offsets = p + offsets_off;
tdata->offsets_size = offsets_size;
}
}
return txn;
}
#if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA))
int FREE_BUFFER(int fd, void *ptr)
{
bwr_t bwr;
uint32_t cmd[2];
cmd[0] = BC_FREE_BUFFER;
cmd[1] = (uint32_t)ptr;
memset(&bwr, 0, sizeof(bwr));
bwr.write_buffer = (unsigned long)cmd;
bwr.write_size = sizeof(cmd);
ioctl_buffer++;
return ioctl(fd, BINDER_WRITE_READ, &bwr);
}
#endif
int parse_command(void *buf, unsigned long size, tdata_t **reply)
{
unsigned char *p, *ep;
unsigned int cmd;
tdata_t *tdata = NULL;
#ifdef INLINE_TRANSACTION_DATA
unsigned long buffer_size;
#endif
p = buf;
ep = p + size;
while (p < ep) {
cmd = *(unsigned int *)p;
p += sizeof(cmd);
switch (cmd) {
case BR_NOOP:
case BR_TRANSACTION_COMPLETE:
break;
case BR_INCREFS:
case BR_ACQUIRE:
case BR_RELEASE:
case BR_DECREFS:
if (p + 2 * sizeof(cmd) > ep) {
fprintf(stderr, "not enough ref_cmd data\n");
return -1;
}
p += 2 * sizeof(cmd);
break;
case BR_TRANSACTION:
fprintf(stderr, "rcv transaction\n");
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "not enough transaction data\n");
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "not enough transaction data buffer\n");
return -1;
}
p += buffer_size;
#endif
tdata = NULL;
break;
case BR_REPLY:
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "not enough transaction data\n");
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "not enough transaction data buffer\n");
return -1;
}
p += buffer_size;
#endif
goto expected_out;
case BR_DEAD_BINDER:
fprintf(stderr, "rcv DEAD_BINDER\n");
if (p + sizeof(cmd) > ep) {
fprintf(stderr, "not enough dead binder data\n");
return -1;
}
p += sizeof(cmd);
break;
case BR_FAILED_REPLY:
fprintf(stderr, "rcv FAILED_BINDER\n");
return -1;
case BR_DEAD_REPLY:
fprintf(stderr, "rcv DEAD_BINDER\n");
return -1;
default:
fprintf(stderr, "rcv unknown command: %u\n", cmd);
return -1;
}
}
expected_out:
*reply = tdata;
return p - (unsigned char *)buf;
}
int simple_transact(int fd, bcmd_txn_t *txn, tdata_t **preply, unsigned char *buf, unsigned int size)
{
bwr_t bwr;
tdata_t *reply = NULL;
int r, retries = 2;
bwr.write_buffer = (unsigned long)txn;
bwr.write_size = sizeof(*txn);
bwr.write_consumed = 0;
wait_reply:
bwr.read_buffer = (unsigned long)buf;
bwr.read_size = size;
bwr.read_consumed = 0;
r = ioctl(fd, BINDER_WRITE_READ, &bwr);
if (r < 0)
return r;
if (bwr.read_consumed > 0) {
r = parse_command(buf, bwr.read_consumed, &reply);
if (r < 0)
return r;
}
if (reply) {
*preply = reply;
return 0;
}
if (retries-- <= 0) {
fprintf(stderr, "no reply received\n");
return -1;
}
bwr.write_size = 0;
goto wait_reply;
}
int add_service(int fd, void *binder, void *cookie, uint16_t *name, int len)
{
unsigned char buf[1024], *p;
obj_t *obj;
size_t *offsets;
bcmd_txn_t *txn;
tdata_t *tdata;
int r;
p = buf;
// strict_policy
*(uint32_t *)p = 0;
p += 4;
// svcmgr_id
*(uint32_t *)p = sizeof(svcmgr_id) / 2;
p += 4;
memcpy(p, svcmgr_id, sizeof(svcmgr_id));
p += sizeof(svcmgr_id);
*(uint16_t *)p = 0;
p += 2;
p = (unsigned char *)ALIGN((unsigned long)p);
// name
*(uint32_t *)p = len;
p += 4;
memcpy(p, name, len * 2);
p += len * 2;
*(uint16_t *)p = 0;
p += 2;
p = (unsigned char *)ALIGN((unsigned long)p);
// flat_binder_obj
obj = (obj_t *)p;
obj->type = BINDER_TYPE_BINDER;
obj->flags = 0;
obj->binder = binder;
obj->cookie = cookie;
p = (unsigned char *)(obj + 1);
// offsets
offsets = (size_t *)p;
*offsets = (unsigned char *)obj - buf;
txn = create_transaction(0, NULL, NULL, 3, buf, p - buf, (unsigned char *)offsets, 4);
if (!txn)
return -1;
r = simple_transact(fd, txn, &tdata, buf, sizeof(buf));
if (r < 0)
return r;
if (tdata->data_size != 4 || *(unsigned int *)tdata->data.ptr.buffer) {
fprintf(stderr, "server invalid reply data received\n");
return -1;
}
#if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA))
if (FREE_BUFFER(fd, (void *)tdata->data.ptr.buffer) < 0) {
fprintf(stderr, "failed to free shared buffer\n");
return -1;
}
#endif
free(txn);
return 0;
}
int start_looper(int fd)
{
bwr_t bwr;
uint32_t cmd[1];
cmd[0] = BC_ENTER_LOOPER;
memset(&bwr, 0, sizeof(bwr));
bwr.write_buffer = (unsigned long)cmd;
bwr.write_size = sizeof(cmd);
return ioctl(fd, BINDER_WRITE_READ, &bwr);
}
int lookup_service(int fd, uint16_t *name, int len, void **binder, void **cookie)
{
unsigned char buf[1024], *p;
obj_t *obj;
bcmd_txn_t *txn;
tdata_t *tdata;
int r;
p = buf;
// strict_policy
*(uint32_t *)p = 0;
p += 4;
// svcmgr_id
*(uint32_t *)p = sizeof(svcmgr_id) / 2;
p += 4;
memcpy(p, svcmgr_id, sizeof(svcmgr_id));
p += sizeof(svcmgr_id);
*(uint16_t *)p = 0;
p += 2;
p = (unsigned char *)ALIGN((unsigned long)p);
// name
*(uint32_t *)p = len;
p += 4;
memcpy(p, name, len * 2);
p += len * 2;
*(uint16_t *)p = 0;
p += 2;
p = (unsigned char *)ALIGN((unsigned long)p);
txn = create_transaction(0, NULL, NULL, 1, buf, p - buf, NULL, 0);
if (!txn)
return -1;
r = simple_transact(fd, txn, &tdata, buf, sizeof(buf));
if (r < 0)
return r;
if (tdata->data_size == 4 && !*(unsigned int *)tdata->data.ptr.buffer)
return 0; // server not ready
if (tdata->data_size != sizeof(*obj) || tdata->offsets_size != 4) {
fprintf(stderr, "client %d invalid reply data received\n", id);
return -1;
}
obj = (obj_t *)((unsigned char *)tdata->data.ptr.buffer + *(unsigned int *)tdata->data.ptr.offsets);
if (obj->type != BINDER_TYPE_HANDLE) {
fprintf(stderr, "client %d invalid object type received\n", id);
return -1;
}
*binder = obj->binder;
*cookie = obj->cookie;
free(txn);
return 1;
}
int server_parse_command(unsigned char *buf, long size, tdata_t **tdata_out, bcmd_txn_t **txn_out)
{
unsigned char *p, *ep;
unsigned int cmd;
tdata_t *tdata = NULL;
bcmd_txn_t *txn = NULL;
#ifdef INLINE_TRANSACTION_DATA
unsigned long buffer_size;
#endif
p = buf;
ep = p + size;
while (p < ep) {
cmd = *(unsigned int *)p;
p += sizeof(cmd);
switch (cmd) {
case BR_NOOP:
case BR_TRANSACTION_COMPLETE:
break;
case BR_INCREFS:
case BR_ACQUIRE:
case BR_RELEASE:
case BR_DECREFS:
fprintf(stderr, "server received unexpected ref_cmd command\n");
if (p + 2 * sizeof(cmd) > ep) {
fprintf(stderr, "server not enough ref_cmd data\n");
return -1;
}
p += 2 * sizeof(cmd);
break;
case BR_TRANSACTION:
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "server not enough transaction data\n");
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
if (tdata->data_size != sizeof(inst_buf_t)) {
fprintf(stderr, "server data size in transaction is incorrect\n");
return -1;
}
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "server not enough transaction data buffer\n");
return -1;
}
txn = (bcmd_txn_t *)(p - sizeof(*tdata) - sizeof(cmd));
txn->cmd = BC_REPLY;
p += buffer_size;
#else
txn = create_transaction(1, NULL, NULL, 0,
(unsigned char *)tdata->data.ptr.buffer, tdata->data_size,
(unsigned char *)tdata->data.ptr.offsets, tdata->offsets_size);
if (!txn) {
fprintf(stderr, "server failed to create reply buffer\n");
return -1;
}
#endif
goto expected_out;
case BR_REPLY:
fprintf(stderr, "server received unexpected reply command\n");
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "server not enough transaction data\n");
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "server not enough transaction data buffer\n");
return -1;
}
p += buffer_size;
#endif
break;
case BR_DEAD_BINDER:
fprintf(stderr, "server received unexpected dead_binder command\n");
if (p + sizeof(cmd) > ep) {
fprintf(stderr, "server not enough dead binder data\n");
return -1;
}
p += sizeof(cmd);
break;
case BR_FAILED_REPLY:
fprintf(stderr, "server received unexpected failed_reply command\n");
return -1;
case BR_DEAD_REPLY:
fprintf(stderr, "server received unexpected dead_reply command\n");
return -1;
default:
fprintf(stderr, "server received unknown command\n");
return -1;
}
}
expected_out:
*tdata_out = tdata;
*txn_out = txn;
return (p - buf);
}
int server_main(void)
{
int fd, r, len;
void *binder, *cookie;
bwr_t bwr;
unsigned char rbuf[RBUF_SIZE], *p;
bcmd_txn_t *reply;
tdata_t *tdata = NULL;
inst_buf_t *inst;
inst_entry_t copy;
if (!share_cpus) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
r = sched_setaffinity(0, sizeof(cpuset), &cpuset);
if (!r)
printf("server is bound to CPU 0\n");
else
fprintf(stderr, "server failed to be bound to CPU 0\n");
}
fd = open("/dev/binder", O_RDWR);
if (fd < 0) {
fprintf(stderr, "failed to open binder device\n");
return -1;
}
#if (!defined(INLINE_TRANSACTION_DATA))
if (mmap(NULL, 128 * 1024, PROT_READ, MAP_PRIVATE, fd, 0) == MAP_FAILED) {
fprintf(stderr, "server failed to mmap shared buffer\n");
return -1;
}
#endif
binder = SVC_BINDER;
cookie = SVC_COOKIE;
r = add_service(fd, binder, cookie, service, sizeof(service) / 2);
if (r < 0) {
printf("server failed to add instrumentation service\n");
return -1;
}
printf("server added instrumentation service\n");
r = start_looper(fd);
if (r < 0) {
printf("server failed to start looper\n");
return -1;
}
bwr.read_buffer = (unsigned long)rbuf;
while (1) {
bwr.read_size = sizeof(rbuf);
bwr.read_consumed = 0;
bwr.write_size = 0;
ioctl_read++;
r = ioctl(fd, BINDER_WRITE_READ, &bwr);
if (r < 0) {
fprintf(stderr, "server failed ioctl\n");
return r;
}
INST_RECORD(©);
p = rbuf;
len = bwr.read_consumed;
while (len > 0) {
r = server_parse_command(p, len, &tdata, &reply);
if (r < 0)
return r;
p += r;
len -= r;
#if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA))
if (tdata)
FREE_BUFFER(fd, (void *)tdata->data.ptr.buffer);
#endif
if (!reply) {
//hexdump(rbuf, bwr.read_consumed);
continue;
}
inst = (inst_buf_t *)reply->tdata.data.ptr.buffer;
INST_ENTRY_COPY(inst, "S_RECV", ©);
bwr.write_buffer = (unsigned long)reply;
bwr.write_size = sizeof(*reply);
bwr.write_consumed = 0;
bwr.read_size = 0;
INST_ENTRY(inst, "S_REPLY");
ioctl_write++;
r = ioctl(fd, BINDER_WRITE_READ, &bwr);
if (r < 0) {
fprintf(stderr, "server failed reply ioctl\n");
return r;
}
#if (!defined(INLINE_TRANSACTION_DATA))
free(reply);
#endif
}
}
free(reply);
return 0;
}
int client_parse_command(int id, unsigned char *buf, unsigned long size, inst_buf_t **pinst)
{
unsigned char *p, *ep;
unsigned int cmd;
tdata_t *tdata;
inst_buf_t *inst = NULL;
#ifdef INLINE_TRANSACTION_DATA
unsigned long buffer_size;
#endif
p = buf;
ep = p + size;
while (p < ep) {
cmd = *(unsigned int *)p;
p += sizeof(cmd);
switch (cmd) {
case BR_NOOP:
case BR_TRANSACTION_COMPLETE:
break;
case BR_INCREFS:
case BR_ACQUIRE:
case BR_RELEASE:
case BR_DECREFS:
fprintf(stderr, "client %d received unexpected ref_cmd command\n", id);
if (p + 2 * sizeof(cmd) > ep) {
fprintf(stderr, "client %d not enough ref_cmd data\n", id);
return -1;
}
p += 2 * sizeof(cmd);
break;
case BR_TRANSACTION:
fprintf(stderr, "client %d received unexpected transaction command\n", id);
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "client %d: not enough transaction data\n", id);
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "client %d: not enough transaction data buffer\n", id);
return -1;
}
p += buffer_size;
#endif
break;
case BR_REPLY:
if (p + sizeof(*tdata) > ep) {
fprintf(stderr, "client %d: not enough transaction data\n", id);
return -1;
}
tdata = (tdata_t *)p;
p += sizeof(*tdata);
#ifdef INLINE_TRANSACTION_DATA
buffer_size = ALIGN(tdata->data_size) + ALIGN(tdata->offsets_size);
if (p + buffer_size > ep) {
fprintf(stderr, "client %d: not enough transaction data buffer\n", id);
return -1;
}
p += buffer_size;
#endif
if (tdata->data_size != sizeof(inst_buf_t)) {
fprintf(stderr, "client %d: data size in reply is incorrect\n", id);
return -1;
}
if (inst) {
fprintf(stderr, "client %d: received multiple reply\n", id);
return -1;
}
inst = (inst_buf_t *)tdata->data.ptr.buffer;
break;
case BR_DEAD_BINDER:
fprintf(stderr, "client %d received unexpected dead_binder command\n", id);
if (p + sizeof(cmd) > ep) {
fprintf(stderr, "client %d: not enough dead binder data\n", id);
return -1;
}
p += sizeof(cmd);
break;
case BR_FAILED_REPLY:
fprintf(stderr, "client %d received unexpected failed_reply command\n", id);
return -1;
case BR_DEAD_REPLY:
fprintf(stderr, "client %d received unexpected dead_reply command\n", id);
return -1;
default:
fprintf(stderr, "client %d received unknown command\n", id);
return -1;
}
}
if (p != ep) {
fprintf(stderr, "client %d receiver buffer has unknown data\n", id);
return -1;
}
*pinst = inst;
return 0;
}
int client_main(void)
{
int fd, r, n, m, wait = 0, retries;
void *binder, *cookie;
bcmd_txn_t *txn;
bwr_t bwr;
inst_buf_t *inst, *inst_reply;
inst_entry_t *entry, copy;
unsigned char rbuf[RBUF_SIZE], *ibuf, *p;
struct timeval ref, delta;
char labels[INST_MAX_ENTRIES][8];
unsigned long long total_usecs[INST_MAX_ENTRIES];
FILE *fp;
if (!share_cpus) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(id + 1, &cpuset);
r = sched_setaffinity(0, sizeof(cpuset), &cpuset);
if (!r)
printf("client %d is bound to CPU %d\n", id, id + 1);
else
fprintf(stderr, "client %d failed to be bound to CPU %d\n", id, id + 1);
}
fd = open("/dev/binder", O_RDWR);
if (fd < 0) {
fprintf(stderr, "client %d failed to open binder device\n", id);
return -1;
}
#if (!defined(INLINE_TRANSACTION_DATA))
if (mmap(NULL, 128 * 1024, PROT_READ, MAP_PRIVATE, fd, 0) == MAP_FAILED) {
fprintf(stderr, "server failed to mmap shared buffer\n");
return -1;
}
#endif
while (1) {
r = lookup_service(fd, service, sizeof(service) / 2, &binder, &cookie);
if (r < 0) {
fprintf(stderr, "client %d failed to find the instrumentation service\n", id);
return -1;
} else if (r > 0)
break;
if (wait++ > 1)
fprintf(stderr, "client %d still waiting on instrumentation service to be ready\n", id);
sleep(1);
}
printf("client %d found instrumentation service\n", id);
txn = create_transaction(0, binder, cookie, 0, NULL, sizeof(inst_buf_t), NULL, 0);
if (!txn) {
fprintf(stderr, "client %d failed to prepare transaction buffer\n", id);
return -1;
}
bwr.write_buffer = (unsigned long)txn;
bwr.read_buffer = (unsigned long)rbuf;
inst = (inst_buf_t *)txn->tdata.data.ptr.buffer;
INST_INIT(inst);
ibuf = malloc((iterations + 1) * sizeof(inst_entry_t) * INST_MAX_ENTRIES);
if (!ibuf)
fprintf(stderr, "client %d failed to allocate instrumentation buffer\n", id);
p = ibuf;
n = iterations + 1;
while (n-- > 0) {
INST_BEGIN(inst);
retries = 2;
bwr.write_size = sizeof(*txn);
bwr.write_consumed = 0;
bwr.read_size = sizeof(rbuf);
bwr.read_consumed = 0;
INST_ENTRY(inst, "C_SEND");
ioctl_write++;
wait_reply:
ioctl_read++;
r = ioctl(fd, BINDER_WRITE_READ, &bwr);
if (r < 0) {
fprintf(stderr, "client %d failed ioctl\n", id);
return r;
}
INST_RECORD(©);
r = client_parse_command(id, rbuf, bwr.read_consumed, &inst_reply);
if (r < 0)
return r;
if (!inst_reply) {
//hexdump(rbuf, bwr.read_consumed);
if (retries-- > 0) {
bwr.write_size = 0;
bwr.read_consumed = 0;
goto wait_reply;
} else {
fprintf(stderr, "client %d failed to receive reply\n", id);
return -1;
}
}
memcpy(inst, inst_reply, sizeof(*inst));
INST_ENTRY_COPY(inst, "C_RECV", ©);
INST_END(inst, &p);
#if (defined(SIMULATE_FREE_BUFFER) || !defined(INLINE_TRANSACTION_DATA))
if (FREE_BUFFER(fd, inst_reply) < 0) {
fprintf(stderr, "client %d: failed to free shared buffer\n", id);
return -1;
}
#endif
}
if (output_file) {
if (clients > 1) {
char *p = malloc(strlen(output_file) + 16);
if (!p) {
fprintf(stderr, "client %d failed to alloc memory for filename\n", id);
return -1;
}
sprintf(p, "%s-%d", output_file, id);
output_file = p;
}
fp = fopen(output_file, "w");
if (!fp) {
fprintf(stderr, "client %d failed to open dump file\n", id);
return -1;
}
} else
fp = stdout;
memset(total_usecs, 0, sizeof(total_usecs));
entry = (inst_entry_t *)ibuf;
for (n = 0; n < inst->seq; n++) {
for (m = 0; m < inst->next_entry; m++) {
if (n > 0) {
if (m == 0) {
if (time_ref == 0) // absolute time
ref.tv_sec = ref.tv_usec = 0;
else
ref = entry->tv;
}
delta.tv_sec = entry->tv.tv_sec - ref.tv_sec;
delta.tv_usec = entry->tv.tv_usec - ref.tv_usec;