1 /**
2 * Copyright (c) 2021-2025 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 auto *stackTraceElement = EtsStackTraceElement::Create(coroutine);
47 ASSERT(stackTraceElement != nullptr);
48 auto element = EtsHandle<EtsStackTraceElement>(coroutine, stackTraceElement);
49 element->SetClassName(className.GetPtr());
50 element->SetMethodName(methodName.GetPtr());
51
52 EtsString *sourceFileName = EtsString::CreateFromMUtf8(sourceFile);
53 element->SetSourceFileName(sourceFileName);
54 element->SetLineNumber(lineNumber);
55 return element.GetPtr();
56 }
57
StdCoreStackTraceProvisionStackTrace()58 extern "C" EtsObjectArray *StdCoreStackTraceProvisionStackTrace()
59 {
60 auto coroutine = EtsCoroutine::GetCurrent();
61 [[maybe_unused]] EtsHandleScope scope(coroutine);
62
63 auto linker = coroutine->GetPandaVM()->GetClassLinker();
64 auto stackTraceElementClass = linker->GetClass(panda_file_items::class_descriptors::STACK_TRACE_ELEMENT.data());
65
66 auto thread = ManagedThread::GetCurrent();
67 auto walker = StackWalker::Create(thread);
68
69 PandaVector<EtsHandle<EtsStackTraceElement>> stackTraceElements;
70 for (auto stack = StackWalker::Create(thread); stack.HasFrame(); stack.NextFrame()) {
71 auto element = EtsHandle<EtsStackTraceElement>(coroutine, CreateStackTraceElement(&stack));
72 stackTraceElements.push_back(element);
73 }
74
75 const auto linesSize = static_cast<uint32_t>(stackTraceElements.size());
76
77 auto *resultArray = EtsObjectArray::Create(stackTraceElementClass, linesSize);
78 ASSERT(resultArray != nullptr);
79 EtsHandle<EtsObjectArray> resultArrayHandle(coroutine, resultArray);
80 for (uint32_t i = 0; i < linesSize; i++) {
81 resultArrayHandle.GetPtr()->Set(i, stackTraceElements[i]->AsObject());
82 }
83 return resultArrayHandle.GetPtr();
84 }
85
86 } // namespace ark::ets::intrinsics
87