1 #include "../../IAndroidShm.h"
2 #include <binder/MemoryHeapBase.h>
3 #include <binder/IServiceManager.h>
4 #include "../../../common/shm.h"
5 
6 namespace android {
7 
8 static sp<IMemoryHeap> receiverMemBase;
9 #define MAX_SHARED_MEMORY_COUNT 257
10 
getAndroidShmService()11 sp<IAndroidShm> getAndroidShmService() {
12   sp<IAndroidShm> shm = 0;
13 
14   /* Get the buffer service */
15   if (shm == NULL) {
16     sp<IServiceManager> sm = defaultServiceManager();
17     sp<IBinder> binder;
18     binder = sm->getService(String16("com.samsung.android.jam.IAndroidShm"));
19     if (binder != 0) {
20         shm = IAndroidShm::asInterface(binder);
21         //shm = interface_cast<IAndroidShm>(binder);
22     }
23   }
24   return shm;
25 }
26 
getBufferMemPointer(int index)27 unsigned int * getBufferMemPointer(int index)
28 {
29   sp<IAndroidShm> shm = getAndroidShmService();
30   if (shm == NULL) {
31     printf("The EneaBufferServer is not published\n");
32     return (unsigned int *)-1; /* return an errorcode... */
33   } else {
34     receiverMemBase = shm->getBuffer(index);
35     if(receiverMemBase != NULL)
36         return (unsigned int *) receiverMemBase->getBase();
37     else
38         return (unsigned int*)-1;
39   }
40 }
41 
42 }
43 
44 using namespace android;
45 
showStatus()46 void showStatus() {
47     sp<IAndroidShm> shm = getAndroidShmService();
48     if(shm == NULL) {
49         printf("shm service is not available\n");
50         return;
51     }
52 
53     printf("<<<<<<<<<<< dump memory allocation status >>>>>>>>>>\n");
54     for(int i = 256; i >= 0; i--) {
55         if(shm->isAllocated(i) == 1) {
56             printf("Mem[%3d] == 0x%x\n", i, (unsigned int)getBufferMemPointer(i));
57         } else {
58             printf("Mem[%3d] == NULL\n", i);
59         }
60     }
61 }
62 
showRegistryIndex()63 void showRegistryIndex() {
64     sp<IAndroidShm> shm = getAndroidShmService();
65 
66     if(shm == NULL) {
67         printf("shm service is not available\n");
68         return;
69     }
70 
71     printf("<<<<<<<<<<< show registry index >>>>>>>>>>\n");
72 
73     printf("index [%3d]\n",shm->getRegistryIndex());
74 }
75 
showHeader()76 void showHeader() {
77     sp<IAndroidShm> shm = getAndroidShmService();
78 
79     if(shm == NULL) {
80         printf("shm service is not available\n");
81         return;
82     }
83 
84     if(shm->getRegistryIndex() > 256) {
85         printf("don't have a registry header\n");
86         return;
87     }
88 
89     unsigned int* buffer = getBufferMemPointer(shm->getRegistryIndex());
90     if(buffer) {
91         jack_shm_header_t * header = (jack_shm_header_t*)buffer;
92         printf("<<<<<<<<<<  register header value  >>>>>>>>>>\n");
93         printf("memory address 0x%x 0x%x\n", (unsigned int)(header), (unsigned int)buffer);
94         printf("magic = %d\n", header->magic);
95         printf("protocol = %d\n", header->protocol);
96         printf("type = %d\n", header->type);
97         printf("size = %d\n", header->size);
98         printf("hdr_len = %d\n", header->hdr_len);
99         printf("entry_len = %d\n", header->entry_len);
100         for(int j = 0; j < MAX_SERVERS; j++) {
101             //char name[256];
102             //memset(name, '\0', 256);
103             //strncpy(name, header->server[j].name, 10);
104             printf("server[%d] pid = %d, name = %s\n", j, header->server[j].pid, header->server[j].name);
105         }
106     }
107 }
108 
showBody()109 void showBody() {
110     sp<IAndroidShm> shm = getAndroidShmService();
111 
112     if(shm == NULL) {
113         printf("shm service is not available\n");
114         return;
115     }
116 
117     if(shm->getRegistryIndex() > 256) {
118         printf("don't have a registry body\n");
119         return;
120     }
121     unsigned int* buffer = getBufferMemPointer(shm->getRegistryIndex());
122     if(buffer) {
123         jack_shm_header_t * header = (jack_shm_header_t*)buffer;
124         printf("<<<<<<<<<< registry body value >>>>>>>>>>\n");
125         jack_shm_registry_t * registry = (jack_shm_registry_t *) (header + 1);
126         for(int k = 255; k >= 0; k--) {
127             printf("registry[%3d] index[%3d],allocator[%3d],size[%6d],id[%10s],fd[%3d]\n", k,
128                 registry[k].index, registry[k].allocator, registry[k].size,
129                 registry[k].id,
130                 registry[k].fd);
131         }
132     }
133 }
134 
showSemaphore()135 void showSemaphore() {
136     sp<IAndroidShm> shm = getAndroidShmService();
137 
138     if(shm == NULL) {
139         printf("shm service is not available\n");
140         return;
141     }
142 
143     shm->sendCommand("semaphore");
144     printf("log will be shown in the logcat log\n");
145 }
146 
main(int argc,char ** argv)147 int main(int argc, char** argv) {
148     // base could be on same address as Servers base but this
149     // is purely by luck do NEVER rely on this. Linux memory
150     // management may put it wherever it likes.
151 
152     if(argc < 2) {
153         printf("usage\n shmservicedump [status|header|body|index|semaphore]\n");
154         printf(" status: show the shared memory allocation status\n");
155         printf(" header: show the registry header infomations if the registry exist\n");
156         printf(" body: show the registry body infomations if the registry exist\n");
157         printf(" index: show the index of array that is allocated registry shared memory\n");
158         printf(" semaphore: show the memory array about semaphore simulation\n");
159         return 0;
160     }
161 
162     if(strcmp(argv[1], "semaphore") == 0) {
163         showSemaphore();
164     } else if(strcmp(argv[1], "index") == 0) {
165         showRegistryIndex();
166     } else if(strcmp(argv[1], "status") == 0) {
167         showStatus();
168     } else if(strcmp(argv[1], "header") == 0) {
169         showHeader();
170     } else if(strcmp(argv[1], "body") == 0) {
171         showBody();
172     } else {
173         printf("%s is invalid parameter\n", argv[1]);
174     }
175 
176     return 0;
177 }
178