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 dalvik.system;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 import android.annotation.UserIdInt;
23 import android.compat.annotation.UnsupportedAppUsage;
24 
25 import libcore.util.NonNull;
26 
27 import java.io.FileDescriptor;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 import dalvik.annotation.optimization.FastNative;
33 
34 /**
35  * Provides access to some VM-specific debug features. Though this class and
36  * many of its members are public, this class is meant to be wrapped in a more
37  * friendly way for use by application developers. On the Android platform, the
38  * recommended way to access this functionality is through the class
39  * <code>android.os.Debug</code>.
40  *
41  * @hide
42  */
43 @SystemApi(client = MODULE_LIBRARIES)
44 public final class VMDebug {
45     /**
46      * flag for startMethodTracing(), which adds the results from
47      * startAllocCounting to the trace key file.
48      *
49      * @hide
50      */
51     @SystemApi(client = MODULE_LIBRARIES)
52     // Must match android.os.Debug.TRACE_COUNT_ALLOCS.
53     public static final int TRACE_COUNT_ALLOCS = 1;
54 
55     /* constants for getAllocCount */
56     private static final int KIND_ALLOCATED_OBJECTS     = 1<<0;
57     private static final int KIND_ALLOCATED_BYTES       = 1<<1;
58     private static final int KIND_FREED_OBJECTS         = 1<<2;
59     private static final int KIND_FREED_BYTES           = 1<<3;
60     private static final int KIND_GC_INVOCATIONS        = 1<<4;
61     private static final int KIND_CLASS_INIT_COUNT      = 1<<5;
62     private static final int KIND_CLASS_INIT_TIME       = 1<<6;
63     private static final int KIND_EXT_ALLOCATED_OBJECTS = 1<<12;
64     private static final int KIND_EXT_ALLOCATED_BYTES   = 1<<13;
65     private static final int KIND_EXT_FREED_OBJECTS     = 1<<14;
66     private static final int KIND_EXT_FREED_BYTES       = 1<<15;
67 
68     /**
69      * Constant for {@link #getAllocCount(int)}
70      * to get the number of all allocated objects.
71      *
72      * @hide
73      */
74     @SystemApi(client = MODULE_LIBRARIES)
75     public static final int KIND_GLOBAL_ALLOCATED_OBJECTS =
76         KIND_ALLOCATED_OBJECTS;
77 
78     /**
79      * Constant for {@link #getAllocCount(int)}
80      * to get the cumulative size of all objects allocated.
81      *
82      * @hide
83      */
84     @SystemApi(client = MODULE_LIBRARIES)
85     public static final int KIND_GLOBAL_ALLOCATED_BYTES =
86         KIND_ALLOCATED_BYTES;
87 
88     /**
89      * Constant for {@link #getAllocCount(int)}
90      * to get the number of freed objects.
91      *
92      * @hide
93      */
94     @SystemApi(client = MODULE_LIBRARIES)
95     public static final int KIND_GLOBAL_FREED_OBJECTS =
96         KIND_FREED_OBJECTS;
97 
98     /**
99      * Constant for {@link #getAllocCount(int)}
100      * to get the cumulative size of all freed objects.
101      *
102      * @hide
103      */
104     @SystemApi(client = MODULE_LIBRARIES)
105     public static final int KIND_GLOBAL_FREED_BYTES =
106         KIND_FREED_BYTES;
107 
108     /**
109      * Constant for {@link #getAllocCount(int)}
110      * to get the number of times an allocation triggered a blocking GC.
111      *
112      * @hide
113      */
114     @SystemApi(client = MODULE_LIBRARIES)
115     public static final int KIND_GLOBAL_GC_INVOCATIONS =
116         KIND_GC_INVOCATIONS;
117 
118     /**
119      * Constant for {@link #getAllocCount(int)}
120      * to get the number of initialized classes.
121      *
122      * @hide
123      */
124     @SystemApi(client = MODULE_LIBRARIES)
125     public static final int KIND_GLOBAL_CLASS_INIT_COUNT =
126         KIND_CLASS_INIT_COUNT;
127 
128     /**
129      * Constant for {@link #getAllocCount(int)}
130      * to get the cumulative time spent in class initialization.
131      *
132      * @hide
133      */
134     @SystemApi(client = MODULE_LIBRARIES)
135     public static final int KIND_GLOBAL_CLASS_INIT_TIME =
136         KIND_CLASS_INIT_TIME;
137 
138     /**
139      * Constant for {@link #getAllocCount(int)}
140      * to get the number of all allocated objects for current thread.
141      *
142      * @hide
143      */
144     @SystemApi(client = MODULE_LIBRARIES)
145     public static final int KIND_THREAD_ALLOCATED_OBJECTS =
146         KIND_ALLOCATED_OBJECTS << 16;
147 
148     /**
149      * Constant for {@link #getAllocCount(int)}
150      * to get the cumulative size of all objects allocated for current thread.
151      *
152      * @hide
153      */
154     @SystemApi(client = MODULE_LIBRARIES)
155     public static final int KIND_THREAD_ALLOCATED_BYTES =
156         KIND_ALLOCATED_BYTES << 16;
157 
158     /**
159      * Constant for {@link #getAllocCount(int)}
160      * to get the number of times an allocation triggered a blocking GC for current thread.
161      *
162      * @hide
163      */
164     @SystemApi(client = MODULE_LIBRARIES)
165     public static final int KIND_THREAD_GC_INVOCATIONS =
166         KIND_GC_INVOCATIONS << 16;
167 
168     /**
169      * Constant for {@link #getAllocCount(int)} to get all possible stats.
170      *
171      * @hide
172      */
173     @SystemApi(client = MODULE_LIBRARIES)
174     public static final int KIND_ALL_COUNTS = 0xffffffff;
175 
176     /* all methods are static */
VMDebug()177     private VMDebug() {}
178 
179     /**
180      * Request JDWP agent to suspend all Java Thread and send VM_START.
181      *
182      * @hide
183      */
184     @SystemApi(client = MODULE_LIBRARIES)
suspendAllAndSendVmStart()185     public static native void suspendAllAndSendVmStart();
186 
187     /**
188      * Returns the time since the last known debugger activity.
189      *
190      * @return the time in milliseconds, or -1 if the debugger is not connected
191      *
192      * @hide
193      */
194     @SystemApi(client = MODULE_LIBRARIES)
195     @FastNative
lastDebuggerActivity()196     public static native long lastDebuggerActivity();
197 
198     /**
199      * Determines if debugging is enabled in this VM.  If debugging is not
200      * enabled, a debugger cannot be attached.
201      *
202      * @return true if debugging is enabled
203      *
204      * @hide
205      */
206     @SystemApi(client = MODULE_LIBRARIES)
207     @FastNative
isDebuggingEnabled()208     public static native boolean isDebuggingEnabled();
209 
210     /**
211      * Determines if a debugger is currently attached.
212      *
213      * @return true if (and only if) a debugger is connected
214      *
215      * @hide
216      */
217     @UnsupportedAppUsage
218     @SystemApi(client = MODULE_LIBRARIES)
219     @FastNative
isDebuggerConnected()220     public static native boolean isDebuggerConnected();
221 
222     /**
223      * Returns an array of strings that identify VM features.  This is
224      * used by DDMS to determine what sorts of operations the VM can
225      * perform.
226      *
227      * @return array of strings identifying VM features
228      *
229      * @hide
230      */
231     @SystemApi(client = MODULE_LIBRARIES)
getVmFeatureList()232     public static native String[] getVmFeatureList();
233 
234     /**
235      * Start method tracing, specifying a file name as well as a default
236      * buffer size. See <a
237      * href="{@docRoot}guide/developing/tools/traceview.html"> Running the
238      * Traceview Debugging Program</a> for information about reading
239      * trace files.
240      *
241      * <p>You can use either a fully qualified path and
242      * name, or just a name. If only a name is specified, the file will
243      * be created under the /sdcard/ directory. If a name is not given,
244      * the default is /sdcard/dmtrace.trace.</p>
245      *
246      * @param traceFileName   name to give the trace file
247      * @param bufferSize      the maximum size of both files combined. If passed
248      *                        as {@code 0}, it defaults to 8MB.
249      * @param flags           flags to control method tracing. The only one that
250      *                        is currently defined is {@link #TRACE_COUNT_ALLOCS}.
251      * @param samplingEnabled if true, sample profiling is enabled. Otherwise,
252      *                        method instrumentation is used.
253      * @param intervalUs      the time between samples in microseconds when
254      *                        sampling is enabled.
255      *
256      * @hide
257      */
258     @SystemApi(client = MODULE_LIBRARIES)
startMethodTracing(String traceFileName, int bufferSize, int flags, boolean samplingEnabled, int intervalUs)259     public static void startMethodTracing(String traceFileName, int bufferSize, int flags, boolean samplingEnabled, int intervalUs) {
260         startMethodTracingFilename(traceFileName, checkBufferSize(bufferSize), flags, samplingEnabled, intervalUs);
261     }
262 
263     /**
264      * Like {@link #startMethodTracing(String, int, int)}, but taking an already-opened
265      * {@code FileDescriptor} in which the trace is written.  The file name is also
266      * supplied simply for logging.  Makes a dup of the file descriptor.
267      * Streams tracing data to the file if streamingOutput is true.
268      *
269      * @param traceFileName   name to give the trace file
270      * @param fd              already opened {@code FileDescriptor} in which trace is written
271      * @param bufferSize      the maximum size of both files combined. If passed
272      *                        as {@code 0}, it defaults to 8MB.
273      * @param flags           flags to control method tracing. The only one that
274      *                        is currently defined is {@link #TRACE_COUNT_ALLOCS}.
275      * @param samplingEnabled if true, sample profiling is enabled. Otherwise,
276      *                        method instrumentation is used.
277      * @param intervalUs      the time between samples in microseconds when
278      *                        sampling is enabled.
279      * @param streamingOutput streams tracing data to the duped {@code fd} file descriptor
280      *                        if {@code streamingOutput} is {@code true}.
281      *
282      * @hide
283      */
284     @SystemApi(client = MODULE_LIBRARIES)
startMethodTracing(String traceFileName, FileDescriptor fd, int bufferSize, int flags, boolean samplingEnabled, int intervalUs, boolean streamingOutput)285     public static void startMethodTracing(String traceFileName, FileDescriptor fd, int bufferSize,
286                                           int flags, boolean samplingEnabled, int intervalUs,
287                                           boolean streamingOutput) {
288         if (fd == null) {
289             throw new NullPointerException("fd == null");
290         }
291         startMethodTracingFd(traceFileName, fd.getInt$(), checkBufferSize(bufferSize), flags,
292                              samplingEnabled, intervalUs, streamingOutput);
293     }
294 
295     /**
296      * Starts method tracing without a backing file.  When {@link #stopMethodTracing()}
297      * is called, the result is sent directly to DDMS.  (If DDMS is not
298      * attached when tracing ends, the profiling data will be discarded.)
299      *
300      * @param bufferSize      the maximum size of both files combined. If passed
301      *                        as {@code 0}, it defaults to 8MB.
302      * @param flags           flags to control method tracing. The only one that
303      *                        is currently defined is {@link #TRACE_COUNT_ALLOCS}.
304      * @param samplingEnabled if true, sample profiling is enabled. Otherwise,
305      *                        method instrumentation is used.
306      * @param intervalUs      the time between samples in microseconds when
307      *                        sampling is enabled.
308      *
309      * @hide
310      */
311     @SystemApi(client = MODULE_LIBRARIES)
startMethodTracingDdms(int bufferSize, int flags, boolean samplingEnabled, int intervalUs)312     public static void startMethodTracingDdms(int bufferSize, int flags, boolean samplingEnabled, int intervalUs) {
313         startMethodTracingDdmsImpl(checkBufferSize(bufferSize), flags, samplingEnabled, intervalUs);
314     }
315 
checkBufferSize(int bufferSize)316     private static int checkBufferSize(int bufferSize) {
317         if (bufferSize == 0) {
318             // Default to 8MB per the documentation.
319             bufferSize = 8 * 1024 * 1024;
320         }
321         if (bufferSize < 1024) {
322             throw new IllegalArgumentException("buffer size < 1024: " + bufferSize);
323         }
324         return bufferSize;
325     }
326 
startMethodTracingDdmsImpl(int bufferSize, int flags, boolean samplingEnabled, int intervalUs)327     private static native void startMethodTracingDdmsImpl(int bufferSize, int flags, boolean samplingEnabled, int intervalUs);
startMethodTracingFd(String traceFileName, int fd, int bufferSize, int flags, boolean samplingEnabled, int intervalUs, boolean streamingOutput)328     private static native void startMethodTracingFd(String traceFileName, int fd, int bufferSize,
329             int flags, boolean samplingEnabled, int intervalUs, boolean streamingOutput);
startMethodTracingFilename(String traceFileName, int bufferSize, int flags, boolean samplingEnabled, int intervalUs)330     private static native void startMethodTracingFilename(String traceFileName, int bufferSize, int flags, boolean samplingEnabled, int intervalUs);
331 
332     /**
333      * Determine whether method tracing is currently active and what type is
334      * active.
335      *
336      * @hide
337      */
338     @SystemApi(client = MODULE_LIBRARIES)
getMethodTracingMode()339     public static native int getMethodTracingMode();
340 
341     /**
342      * Stops method tracing.
343      *
344      * @hide
345      */
346     @SystemApi(client = MODULE_LIBRARIES)
stopMethodTracing()347     public static native void stopMethodTracing();
348 
349     /**
350      * Get an indication of thread CPU usage. The value returned indicates the
351      * amount of time that the current thread has spent executing code or
352      * waiting for certain types of I/O.
353      * <p>
354      * The time is expressed in nanoseconds, and is only meaningful when
355      * compared to the result from an earlier call. Note that nanosecond
356      * resolution does not imply nanosecond accuracy.
357      *
358      * @return the CPU usage. A value of -1 means the system does not support
359      *         this feature.
360      *
361      * @hide
362      */
363     @SystemApi(client = MODULE_LIBRARIES)
364     @FastNative
threadCpuTimeNanos()365     public static native long threadCpuTimeNanos();
366 
367     /**
368      * Starts counting the number and aggregate size of memory allocations.
369      *
370      * @hide
371      */
372     @SystemApi(client = MODULE_LIBRARIES)
startAllocCounting()373     public static native void startAllocCounting();
374 
375     /**
376      * Stops counting the number and aggregate size of memory allocations.
377      *
378      * @hide
379      */
380     @SystemApi(client = MODULE_LIBRARIES)
stopAllocCounting()381     public static native void stopAllocCounting();
382 
383     /**
384      * Returns information on the number of objects allocated by the runtime between a
385      * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
386      *
387      * @param kind either {@code KIND_GLOBAL_*} or {@code KIND_THREAD_*}.
388      *
389      * @hide
390      */
391     @SystemApi(client = MODULE_LIBRARIES)
getAllocCount(int kind)392     public static native int getAllocCount(int kind);
393 
394     /**
395      * Resets counting the number and aggregate size of memory allocations for the given kinds.
396      *
397      * @param kinds a union of {@code KIND_GLOBAL_*} and {@code KIND_THREAD_*}.
398      *
399      * @hide
400      */
401     @SystemApi(client = MODULE_LIBRARIES)
resetAllocCount(int kinds)402     public static native void resetAllocCount(int kinds);
403 
404     /**
405      * This method exists for binary compatibility.  It was part of
406      * the allocation limits API which was removed in Android 3.0 (Honeycomb).
407      *
408      * @hide
409      */
410     @Deprecated
setAllocationLimit(int limit)411     public static int setAllocationLimit(int limit) {
412         return -1;
413     }
414 
415     /**
416      * This method exists for binary compatibility.  It was part of
417      * the allocation limits API which was removed in Android 3.0 (Honeycomb).
418      *
419      * @hide
420      */
421     @Deprecated
setGlobalAllocationLimit(int limit)422     public static int setGlobalAllocationLimit(int limit) {
423         return -1;
424     }
425 
426     /**
427      * Count the number of instructions executed between two points.
428      *
429      * @hide
430      */
431     @Deprecated
startInstructionCounting()432     public static void startInstructionCounting() {}
433 
434     /**
435      *
436      * @hide
437      */
438     @Deprecated
stopInstructionCounting()439     public static void stopInstructionCounting() {}
440 
441     /**
442      *
443      * @hide
444      */
445     @Deprecated
getInstructionCount(int[] counts)446     public static void getInstructionCount(int[] counts) {}
447 
448     /**
449      *
450      * @hide
451      */
452     @Deprecated
resetInstructionCount()453     public static void resetInstructionCount() {}
454 
455     /**
456      * Dumps a list of loaded class to the log file.
457      *
458      * @param flags a union of {@link android.os.Debug.SHOW_FULL_DETAIL},
459      *    {@link android.os.Debug.SHOW_CLASSLOADER}, and {@link android.os.Debug.SHOW_INITIALIZED}.
460      *
461      * @hide
462      */
463     @SystemApi(client = MODULE_LIBRARIES)
464     @FastNative
printLoadedClasses(int flags)465     public static native void printLoadedClasses(int flags);
466 
467     /**
468      * Gets the number of loaded classes.
469      *
470      * @return the number of loaded classes
471      *
472      * @hide
473      */
474     @SystemApi(client = MODULE_LIBRARIES)
475     @FastNative
getLoadedClassCount()476     public static native int getLoadedClassCount();
477 
478     /**
479      * Dumps "hprof" data to the specified file.  This may cause a GC.
480      *
481      * The VM may create a temporary file in the same directory.
482      *
483      * @param filename Full pathname of output file (e.g. "/sdcard/dump.hprof").
484      * @throws UnsupportedOperationException if the VM was built without
485      *         HPROF support.
486      * @throws IOException if an error occurs while opening or writing files.
487      *
488      * @hide
489      */
490     @SystemApi(client = MODULE_LIBRARIES)
dumpHprofData(String filename)491     public static void dumpHprofData(String filename) throws IOException {
492         if (filename == null) {
493             throw new NullPointerException("filename == null");
494         }
495         dumpHprofData(filename, null);
496     }
497 
498     /**
499      * Collects "hprof" heap data and sends it to DDMS.  This may cause a GC.
500      *
501      * @throws UnsupportedOperationException if the VM was built without
502      *         HPROF support.
503      *
504      * @hide
505      */
506     @SystemApi(client = MODULE_LIBRARIES)
dumpHprofDataDdms()507     public static native void dumpHprofDataDdms();
508 
509     /**
510      * Dumps "hprof" heap data to a file, by name or descriptor.
511      *
512      * @param fileName Name of output file.  If fd is non-null, the
513      *        file name is only used in log messages (and may be null).
514      * @param fd Descriptor of open file that will receive the output.
515      *        If this is null, the fileName is used instead.
516      * @throws {@link IOException} if an error occurs while opening or writing files.
517      *
518      * @hide
519      */
520     @SystemApi(client = MODULE_LIBRARIES)
dumpHprofData(String fileName, FileDescriptor fd)521     public static void dumpHprofData(String fileName, FileDescriptor fd)
522             throws IOException {
523        dumpHprofData(fileName, fd != null ? fd.getInt$() : -1);
524     }
525 
dumpHprofData(String fileName, int fd)526     private static native void dumpHprofData(String fileName, int fd)
527             throws IOException;
528 
529     /**
530      * Dumps the contents of the VM reference tables (e.g. JNI locals and
531      * globals) to the log file.
532      *
533      * @hide
534      */
535     @UnsupportedAppUsage
536     @SystemApi(client = MODULE_LIBRARIES)
dumpReferenceTables()537     public static native void dumpReferenceTables();
538 
539     /**
540      * Counts the instances of a class.
541      * It is the caller's responsibility to do GC if they don't want unreachable
542      * objects to get counted.
543      *
544      * @param klass the class to be counted.
545      * @param assignable if true, any instance whose class is assignable to
546      *                   {@code klass}, as defined by {@link Class#isAssignableFrom},
547      *                   is counted. If false, only instances whose class is
548      *                   equal to {@code klass} are counted.
549      * @return the number of matching instances.
550      *
551      * @hide
552      */
553     @SystemApi(client = MODULE_LIBRARIES)
countInstancesOfClass(Class klass, boolean assignable)554     public static native long countInstancesOfClass(Class klass, boolean assignable);
555 
556     /**
557      * Counts the instances of classes.
558      * It is the caller's responsibility to do GC if they don't want unreachable
559      * objects to get counted.
560      *
561      * @param classes the classes to be counted.
562      * @param assignable if true, any instance whose class is assignable to
563      *                   {@code classes[i]}, as defined by {@link Class#isAssignableFrom},
564      *                   is counted. If false, only instances whose class is
565      *                   equal to {@code classes[i]} are counted.
566      * @return an array containing the number of matching instances. The value
567      *         for index {@code i} is the number of instances of
568      *         the class {@code classes[i]}
569      *
570      * @hide
571      */
572     @SystemApi(client = MODULE_LIBRARIES)
countInstancesOfClasses(Class[] classes, boolean assignable)573     public static native long[] countInstancesOfClasses(Class[] classes, boolean assignable);
574 
575     /* Map from the names of the runtime stats supported by getRuntimeStat() to their IDs */
576     private static final HashMap<String, Integer> runtimeStatsMap = new HashMap<>();
577 
578     static {
579         runtimeStatsMap.put("art.gc.gc-count", 0);
580         runtimeStatsMap.put("art.gc.gc-time", 1);
581         runtimeStatsMap.put("art.gc.bytes-allocated", 2);
582         runtimeStatsMap.put("art.gc.bytes-freed", 3);
583         runtimeStatsMap.put("art.gc.blocking-gc-count", 4);
584         runtimeStatsMap.put("art.gc.blocking-gc-time", 5);
585         runtimeStatsMap.put("art.gc.gc-count-rate-histogram", 6);
586         runtimeStatsMap.put("art.gc.blocking-gc-count-rate-histogram", 7);
587         runtimeStatsMap.put("art.gc.objects-allocated", 8);
588         runtimeStatsMap.put("art.gc.total-time-waiting-for-gc", 9);
589         runtimeStatsMap.put("art.gc.pre-oome-gc-count", 10);
590     }
591 
592     /**
593      * Returns the value of a particular runtime statistic or {@code null} if no
594      * such runtime statistic exists.
595      *
596      * @param statName the name of the runtime statistic to look up.
597      *
598      * @return the value of the runtime statistic.
599      *
600      * @hide
601      */
602     @SystemApi(client = MODULE_LIBRARIES)
getRuntimeStat(String statName)603     public static String getRuntimeStat(String statName) {
604         if (statName == null) {
605             throw new NullPointerException("statName == null");
606         }
607         Integer statId = runtimeStatsMap.get(statName);
608         if (statId != null) {
609             return getRuntimeStatInternal(statId);
610         }
611         return null;
612     }
613 
614     /**
615      * Returns a map of the names/values of the runtime statistics
616      * that {@link #getRuntimeStat()} supports.
617      *
618      * @return a map of the names/values of the supported runtime statistics.
619      *
620      * @hide
621      */
622     @SystemApi(client = MODULE_LIBRARIES)
getRuntimeStats()623     public static Map<String, String> getRuntimeStats() {
624         HashMap<String, String> map = new HashMap<>();
625         String[] values = getRuntimeStatsInternal();
626         for (String name : runtimeStatsMap.keySet()) {
627             int id = runtimeStatsMap.get(name);
628             String value = values[id];
629             map.put(name, value);
630         }
631         return map;
632     }
633 
getRuntimeStatInternal(int statId)634     private static native String getRuntimeStatInternal(int statId);
getRuntimeStatsInternal()635     private static native String[] getRuntimeStatsInternal();
636 
637     /**
638      * Attaches an agent to the VM.
639      *
640      * @param agent       The path to the agent .so file plus optional agent arguments.
641      * @param classLoader The classloader to use as a loading context.
642      *
643      * @throws IOException if an error occurs while opening {@code agent} file.
644      *
645      * @hide
646      */
647     @SystemApi(client = MODULE_LIBRARIES)
attachAgent(String agent, ClassLoader classLoader)648     public static void attachAgent(String agent, ClassLoader classLoader) throws IOException {
649         nativeAttachAgent(agent, classLoader);
650     }
651 
nativeAttachAgent(String agent, ClassLoader classLoader)652     private static native void nativeAttachAgent(String agent, ClassLoader classLoader)
653             throws IOException;
654 
655     /**
656      * Exempts a class from any future non-SDK API access checks.
657      * Methods declared in the class will be allowed to perform
658      * reflection/JNI against the framework completely unrestricted.
659      * Note that this does not affect uses of non-SDK APIs that the class links against.
660      * Note that this does not affect methods declared outside this class, e.g.
661      * inherited from a superclass or an implemented interface.
662      *
663      * @param klass The class whose methods should be exempted.
664      *
665      * @hide
666      */
667     @UnsupportedAppUsage
allowHiddenApiReflectionFrom(Class<?> klass)668     public static native void allowHiddenApiReflectionFrom(Class<?> klass);
669 
670     /**
671      * Sets the number of frames recorded for allocation tracking.
672      *
673      * @param stackDepth The number of frames captured for each stack trace.
674      *
675      * @hide
676      */
677     @SystemApi(client = MODULE_LIBRARIES)
setAllocTrackerStackDepth(int stackDepth)678     public static native void setAllocTrackerStackDepth(int stackDepth);
679 
680     /**
681      * Called every time the Process is renamed (via android.os.Process)
682      * This is a no-op on host.
683      *
684      * @param processName The new name of the process.
685      *
686      * @hide
687      **/
688     @SystemApi(client = MODULE_LIBRARIES)
setCurrentProcessName(@onNull String processName)689     public static native void setCurrentProcessName(@NonNull String processName);
690 
691     /**
692      * Called every time an app is added to the current Process
693      * This is a no-op on host.
694      *
695      * @param packageName The package name of the added app.
696      *
697      * @hide
698      **/
699     @SystemApi(client = MODULE_LIBRARIES)
addApplication(@onNull String packageName)700     public static native void addApplication(@NonNull String packageName);
701 
702     /**
703      * Called every time an app is removed from the current Process
704      * This is a no-op on host.
705      *
706      * @param packageName The package name of the removed app.
707      *
708      * @hide
709      **/
710     @SystemApi(client = MODULE_LIBRARIES)
removeApplication(@onNull String packageName)711     public static native void removeApplication(@NonNull String packageName);
712 
713     /**
714      * Called as soon as Zygote specialize and knows the Android User ID.
715      * This is a no-op on host.
716      *
717      * @param userId The Android User ID.
718      *
719      * @hide
720      **/
721     @SystemApi(client = MODULE_LIBRARIES)
setUserId(@serIdInt int userId)722     public static native void setUserId(@UserIdInt int userId);
723 
724     /**
725      * Signal to ART we are waiting for the debugger
726      * This is a no-op on host.
727      *
728      * @hide
729      */
730     @SystemApi(client = MODULE_LIBRARIES)
setWaitingForDebugger(boolean waiting)731     public static native void setWaitingForDebugger(boolean waiting);
732 }
733