2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Miscellaneous utility functions.
24 #include <sys/types.h>
33 * Used to shut up the compiler when a parameter isn't used.
35 #define UNUSED_PARAMETER(p) (void)(p)
38 * Floating point conversion functions. These are necessary to avoid
39 * strict-aliasing problems ("dereferencing type-punned pointer will break
40 * strict-aliasing rules"). According to the gcc info page, this usage
41 * is allowed, even with "-fstrict-aliasing".
43 * The code generated by gcc-4.1.1 appears to be much better than a
44 * type cast dereference ("int foo = *(int*)&myfloat") when the conversion
45 * function is inlined. It also allows us to take advantage of the
46 * optimizations that strict aliasing rules allow.
48 INLINE float dvmU4ToFloat(u4 val) {
49 union { u4 in; float out; } conv;
53 INLINE u4 dvmFloatToU4(float val) {
54 union { float in; u4 out; } conv;
60 * Print a hex dump to the log file.
62 * "local" mode prints a hex dump starting from offset 0 (roughly equivalent
65 * "mem" mode shows the actual memory address, and will offset the start
66 * so that the low nibble of the address is always zero.
68 * If "tag" is NULL the default tag ("dalvikvm") will be used.
70 typedef enum { kHexDumpLocal, kHexDumpMem } HexDumpMode;
71 void dvmPrintHexDumpEx(int priority, const char* tag, const void* vaddr,
72 size_t length, HexDumpMode mode);
75 * Print a hex dump, at INFO level.
77 INLINE void dvmPrintHexDump(const void* vaddr, size_t length) {
78 dvmPrintHexDumpEx(ANDROID_LOG_INFO, LOG_TAG,
79 vaddr, length, kHexDumpLocal);
83 * Print a hex dump at VERBOSE level. This does nothing in non-debug builds.
85 INLINE void dvmPrintHexDumpDbg(const void* vaddr, size_t length,const char* tag)
88 dvmPrintHexDumpEx(ANDROID_LOG_VERBOSE, (tag != NULL) ? tag : LOG_TAG,
89 vaddr, length, kHexDumpLocal);
94 kDebugTargetUnknown = 0,
100 * We pass one of these around when we want code to be able to write debug
101 * info to either the log or to a file (or stdout/stderr).
103 typedef struct DebugOutputTarget {
105 DebugTargetKind which;
107 /* additional bits */
120 * Fill in a DebugOutputTarget struct.
122 void dvmCreateLogOutputTarget(DebugOutputTarget* target, int priority,
124 void dvmCreateFileOutputTarget(DebugOutputTarget* target, FILE* fp);
127 * Print a debug message.
129 void dvmPrintDebugMessage(const DebugOutputTarget* target, const char* format,
131 #if defined(__GNUC__)
132 __attribute__ ((format(printf, 2, 3)))
137 * Return a newly-allocated string in which all occurrences of '.' have
138 * been changed to '/'. If we find a '/' in the original string, NULL
139 * is returned to avoid ambiguity.
141 char* dvmDotToSlash(const char* str);
144 * Return a newly-allocated string containing a human-readable equivalent
145 * of 'descriptor'. So "I" would be "int", "[[I" would be "int[][]",
146 * "[Ljava/lang/String;" would be "java.lang.String[]", and so forth.
148 char* dvmHumanReadableDescriptor(const char* descriptor);
151 * Return a newly-allocated string for the "dot version" of the class
152 * name for the given type descriptor. That is, The initial "L" and
153 * final ";" (if any) have been removed and all occurrences of '/'
154 * have been changed to '.'.
156 * "Dot version" names are used in the class loading machinery.
157 * See also dvmHumanReadableDescriptor.
159 char* dvmDescriptorToDot(const char* str);
162 * Return a newly-allocated string for the type descriptor
163 * corresponding to the "dot version" of the given class name. That
164 * is, non-array names are surrounded by "L" and ";", and all
165 * occurrences of '.' have been changed to '/'.
167 * "Dot version" names are used in the class loading machinery.
169 char* dvmDotToDescriptor(const char* str);
172 * Return a newly-allocated string for the internal-form class name for
173 * the given type descriptor. That is, the initial "L" and final ";" (if
174 * any) have been removed.
176 char* dvmDescriptorToName(const char* str);
179 * Return a newly-allocated string for the type descriptor for the given
180 * internal-form class name. That is, a non-array class name will get
181 * surrounded by "L" and ";", while array names are left as-is.
183 char* dvmNameToDescriptor(const char* str);
186 * Get the current time, in nanoseconds. This is "relative" time, meaning
187 * it could be wall-clock time or a monotonic counter, and is only suitable
188 * for computing time deltas.
190 u8 dvmGetRelativeTimeNsec(void);
193 * Get the current time, in microseconds. This is "relative" time, meaning
194 * it could be wall-clock time or a monotonic counter, and is only suitable
195 * for computing time deltas.
197 INLINE u8 dvmGetRelativeTimeUsec(void) {
198 return dvmGetRelativeTimeNsec() / 1000;
202 * Get the current time, in milliseconds. This is "relative" time,
203 * meaning it could be wall-clock time or a monotonic counter, and is
204 * only suitable for computing time deltas. The value returned from
205 * this function is a u4 and should only be used for debugging
206 * messages. TODO: make this value relative to the start-up time of
209 INLINE u4 dvmGetRelativeTimeMsec(void) {
210 return (u4)(dvmGetRelativeTimeUsec() / 1000);
214 * Get the current per-thread CPU time. This clock increases monotonically
215 * when the thread is running, but not when it's sleeping or blocked on a
216 * synchronization object.
218 * The absolute value of the clock may not be useful, so this should only
219 * be used for time deltas.
221 * If the thread CPU clock is not available, this always returns (u8)-1.
223 u8 dvmGetThreadCpuTimeNsec(void);
226 * Per-thread CPU time, in micros.
228 INLINE u8 dvmGetThreadCpuTimeUsec(void) {
229 return dvmGetThreadCpuTimeNsec() / 1000;
233 * Like dvmGetThreadCpuTimeNsec, but for a different thread.
235 u8 dvmGetOtherThreadCpuTimeNsec(pthread_t thread);
236 INLINE u8 dvmGetOtherThreadCpuTimeUsec(pthread_t thread) {
237 return dvmGetOtherThreadCpuTimeNsec(thread) / 1000;
241 * Sleep for increasingly longer periods, until "maxTotalSleep" microseconds
242 * have elapsed. Pass in the start time, which must be a value returned by
243 * dvmGetRelativeTimeUsec().
245 * Returns "false" if we were unable to sleep because our time is up.
247 bool dvmIterativeSleep(int iteration, int maxTotalSleep, u8 relStartTime);
250 * Set the "close on exec" flag on a file descriptor.
252 bool dvmSetCloseOnExec(int fd);
255 * Unconditionally abort the entire VM. Try not to use this.
257 * NOTE: if this is marked ((noreturn)), gcc will merge multiple dvmAbort()
258 * calls in a single function together. This is good, in that it reduces
259 * code size slightly, but also bad, because the native stack trace we
260 * get from the abort may point at the wrong call site. Best to leave
264 void dvmPrintNativeBackTrace(void);
267 /* Implementation of strlcpy() for platforms that don't already have it. */
268 size_t strlcpy(char *dst, const char *src, size_t size);
272 * Allocates a memory region using ashmem and mmap, initialized to
273 * zero. Actual allocation rounded up to page multiple. Returns
276 void *dvmAllocRegion(size_t size, int prot, const char *name);
279 * Get some per-thread stats from /proc/self/task/N/stat.
282 unsigned long utime; /* number of jiffies scheduled in user mode */
283 unsigned long stime; /* number of jiffies scheduled in kernel mode */
284 int processor; /* number of CPU that last executed thread */
286 bool dvmGetThreadStats(ProcStatData* pData, pid_t tid);
289 * Returns the pointer to the "absolute path" part of the given path
290 * string, treating first (if any) instance of "/./" as a sentinel
291 * indicating the start of the absolute path. If the path isn't absolute
292 * in the usual way (i.e., starts with "/") and doesn't have the sentinel,
293 * then this returns NULL.
296 * "/foo/bar/baz" returns "/foo/bar/baz"
297 * "foo/./bar/baz" returns "/bar/baz"
298 * "foo/bar/baz" returns NULL
300 * The sentinel is used specifically to aid in cross-optimization, where
301 * a host is processing dex files in a build tree, and where we don't want
302 * the build tree's directory structure to be baked into the output (such
303 * as, for example, in the dependency paths of optimized dex files).
305 const char* dvmPathToAbsolutePortion(const char* path);
311 #endif /*_DALVIK_MISC*/