1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.ddm;
18 
19 import org.apache.harmony.dalvik.ddmc.Chunk;
20 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21 import org.apache.harmony.dalvik.ddmc.DdmServer;
22 import android.util.Log;
23 
24 /**
25  * Handle thread-related traffic.
26  */
27 public class DdmHandleNativeHeap extends ChunkHandler {
28 
29     public static final int CHUNK_NHGT = type("NHGT");
30 
31     private static DdmHandleNativeHeap mInstance = new DdmHandleNativeHeap();
32 
33 
34     /* singleton, do not instantiate */
DdmHandleNativeHeap()35     private DdmHandleNativeHeap() {}
36 
37     /**
38      * Register for the messages we're interested in.
39      */
register()40     public static void register() {
41         DdmServer.registerHandler(CHUNK_NHGT, mInstance);
42     }
43 
44     /**
45      * Called when the DDM server connects.  The handler is allowed to
46      * send messages to the server.
47      */
connected()48     public void connected() {}
49 
50     /**
51      * Called when the DDM server disconnects.  Can be used to disable
52      * periodic transmissions or clean up saved state.
53      */
disconnected()54     public void disconnected() {}
55 
56     /**
57      * Handle a chunk of data.
58      */
handleChunk(Chunk request)59     public Chunk handleChunk(Chunk request) {
60         Log.i("ddm-nativeheap", "Handling " + name(request.type) + " chunk");
61         int type = request.type;
62 
63         if (type == CHUNK_NHGT) {
64             return handleNHGT(request);
65         } else {
66             throw new RuntimeException("Unknown packet "
67                 + ChunkHandler.name(type));
68         }
69     }
70 
71     /*
72      * Handle a "Native Heap GeT" request.
73      */
handleNHGT(Chunk request)74     private Chunk handleNHGT(Chunk request) {
75         //ByteBuffer in = wrapChunk(request);
76 
77         byte[] data = getLeakInfo();
78 
79         if (data != null) {
80             // wrap & return
81             Log.i("ddm-nativeheap", "Sending " + data.length + " bytes");
82             return new Chunk(ChunkHandler.type("NHGT"), data, 0, data.length);
83         } else {
84             // failed, return a failure error code and message
85             return createFailChunk(1, "Something went wrong");
86         }
87     }
88 
getLeakInfo()89     private native byte[] getLeakInfo();
90 }
91 
92