blob: ebb868b11f5af427905f60ec50a2b3ace4bab974 [file] [log] [blame]
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001/*
2 * Copyright (C) 2011 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#include "oat_quick_method_header.h"
18
19#include "art_method.h"
David Sehr9e734c72018-01-04 17:56:19 -080020#include "dex/dex_file_types.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000021#include "interpreter/interpreter_mterp_impl.h"
22#include "interpreter/mterp/mterp.h"
23#include "nterp_helpers.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
David Srbecky052f8ca2018-04-26 15:42:54 +010025#include "stack_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026#include "thread.h"
27
28namespace art {
29
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000030uint32_t OatQuickMethodHeader::ToDexPc(ArtMethod** frame,
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010031 const uintptr_t pc,
32 bool abort_on_failure) const {
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000033 ArtMethod* method = *frame;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034 const void* entry_point = GetEntryPoint();
35 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(entry_point);
David Srbeckyafc97bc2018-07-05 08:14:35 +000036 if (method->IsNative()) {
37 return dex::kDexNoIndex;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000038 } else if (IsNterpMethodHeader()) {
39 return NterpGetDexPC(frame);
David Srbeckyafc97bc2018-07-05 08:14:35 +000040 } else {
41 DCHECK(IsOptimized());
David Srbecky0d4567f2019-05-30 22:45:40 +010042 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
David Srbecky052f8ca2018-04-26 15:42:54 +010043 StackMap stack_map = code_info.GetStackMapForNativePcOffset(sought_offset);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010044 if (stack_map.IsValid()) {
David Srbecky052f8ca2018-04-26 15:42:54 +010045 return stack_map.GetDexPc();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010046 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010047 }
48 if (abort_on_failure) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010049 LOG(FATAL) << "Failed to find Dex offset for PC offset "
50 << reinterpret_cast<void*>(sought_offset)
51 << "(PC " << reinterpret_cast<void*>(pc) << ", entry_point=" << entry_point
52 << " current entry_point=" << method->GetEntryPointFromQuickCompiledCode()
David Sehr709b0702016-10-13 09:12:37 -070053 << ") in " << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010054 }
Andreas Gampee2abbc62017-09-15 11:59:26 -070055 return dex::kDexNoIndex;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010056}
57
58uintptr_t OatQuickMethodHeader::ToNativeQuickPc(ArtMethod* method,
59 const uint32_t dex_pc,
60 bool is_for_catch_handler,
61 bool abort_on_failure) const {
62 const void* entry_point = GetEntryPoint();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010063 DCHECK(!method->IsNative());
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000064 if (IsNterpMethodHeader()) {
65 // This should only be called on an nterp frame for getting a catch handler.
66 CHECK(is_for_catch_handler);
67 return NterpGetCatchHandler();
68 }
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010069 DCHECK(IsOptimized());
70 // Search for the dex-to-pc mapping in stack maps.
David Srbecky0d4567f2019-05-30 22:45:40 +010071 CodeInfo code_info = CodeInfo::DecodeInlineInfoOnly(this);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010072
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010073 // All stack maps are stored in the same CodeItem section, safepoint stack
74 // maps first, then catch stack maps. We use `is_for_catch_handler` to select
75 // the order of iteration.
76 StackMap stack_map =
David Srbecky052f8ca2018-04-26 15:42:54 +010077 LIKELY(is_for_catch_handler) ? code_info.GetCatchStackMapForDexPc(dex_pc)
78 : code_info.GetStackMapForDexPc(dex_pc);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010079 if (stack_map.IsValid()) {
80 return reinterpret_cast<uintptr_t>(entry_point) +
David Srbecky052f8ca2018-04-26 15:42:54 +010081 stack_map.GetNativePcOffset(kRuntimeISA);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010082 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010083 if (abort_on_failure) {
84 ScopedObjectAccess soa(Thread::Current());
85 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
David Sehr709b0702016-10-13 09:12:37 -070086 << " in " << method->PrettyMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010087 }
88 return UINTPTR_MAX;
89}
90
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000091OatQuickMethodHeader* OatQuickMethodHeader::NterpMethodHeader =
92 (interpreter::IsNterpSupported()
93 ? reinterpret_cast<OatQuickMethodHeader*>(
94 reinterpret_cast<uintptr_t>(interpreter::GetNterpEntryPoint()) -
95 sizeof(OatQuickMethodHeader))
96 : nullptr);
97
98bool OatQuickMethodHeader::IsNterpMethodHeader() const {
99 return interpreter::IsNterpSupported() ? (this == NterpMethodHeader) : false;
100}
101
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100102} // namespace art