utils.h revision c200a4abeca91e19969f5b35543f17f812ba32b9
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_UTILS_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_UTILS_H_
19a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
2092b3b5623ec8b65f3e099c076e247bb8273692f8Elliott Hughes#include <pthread.h>
21e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes
2234023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <string>
2334023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <vector>
2434023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes
25e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/logging.h"
26e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "globals.h"
270e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom#include "instruction_set.h"
28e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "primitive.h"
29e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes
30bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#ifdef HAVE_ANDROID_OS
31bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#include "cutils/properties.h"
32bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#endif
33bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
346b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapironamespace art {
35a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
360571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersclass DexFile;
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers
382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersnamespace mirror {
39ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtField;
40ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtMethod;
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersclass Class;
4211e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughesclass Object;
435174fe6e4e931c423e910366ff22ce0838567940Elliott Hughesclass String;
442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers}  // namespace mirror
4511e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
460325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartierenum TimeUnit {
470325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitNanosecond,
480325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitMicrosecond,
490325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitMillisecond,
500325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitSecond,
510325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier};
520325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
53a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirotemplate<typename T>
548194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr bool IsPowerOfTwo(T x) {
55a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return (x & (x - 1)) == 0;
56a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
57a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
5806b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T>
5906b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T x) {
6006b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes  COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two);
61a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return (x & (n - 1)) == 0;
62a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
63a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
6406b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T>
6506b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T* x) {
6689521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
67a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
68a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
69af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampetemplate<typename T>
70af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampestatic inline bool IsAlignedParam(T x, int n) {
71af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe  return (x & (n - 1)) == 0;
72af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe}
73af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe
7406b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define CHECK_ALIGNED(value, alignment) \
7589521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
7606b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes
7706b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define DCHECK_ALIGNED(value, alignment) \
7889521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
79af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe
80af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe#define DCHECK_ALIGNED_PARAM(value, alignment) \
81af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe  DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
8206b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes
83a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro// Check whether an N-bit two's-complement representation can hold value.
84a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsInt(int N, word value) {
85a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(0, N);
86a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(N, kBitsPerWord);
87a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  word limit = static_cast<word>(1) << (N - 1);
88a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  return (-limit <= value) && (value < limit);
89a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
90a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
91a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsUint(int N, word value) {
92a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(0, N);
93a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(N, kBitsPerWord);
94a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  word limit = static_cast<word>(1) << N;
95a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  return (0 <= value) && (value < limit);
96a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
97a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
98a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline bool IsAbsoluteUint(int N, word value) {
99a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  CHECK_LT(0, N);
100a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  CHECK_LT(N, kBitsPerWord);
101a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  if (value < 0) value = -value;
102a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return IsUint(N, value);
103a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
104a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
1054ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t Low16Bits(uint32_t value) {
1064ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint16_t>(value);
107b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}
108b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
1094ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t High16Bits(uint32_t value) {
1104ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint16_t>(value >> 16);
111b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}
112a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
1134ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t Low32Bits(uint64_t value) {
1144ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint32_t>(value);
115a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
116a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
1174ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t High32Bits(uint64_t value) {
1184ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint32_t>(value >> 32);
119a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
120a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
1210a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// A static if which determines whether to return type A or B based on the condition boolean.
1228194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <bool condition, typename A, typename B>
1230a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf {
1248194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef A type;
1250a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier};
1260a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier
1270a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// Specialization to handle the false case.
1280a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartiertemplate <typename A, typename B>
1290a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf<false, A,  B> {
1308194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef B type;
1318194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko};
1328194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko
1338194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko// Type identity.
1348194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <typename T>
1358194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostruct TypeIdentity {
1368194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef T type;
1370a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier};
1380a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier
1390941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For rounding integers.
14061e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T>
1418194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) {
1428194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return
143836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko      DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))
144836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko      (x & -n);
14561e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
14661e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
14761e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T>
1488194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) {
14961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  return RoundDown(x + n - 1, n);
15061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
15161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
1520941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For aligning pointers.
1530941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T>
1548194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignDown(T* x, uintptr_t n) {
1558194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
1560941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi}
1570941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi
1580941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T>
1598194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignUp(T* x, uintptr_t n) {
1608194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
1610941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi}
1620941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi
163a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
1641fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro// figure 3-3, page 48, where the function is called clp2.
1651fb8620309a4e94d11879aabc33364acfa733904Carl Shapirostatic inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
1661fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x - 1;
1671fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 1);
1681fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 2);
1691fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 4);
1701fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 8);
1711fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 16);
1721fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  return x + 1;
1731fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro}
1741fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro
1758194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate<typename T>
1768194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CLZ(T x) {
1778194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
1788194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      ? __builtin_clz(x)
1798194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_clzll(x);
180a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
181a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
182ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T>
1838194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CTZ(T x) {
1848194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
1858194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      ? __builtin_ctz(x)
1868194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_ctzll(x);
187ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
188ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
189ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T>
1908194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int POPCOUNT(T x) {
1918194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
1928194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      ? __builtin_popcount(x)
1938194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_popcountll(x);
194ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
195ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
196ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic inline uint32_t PointerToLowMemUInt32(const void* p) {
197ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uintptr_t intp = reinterpret_cast<uintptr_t>(p);
198ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  DCHECK_LE(intp, 0xFFFFFFFFU);
199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  return intp & 0xFFFFFFFFU;
200ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
201db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
20246b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughesstatic inline bool NeedsEscaping(uint16_t ch) {
20346b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes  return (ch < ' ' || ch > '~');
20446b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes}
20546b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes
206c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// Interpret the bit pattern of input (type U) as type V. Requires the size
207c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// of V >= size of U (compile-time checked).
208c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampetemplate<typename U, typename V>
209c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampestatic inline V bit_cast(U in) {
210c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  COMPILE_ASSERT(sizeof(U) <= sizeof(V), size_of_u_not_le_size_of_v);
211c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  union {
212c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe    U u;
213c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe    V v;
214c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  } tmp;
215c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  tmp.u = in;
216c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  return tmp.v;
217c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe}
218c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe
219576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogersstd::string PrintableChar(uint16_t ch);
220c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes
22182914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Returns an ASCII string corresponding to the given UTF-8 string.
22282914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Java escapes are used for non-ASCII characters.
22382914b6164fd0109531391975389e4f0ff6832c8Elliott Hughesstd::string PrintableString(const std::string& utf8);
224c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes
225f1a5adc87760f938b01df26d906295063546b259Elliott Hughes// Tests whether 's' starts with 'prefix'.
226f1a5adc87760f938b01df26d906295063546b259Elliott Hughesbool StartsWith(const std::string& s, const char* prefix);
227f1a5adc87760f938b01df26d906295063546b259Elliott Hughes
2287a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom// Tests whether 's' starts with 'suffix'.
2297a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrombool EndsWith(const std::string& s, const char* suffix);
2307a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom
23154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
23254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// one of which is probably more useful to you.
233a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
234a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
235a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "java.lang.String[]", and so forth.
236ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::String* descriptor)
237ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2386c8867daab4af4667e0e816f6beafa7c5d13e043Elliott Hughesstd::string PrettyDescriptor(const std::string& descriptor);
2396b4ef025af12b158d117fc80fc79acf620f411a0Brian Carlstromstd::string PrettyDescriptor(Primitive::Type type);
240ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::Class* klass)
241b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
24211e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
24354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or
24454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// "int a.b.C.f" (depending on the value of 'with_type').
245ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyField(mirror::ArtField* f, bool with_type = true)
246b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2476f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstromstd::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type = true);
248a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
249a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
250a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "a.b.C.m(II)V" (depending on the value of 'with_signature').
251ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyMethod(mirror::ArtMethod* m, bool with_signature = true)
252b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2530571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersstd::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature = true);
254a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes
255a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable form of the name of the *class* of the given object.
256a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// So given an instance of java.lang.String, the output would
25711e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// be "java.lang.String". Given an array of int, the output would be "int[]".
25811e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>".
259ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyTypeOf(mirror::Object* obj)
260b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2614c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier
2624c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Returns a human-readable form of the type at an index in the specified dex file.
2634c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Example outputs: char[], java.lang.String.
26418c24b6e05e7591069f1a2ac9c614b28fc0853acMathieu Chartierstd::string PrettyType(uint32_t type_idx, const DexFile& dex_file);
26554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes
26654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable form of the name of the given class.
26754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>".
268ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClass(mirror::Class* c)
269b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
27011e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
271d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers// Returns a human-readable form of the name of the given class with its class loader.
272ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClassAndClassLoader(mirror::Class* c)
273b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
274d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers
275c967f78cd29b6019f7cfca40a02e9b677112da70Elliott Hughes// Returns a human-readable size string such as "1MB".
276e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartierstd::string PrettySize(int64_t size_in_bytes);
2773bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
2783bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Returns a human-readable time string which prints every nanosecond while trying to limit the
2793bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// number of trailing zeros. Prints using the largest human readable unit up to a second.
2803bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// e.g. "1ms", "1.000000001s", "1.001us"
281f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
2823bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
2830325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Format a nanosecond time to specified units.
284f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
285f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier                           size_t max_fraction_digits);
2860325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
2870325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Get the appropriate unit for a nanosecond duration.
2880325e6296d2370c42e7be80d846bfc7f8b28423bMathieu ChartierTimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
2890325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
2907020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Get the divisor to convert from a nanoseconds to a time unit.
2910325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartieruint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
2920325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
29379082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
29479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// of the JNI spec.
29579082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughesstd::string MangleForJni(const std::string& s);
29679082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
297f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Turn "java.lang.String" into "Ljava/lang/String;".
298f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstd::string DotToDescriptor(const char* class_name);
299f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
300aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom// Turn "Ljava/lang/String;" into "java.lang.String".
301f1a5adc87760f938b01df26d906295063546b259Elliott Hughesstd::string DescriptorToDot(const char* descriptor);
302aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
30391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes// Turn "Ljava/lang/String;" into "java/lang/String".
30491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughesstd::string DescriptorToName(const char* descriptor);
30591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
306906e685ce43092812bf403016057376d0657a671Elliott Hughes// Tests for whether 's' is a valid class name in the three common forms:
307906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidBinaryClassName(const char* s);  // "java.lang.String"
308906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidJniClassName(const char* s);     // "java/lang/String"
309906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidDescriptor(const char* s);       // "Ljava/lang/String;"
31064bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes
31110037c866b04550fc5461058c398c2e3e509381ajeffhao// Returns whether the given string is a valid field or method name,
31210037c866b04550fc5461058c398c2e3e509381ajeffhao// additionally allowing names that begin with '<' and end with '>'.
31310037c866b04550fc5461058c398c2e3e509381ajeffhaobool IsValidMemberName(const char* s);
31410037c866b04550fc5461058c398c2e3e509381ajeffhao
31579082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the non-overloaded method 'm'.
316ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniShortName(mirror::ArtMethod* m)
317b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
31879082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the overloaded method 'm'.
319ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniLongName(mirror::ArtMethod* m)
320b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
32179082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
322d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesbool ReadFileToString(const std::string& file_name, std::string* result);
323c143c55718342519db5398e41dda31422cf16c79buzbee
324e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
325e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesstd::string GetIsoDate();
326e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
3270512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in milliseconds.
3287162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesuint64_t MilliTime();
3297162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
3300512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in microseconds.
331a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhaouint64_t MicroTime();
332a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao
3330512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in nanoseconds.
33483df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughesuint64_t NanoTime();
33583df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughes
3360512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
3370512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesuint64_t ThreadCpuNanoTime();
3380512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes
339bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes// Converts the given number of nanoseconds to milliseconds.
340720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t NsToMs(uint64_t ns) {
3413bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers  return ns / 1000 / 1000;
3423bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers}
3433bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
3443bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Converts the given number of milliseconds to nanoseconds
345720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t MsToNs(uint64_t ns) {
3463bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers  return ns * 1000 * 1000;
3473bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers}
3483bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
349bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#if defined(__APPLE__)
350bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// No clocks to specify on OS/X, fake value to pass to routines that require a clock.
351bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CLOCK_REALTIME 0xebadf00d
352bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif
353bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
35456edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers// Sleep for the given number of nanoseconds, a bad way to handle contention.
35556edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersvoid NanoSleep(uint64_t ns);
35656edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
357bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// Initialize a timespec to either an absolute or relative time.
358bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromvoid InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
359bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
36048436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Splits a string using the given separator character into a vector of
36134023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes// strings. Empty strings will be omitted.
36248436bbff87aae61bba20029b4382fa41ce787feElliott Hughesvoid Split(const std::string& s, char separator, std::vector<std::string>& result);
36348436bbff87aae61bba20029b4382fa41ce787feElliott Hughes
3647020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Trims whitespace off both ends of the given string.
3657020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allisonstd::string Trim(std::string s);
3667020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
36748436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Joins a vector of strings into a single string, using the given separator.
36848436bbff87aae61bba20029b4382fa41ce787feElliott Hughestemplate <typename StringT> std::string Join(std::vector<StringT>& strings, char separator);
36934023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes
37042ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes// Returns the calling thread's tid. (The C libraries don't expose this.)
37142ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughespid_t GetTid();
37242ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes
373289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes// Returns the given thread's name.
374289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughesstd::string GetThreadName(pid_t tid);
375289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes
376120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers// Returns details of the given thread's stack.
3772921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstromvoid GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size);
378e188419b971936086a188843378375f5ced13724Elliott Hughes
379bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes// Reads data from "/proc/self/task/${tid}/stat".
3802921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstromvoid GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
381bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes
3821bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes// Returns the name of the scheduler group for the given thread the current process, or the empty string.
3831bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstd::string GetSchedulerGroupName(pid_t tid);
3841bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes
385dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// Sets the name of the current thread. The name may be truncated to an
386dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// implementation-defined limit.
38722869a9026a08b544eca4cefd67386d347e30d2cElliott Hughesvoid SetThreadName(const char* thread_name);
388dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
38946e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the native stack for thread 'tid' to 'os'.
390067d20fd2fddd947ce3a014d023e9de249082ebeKenny Rootvoid DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix = "",
391a2cee18799d7e61adb037a4219adb23fef7d022bChristopher Ferris    mirror::ArtMethod* current_method = nullptr)
392067d20fd2fddd947ce3a014d023e9de249082ebeKenny Root    NO_THREAD_SAFETY_ANALYSIS;
39346e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes
39446e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the kernel stack for thread 'tid' to 'os'. Note that this is only available on linux-x86.
39546e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughesvoid DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix = "", bool include_count = true);
39646e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes
3977020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_ROOT, /system, or abort.
398a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidRoot();
399a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom
4007020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_DATA, /data, or abort.
401a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidData();
402a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom
40311d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the dalvik-cache location, or dies trying. subdir will be
40411d9f06a96a6909905c248ed684366190140095cNarayan Kamath// appended to the cache location.
40511d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheOrDie(const char* subdir, bool create_if_absent = true);
40611d9f06a96a6909905c248ed684366190140095cNarayan Kamath
40711d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the absolute dalvik-cache path for a DexFile or OatFile, or
40811d9f06a96a6909905c248ed684366190140095cNarayan Kamath// dies trying. The path returned will be rooted at cache_location.
40911d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheFilenameOrDie(const char* file_location,
41011d9f06a96a6909905c248ed684366190140095cNarayan Kamath                                        const char* cache_location);
411262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao
4120e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns the system location for an image
4132afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string GetSystemImageFilename(const char* location, InstructionSet isa);
4140e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom
4150e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns an .odex file name next adjacent to the dex location.
4160e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// For example, for "/foo/bar/baz.jar", return "/foo/bar/<isa>/baz.odex".
417833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe// Note: does not support multidex location strings.
4182afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string DexFilenameToOdexFilename(const std::string& location, InstructionSet isa);
4190e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom
4207c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom// Check whether the given magic matches a known file type.
4217c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsZipMagic(uint32_t magic);
4227c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsDexMagic(uint32_t magic);
4237c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsOatMagic(uint32_t magic);
424b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom
4256449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom// Wrapper on fork/execv to run a command in a subprocess.
4266449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrombool Exec(std::vector<std::string>& arg_vector, std::string* error_msg);
4276449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom
428d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartierclass VoidFunctor {
429357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier public:
430d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A>
431df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a) const {
432d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
433d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
434d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier
435d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A, typename B>
436df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a, B b) const {
437d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
438d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(b);
439d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
440d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier
441d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A, typename B, typename C>
442df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a, B b, C c) const {
443d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
444d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(b);
445d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(c);
446d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
447357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier};
448357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier
4496b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro}  // namespace art
450a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
451fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_UTILS_H_
452