forked from adetaylor/strace-android
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenbinder.c
603 lines (577 loc) · 15.6 KB
/
openbinder.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
/*
* Copyright (c) 1991, 1992 Paul Kranenburg <[email protected]>
* Copyright (c) 1993 Branko Lankester <[email protected]>
* Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <[email protected]>
* Copyright (c) 2007 Adrian Taylor <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "defs.h"
#ifdef HAVE_LINUX_BINDER_H
#include <inttypes.h>
#include <linux/types.h>
#include <linux/version.h>
#include <linux/ioctl.h>
#include <linux/binder.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
typedef unsigned int u32;
static int logfh=0;
static int logfhopened = 0;
void logmsg(str)
char* str;
{
if (!logfhopened)
{
logfh = open("/dev/kmsg",O_WRONLY|O_CREAT,0666);
logfhopened = 1;
}
if (logfh)
{
write(logfh,str,strlen(str));
}
}
void dump_bytes(data, size)
char* data;
unsigned long size;
{
int i;
tprintf("[");
for (i=0;i<size;i++) {
tprintf("0x%02x", (u32)data[i]);
if (i<size-1)
tprintf(",");
}
tprintf("] /*");
for (i=0;i<size;i++) {
if (data[i] >= '!' && data[i] <= '~')
tprintf("%c", data[i]);
else
tprintf(".");
}
tprintf(" */");
}
int is_within_offsets(offset,offsets,num_offsets)
size_t offset;
size_t* offsets;
int num_offsets;
{
int i;
int found = 0;
for (i=0;i<num_offsets;i++) {
if (offsets[i] == offset)
found = 1;
}
return found;
}
void dump_flat_obj(flatobj)
struct flat_binder_object* flatobj;
{
tprintf("{type=");
switch (flatobj->type) {
case BINDER_TYPE_BINDER:
tprintf("BINDER_TYPE_BINDER,binder=0x%08x,cookie=0x%08x",(u32)flatobj->binder,(u32)flatobj->cookie);
break;
case BINDER_TYPE_WEAK_BINDER:
tprintf("BINDER_TYPE_WEAK_BINDER,binder=0x%08x,cookie=0x%08x",(u32)flatobj->binder,(u32)flatobj->cookie);
break;
case BINDER_TYPE_HANDLE:
tprintf("BINDER_TYPE_HANDLE,handle=0x%ld",flatobj->handle);
break;
case BINDER_TYPE_WEAK_HANDLE:
tprintf("BINDER_TYPE_WEAK_HANDLE,handle=0x%ld",flatobj->handle);
break;
case BINDER_TYPE_FD:
tprintf("BINDER_TYPE_FD,fd=%ld",flatobj->handle);
break;
}
tprintf("}");
}
void comma()
{
tprintf(",");
}
void dump_bytes_with_offsets(transdata,transdatasize,offsetsdata,offsetsdatasize,startfrom)
char* transdata;
int transdatasize;
size_t* offsetsdata;
int offsetsdatasize;
int startfrom;
{
bool needsComma = false;
tprintf("[");
int numoffsets = offsetsdatasize/sizeof(size_t);
char* currentpointer = transdata + startfrom;
char* endpointer = currentpointer + transdatasize;
char* currentdumpstart = currentpointer;
while (currentpointer < endpointer) {
int thisoffset = currentpointer - transdata;
if (is_within_offsets(thisoffset,offsetsdata,numoffsets)) {
if (currentdumpstart != currentpointer) {
if (needsComma) comma();
tprintf("rawbytes=");
dump_bytes(currentdumpstart,currentpointer-currentdumpstart);
needsComma = true;
}
if (needsComma) comma();
tprintf("objref=");
dump_flat_obj((struct flat_binder_object*) currentpointer);
needsComma = true;
currentpointer += sizeof(struct flat_binder_object);
currentdumpstart = currentpointer;
} else {
currentpointer++;
}
}
if (currentdumpstart != currentpointer) {
if (needsComma) comma();
tprintf("rawbytes=");
dump_bytes(currentdumpstart,currentpointer-currentdumpstart);
needsComma = true;
}
tprintf("]");
}
typedef struct
{
u32 pid;
u32 uid;
} presumed_transaction_contents;
void print_decimal_if_appropriate(number)
u32 number;
{
if (number == 0xffffffff)
tprintf("0x%08x",number);
else
tprintf("%u",number);
}
void dump_transaction_contents(transdata,transdatasize,offsetsdata,offsetsdatasize)
char* transdata;
int transdatasize;
size_t* offsetsdata;
int offsetsdatasize;
{
tprintf("{");
if (transdatasize < sizeof(presumed_transaction_contents))
tprintf("...");
else
{
presumed_transaction_contents* contents = (presumed_transaction_contents*)transdata;
tprintf("pid=");
print_decimal_if_appropriate(contents->pid);
tprintf(",uid=");
print_decimal_if_appropriate(contents->uid);
tprintf(",");
dump_bytes_with_offsets(transdata,transdatasize,offsetsdata,offsetsdatasize,sizeof(presumed_transaction_contents));
}
tprintf("}");
}
void dump_transaction_data(tcp,transaction,is_transaction)
struct tcb *tcp;
struct binder_transaction_data* transaction;
bool is_transaction;
{
if (is_transaction == true)
tprintf("target=0x%08x,cookie=0x%08x,code=0x%08x,",(u32)transaction->target.handle,(u32)transaction->cookie,transaction->code);
tprintf("flags=0x%08x,sender_pid=%d,sender_euid=%d,data_size=%d,offsets_size=%d,data=",transaction->flags,transaction->sender_pid,transaction->sender_euid,transaction->data_size,transaction->offsets_size);
char* transdata;
size_t* offsetsdata;
tprintf("{buffer=0x%08x,offsets=0x%08x,*buffer=",(u32)transaction->data.ptr.buffer,(u32)transaction->data.ptr.offsets);
transdata=malloc(transaction->data_size);
if (transdata != NULL) {
offsetsdata=malloc(transaction->offsets_size);
if (offsetsdata != NULL) {
//if (umoven(tcp,(u32)transaction->data.ptr.buffer,1,transdata) == 0) {
if (umoven(tcp,(u32)transaction->data.ptr.buffer,transaction->data_size,transdata) == 0 && umoven(tcp,(u32)transaction->data.ptr.offsets,transaction->offsets_size,(char*)offsetsdata)==0) {
dump_transaction_contents(transdata,transaction->data_size,offsetsdata,transaction->offsets_size);
//dump_bytes(transdata,transaction->data_size);
//tprintf(",*offsets=");
//dump_bytes(offsetsdata,transaction->offsets_size);
} else
tprintf("[...]");
free(offsetsdata);
}
free(transdata);
}
tprintf("}");
}
void
dump_write_data(tcp, buffer, size)
struct tcb *tcp;
signed long buffer;
unsigned long size;
{
char* data = malloc(size);
if (data != NULL) {
if (umoven(tcp,buffer,size,data) == 0) {
char* location = data;
tprintf("[");
while (location < data+size) {
tprintf("{type=");
u32 cmd;
cmd = *((u32*)location);
location+=sizeof(u32);
switch (cmd) {
case BC_INCREFS:
{
u32 object;
object = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_INCREFS,body={target=0x%08x}",object);
break;
}
case BC_INCREFS_DONE:
{
void* ptr;
void* cookie;
ptr = *((void**)location);
location+=sizeof(void*);
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("BC_INCREFS_DONE,body={ptr=0x%08x,cookie=0x%08x}",(u32)ptr,(u32)cookie);
break;
}
case BC_ACQUIRE:
{
u32 object;
object = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_ACQUIRE,body={target=0x%08x}",object);
break;
}
case BC_ACQUIRE_DONE:
{
void* ptr;
void* cookie;
ptr = *((void**)location);
location+=sizeof(void*);
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("BC_ACQUIRE_DONE,body={ptr=0x%08x,cookie=0x%08x}",(u32)ptr,(u32)cookie);
break;
}
case BC_ATTEMPT_ACQUIRE:
{
u32 priority;
u32 target;
priority = *((u32*)location);
location+=sizeof(u32);
target = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_ATTEMPT_ACQUIRE,body={priority=0x%08x,target=0x%08x}",priority,target);
break;
}
case BC_ACQUIRE_RESULT:
{
u32 result;
result = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_ACQUIRE_RESULT,body={result=0x%08x}",result);
break;
}
case BC_RELEASE:
{
u32 object;
object = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_RELEASE,body={target=0x%08x}",object);
break;
}
case BC_DECREFS:
{
u32 object;
object = *((u32*)location);
location+=sizeof(u32);
tprintf("BC_DECREFS,body={target=0x%08x}",object);
break;
}
case BC_FREE_BUFFER:
{
void* ptr;
ptr = *((void**)location);
location+=sizeof(void*);
tprintf("BC_FREE_BUFFER,body={ptr=0x%08x}",(u32)ptr);
break;
}
case BC_TRANSACTION:
case BC_REPLY:
{
struct binder_transaction_data* transaction;
transaction = ((struct binder_transaction_data*)location);
location += sizeof(struct binder_transaction_data);
if (cmd == BC_TRANSACTION)
tprintf("BC_TRANSACTION,body={");
else
tprintf("BC_REPLY,body={");
dump_transaction_data(tcp,transaction,cmd==BC_TRANSACTION?true:false);
tprintf("}");
break;
}
case BC_REGISTER_LOOPER:
{
tprintf("BC_REGISTER_LOOPER");
break;
}
case BC_ENTER_LOOPER:
{
tprintf("BC_ENTER_LOOPER");
break;
}
case BC_EXIT_LOOPER:
{
tprintf("BC_EXIT_LOOPER");
break;
}
case BC_REQUEST_DEATH_NOTIFICATION:
{
u32 target;
void* cookie;
target = *((u32*)location);
location+=sizeof(u32);
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("BC_REQUEST_DEATH_NOTIFICATION,body={target=0x%08x,cookie=0x%08x}",target,(u32)cookie);
break;
}
case BC_CLEAR_DEATH_NOTIFICATION:
{
u32 target;
void* cookie;
target = *((u32*)location);
location+=sizeof(u32);
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("BC_CLEAR_DEATH_NOTIFICATION,body={target=0x%08x,cookie=0x%08x}",target,(u32)cookie);
break;
}
case BC_DEAD_BINDER_DONE:
{
void* cookie;
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("BC_DEAD_BINDER_DONE,body={cookie=0x%08x}",(u32)cookie);
break;
}
default:
{
tprintf("unknown");
break;
}
}
tprintf("}");
if (location < data+size)
tprintf(",");
}
tprintf("]");
} else
tprintf("{...}");
free(data);
} else
tprintf("{...}");
}
void
dump_read_data(tcp, buffer, size)
struct tcb *tcp;
signed long buffer;
unsigned long size;
{
char* data = malloc(size);
if (data != NULL) {
if (umoven(tcp,buffer,size,data) == 0) {
char* location = data;
tprintf("[");
while (location < data+size) {
tprintf("{type=");
u32 cmd;
cmd = *((u32*)location);
location+=sizeof(u32);
switch (cmd) {
case BR_ERROR:
{
u32 error;
error = *((u32*)location);
location+=sizeof(u32);
tprintf("BR_ERROR,body={error=%d}",error);
break;
}
case BR_OK:
tprintf("BR_OK");
break;
case BR_TRANSACTION:
case BR_REPLY:
{
struct binder_transaction_data* transaction;
transaction = ((struct binder_transaction_data*)location);
location += sizeof(struct binder_transaction_data);
if (cmd == BR_TRANSACTION)
tprintf("BR_TRANSACTION,body={");
else
tprintf("BR_REPLY,body={");
dump_transaction_data(tcp,transaction,cmd==BR_TRANSACTION?true:false);
tprintf("}");
break;
}
case BR_ACQUIRE_RESULT:
{
u32 result;
result = *((u32*)location);
location++;
tprintf("BR_ACQUIRE_RESULT,body={result=%d}",result);
break;
}
case BR_DEAD_REPLY:
tprintf("BR_DEAD_REPLY");
break;
case BR_TRANSACTION_COMPLETE:
tprintf("BR_TRANSACTION_COMPLETE");
break;
case BR_INCREFS:
case BR_ACQUIRE:
case BR_RELEASE:
case BR_DECREFS:
{
void* ptr;
void* cookie;
const char* typestring = (cmd == BR_INCREFS) ? "BR_INCREFS" : (cmd == BR_ACQUIRE) ? "BR_ACQUIRE" : (cmd == BR_RELEASE) ? "BR_RELEASE" : "BR_DECREFS";
ptr = *((void**)location);
location+=sizeof(void*);
cookie = *((void**)location);
location+=sizeof(void*);
tprintf("%s,body={ptr=0x%08x,cookie=0x%08x}",typestring,(u32)ptr,(u32)cookie);
break;
}
case BR_ATTEMPT_ACQUIRE:
{
u32 priority;
void* ptr;
void* cookie;
priority = *((u32*)location);
location++;
ptr = *((void**)location);
location++;
cookie = *((void**)location);
location++;
tprintf("BR_ATTEMPT_ACQUIRE,body={priority=%d,ptr=0x%08x,cookie=0x%08x}",priority,(u32)ptr,(u32)cookie);
break;
}
case BR_NOOP:
tprintf("BR_NOOP");
break;
case BR_SPAWN_LOOPER:
tprintf("BR_SPAWN_LOOPER");
break;
case BR_FINISHED:
tprintf("BR_FINISHED");
break;
case BR_DEAD_BINDER:
case BR_CLEAR_DEATH_NOTIFICATION_DONE:
{
const char* typestring = (cmd == BR_DEAD_BINDER) ? "BR_DEAD_BINDER" : "BR_CLEAR_DEATH_NOTIFICATION_DONE";
void* cookie;
cookie = *((void**)location);
location++;
tprintf("%s,body={cookie=0x%08x}",typestring,(u32)cookie);
break;
}
case BR_FAILED_REPLY:
tprintf("BR_FAILED_REPLY");
break;
default:
tprintf("unknown");
break;
}
tprintf("}");
if (location < data+size)
tprintf(",");
}
tprintf("]");
} else
tprintf("{...}");
free(data);
}
}
int
openbinder_ioctl(tcp, code, arg)
struct tcb *tcp;
long code;
long arg;
{
switch (code) {
case BINDER_WRITE_READ:
if (exiting(tcp)) {
struct binder_write_read wk;
if (syserror(tcp) || umove(tcp,arg,&wk) < 0)
tprintf(", %#lx", arg);
else {
tprintf(", {write_size=%ld,write_consumed=%ld,write_buffer=0x%lx,read_size=%ld,read_consumed=%ld,read_buffer=0x%lx",wk.write_size,wk.write_consumed,wk.write_buffer,wk.read_size,wk.read_consumed,wk.read_buffer);
tprintf(",write_data=");
dump_write_data(tcp,wk.write_buffer,wk.write_size);
tprintf(",read_data=");
dump_read_data(tcp,wk.read_buffer,wk.read_consumed);
tprintf("}");
}
}
break;
case BINDER_SET_IDLE_TIMEOUT:
tprintf(", ...");
break;
case BINDER_SET_MAX_THREADS:
if (exiting(tcp)) {
size_t wk;
if (syserror(tcp) || umove(tcp,arg,&wk) < 0)
tprintf(", %#lx", arg);
else {
tprintf(", %d",(int)wk);
}
}
break;
case BINDER_SET_IDLE_PRIORITY:
case BINDER_SET_CONTEXT_MGR:
case BINDER_THREAD_EXIT:
if (exiting(tcp)) {
int wk;
if (syserror(tcp) || umove(tcp,arg,&wk) < 0)
tprintf(", %#lx", arg);
else {
tprintf(", %d",wk);
}
}
break;
case BINDER_VERSION:
if (exiting(tcp)) {
struct binder_version wk;
if (syserror(tcp) || umove(tcp,arg,&wk) < 0)
tprintf(", %#lx", arg);
else {
tprintf(", {protocol_version=%ld}",wk.protocol_version);
}
}
break;
default:
break;
}
return 1;
}
#endif