1/*
2 * Copyright (C) 2008 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/*
18 * org.apache.harmony.dalvik.ddmc.DdmVmInternal
19 */
20#include "Dalvik.h"
21#include "native/InternalNativePriv.h"
22
23
24/*
25 * public static void threadNotify(boolean enable)
26 *
27 * Enable DDM thread notifications.
28 */
29static void Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_threadNotify(
30    const u4* args, JValue* pResult)
31{
32    bool enable = (args[0] != 0);
33
34    //ALOGI("ddmThreadNotification: %d", enable);
35    dvmDdmSetThreadNotification(enable);
36    RETURN_VOID();
37}
38
39/*
40 * public static byte[] getThreadStats()
41 *
42 * Get a buffer full of thread info.
43 */
44static void Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getThreadStats(
45    const u4* args, JValue* pResult)
46{
47    UNUSED_PARAMETER(args);
48
49    ArrayObject* result = dvmDdmGenerateThreadStats();
50    dvmReleaseTrackedAlloc((Object*) result, NULL);
51    RETURN_PTR(result);
52}
53
54/*
55 * public static int heapInfoNotify(int what)
56 *
57 * Enable DDM heap notifications.
58 */
59static void Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_heapInfoNotify(
60    const u4* args, JValue* pResult)
61{
62    int when = args[0];
63    bool ret;
64
65    ret = dvmDdmHandleHpifChunk(when);
66    RETURN_BOOLEAN(ret);
67}
68
69/*
70 * public static boolean heapSegmentNotify(int when, int what, bool native)
71 *
72 * Enable DDM heap notifications.
73 */
74static void
75    Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_heapSegmentNotify(
76    const u4* args, JValue* pResult)
77{
78    int  when   = args[0];        // 0=never (off), 1=during GC
79    int  what   = args[1];        // 0=merged objects, 1=distinct objects
80    bool native = (args[2] != 0); // false=virtual heap, true=native heap
81    bool ret;
82
83    ret = dvmDdmHandleHpsgNhsgChunk(when, what, native);
84    RETURN_BOOLEAN(ret);
85}
86
87/*
88 * public static StackTraceElement[] getStackTraceById(int threadId)
89 *
90 * Get a stack trace as an array of StackTraceElement objects.  Returns
91 * NULL on failure, e.g. if the threadId couldn't be found.
92 */
93static void
94    Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getStackTraceById(
95    const u4* args, JValue* pResult)
96{
97    u4 threadId = args[0];
98    ArrayObject* trace;
99
100    trace = dvmDdmGetStackTraceById(threadId);
101    RETURN_PTR(trace);
102}
103
104/*
105 * public static void enableRecentAllocations(boolean enable)
106 *
107 * Enable or disable recent allocation tracking.
108 */
109static void
110    Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_enableRecentAllocations(
111    const u4* args, JValue* pResult)
112{
113    bool enable = (args[0] != 0);
114
115    if (enable)
116        (void) dvmEnableAllocTracker();
117    else
118        (void) dvmDisableAllocTracker();
119    RETURN_VOID();
120}
121
122/*
123 * public static boolean getRecentAllocationStatus()
124 *
125 * Returns "true" if allocation tracking is enabled.
126 */
127static void
128    Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getRecentAllocationStatus(
129    const u4* args, JValue* pResult)
130{
131    UNUSED_PARAMETER(args);
132    RETURN_BOOLEAN(gDvm.allocRecords != NULL);
133}
134
135/*
136 * public static byte[] getRecentAllocations()
137 *
138 * Fill a buffer with data on recent heap allocations.
139 */
140static void
141    Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getRecentAllocations(
142    const u4* args, JValue* pResult)
143{
144    ArrayObject* data;
145
146    data = dvmDdmGetRecentAllocations();
147    dvmReleaseTrackedAlloc((Object*) data, NULL);
148    RETURN_PTR(data);
149}
150
151const DalvikNativeMethod dvm_org_apache_harmony_dalvik_ddmc_DdmVmInternal[] = {
152    { "threadNotify",       "(Z)V",
153      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_threadNotify },
154    { "getThreadStats",     "()[B",
155      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getThreadStats },
156    { "heapInfoNotify",     "(I)Z",
157      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_heapInfoNotify },
158    { "heapSegmentNotify",  "(IIZ)Z",
159      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_heapSegmentNotify },
160    { "getStackTraceById",  "(I)[Ljava/lang/StackTraceElement;",
161      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getStackTraceById },
162    { "enableRecentAllocations", "(Z)V",
163      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_enableRecentAllocations },
164    { "getRecentAllocationStatus", "()Z",
165      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getRecentAllocationStatus },
166    { "getRecentAllocations", "()[B",
167      Dalvik_org_apache_harmony_dalvik_ddmc_DdmVmInternal_getRecentAllocations },
168    { NULL, NULL, NULL },
169};
170