• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ets_handle.h"
17 #include "include/mem/panda_containers.h"
18 #include "runtime/runtime_helpers.h"
19 #include "types/ets_method.h"
20 #include "types/ets_stacktrace_element.h"
21 #include "plugins/ets/runtime/ets_coroutine.h"
22 #include "plugins/ets/runtime/ets_exceptions.h"
23 #include "plugins/ets/runtime/ets_panda_file_items.h"
24 
25 #include "runtime/include/stack_walker.h"
26 #include "runtime/include/thread.h"
27 #include "runtime/interpreter/runtime_interface.h"
28 #include "runtime/handle_scope.h"
29 #include "runtime/handle_scope-inl.h"
30 
31 namespace ark::ets::intrinsics {
32 
CreateStackTraceElement(StackWalker * stack)33 EtsStackTraceElement *CreateStackTraceElement(StackWalker *stack)
34 {
35     auto coroutine = EtsCoroutine::GetCurrent();
36     [[maybe_unused]] EtsHandleScope scope(coroutine);
37 
38     EtsMethod *method = EtsMethod::FromRuntimeMethod(stack->GetMethod());
39     auto className = EtsHandle<EtsString>(coroutine, method->GetClass()->GetName());
40     auto methodName = EtsHandle<EtsString>(coroutine, method->GetNameString());
41     const auto lineNumber = method->GetLineNumFromBytecodeOffset(stack->GetBytecodePc());
42     auto *sourceFile = reinterpret_cast<const char *>(method->GetClassSourceFile().data);
43     if (sourceFile == nullptr) {
44         sourceFile = "<unknown>";
45     }
46 
47     auto element = EtsHandle<EtsStackTraceElement>(coroutine, EtsStackTraceElement::Create(coroutine));
48     element->SetClassName(className.GetPtr());
49     element->SetMethodName(methodName.GetPtr());
50 
51     EtsString *sourceFileName = EtsString::CreateFromMUtf8(sourceFile);
52     element->SetSourceFileName(sourceFileName);
53     element->SetLineNumber(lineNumber);
54     return element.GetPtr();
55 }
56 
StdCoreStackTraceProvisionStackTrace()57 extern "C" EtsObjectArray *StdCoreStackTraceProvisionStackTrace()
58 {
59     auto coroutine = EtsCoroutine::GetCurrent();
60     [[maybe_unused]] EtsHandleScope scope(coroutine);
61 
62     auto linker = coroutine->GetPandaVM()->GetClassLinker();
63     auto stackTraceElementClass = linker->GetClass(panda_file_items::class_descriptors::STACK_TRACE_ELEMENT.data());
64 
65     auto thread = ManagedThread::GetCurrent();
66     auto walker = StackWalker::Create(thread);
67 
68     PandaVector<EtsHandle<EtsStackTraceElement>> stackTraceElements;
69     for (auto stack = StackWalker::Create(thread); stack.HasFrame(); stack.NextFrame()) {
70         auto element = EtsHandle<EtsStackTraceElement>(coroutine, CreateStackTraceElement(&stack));
71         stackTraceElements.push_back(element);
72     }
73 
74     const auto linesSize = static_cast<uint32_t>(stackTraceElements.size());
75 
76     EtsHandle<EtsObjectArray> resultArrayHandle(coroutine, EtsObjectArray::Create(stackTraceElementClass, linesSize));
77     for (uint32_t i = 0; i < linesSize; i++) {
78         resultArrayHandle.GetPtr()->Set(i, stackTraceElements[i]->AsObject());
79     }
80     return resultArrayHandle.GetPtr();
81 }
82 
83 }  // namespace ark::ets::intrinsics
84