Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Rework debugging interface to allow coexisting for multiple frameworks. #3818

Merged
merged 7 commits into from
Feb 6, 2020

Conversation

olonho
Copy link
Contributor

@olonho olonho commented Feb 3, 2020

No description provided.

return sizeof(debugBuffer);
}

// Auxilary function which can be called by developer/debugger to inspect an object.
RUNTIME_USED KInt Konan_DebugObjectToUtf8Array(KRef obj, char* buffer, KInt bufferSize) {
RUNTIME_USED RUNTIME_WEAK KInt Konan_DebugObjectToUtf8Array(KRef obj, char* buffer, KInt bufferSize) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugger still uses this API, so now it allows an object of one runtime to leak into other runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it's up to bindings if use legacy API or rely upon the new API. So this is runtime part of the mechanism.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy API turns out to be unsafe.
Why not make it safe by implementing public API through function pointers from "extended info"?


// Auxilary function which can be called by developer/debugger to inspect an object.
RUNTIME_USED RUNTIME_WEAK int Konan_DebugObjectToUtf8Array(KRef obj, char* buffer, int bufferSize) {
auto* impl = getImpl<int (*)(KRef, char*, int)>(obj, DO_DebugObjectToUtf8Array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KInt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched all to explicit int32_t.

}

RUNTIME_USED RUNTIME_WEAK
void* Konan_DebugGetOperation(KRef obj, /* Konan_DebugOperation */ int32_t operation) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this public debug API actually required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly for the completness of the debug API, but indeed, we may not expose it and enum.

}

// IMPORTANT: when updating, fix `debugOperationsSize` in RTTIGenerator.kt.
RUNTIME_USED RUNTIME_WEAK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these attributes required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we could use them from Kotlin side and not conflict on dups.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "Kotlin side"? Generated code?
There are plenty of runtime functions that aren't RUNTIME_USED RUNTIME_WEAK and can be properly used from generated code without conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if needed we can easily add it.

private val debugOperations: ConstValue by lazy {
if (debugRuntimeOrNull != null) {
val external = LLVMGetNamedGlobal(debugRuntimeOrNull, "Konan_debugOperationsList")!!
val local = LLVMAddGlobal(context.llvmModule, LLVMTypeOf(external),"Konan_debugOperationsList")!!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVMTypeOf(external) is not the type of the global itself, it is the type of the pointer to it.
As a result, Konan_debugOperationsList is declared here as external global [14 x i8*]*, while originally defined as global [14 x i8*].

This is also likely the cause of "LLVMGetArrayLength() doesn't work as expected" below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of the global added here is still wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's important, but fixed.

@@ -395,6 +395,30 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
private fun mapRuntimeType(type: LLVMTypeRef): Int =
runtimeTypeMap[type] ?: throw Error("Unmapped type: ${llvmtype2string(type)}")

private val debugRuntimeOrNull: LLVMModuleRef? by lazy {
context.config.runtimeNativeLibraries.singleOrNull { it.endsWith("debug.bc")}?.let {
parseBitcodeFile(it)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be disposed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we can avoid loading this module at all.
debugOperationsSize could be stored in a runtime-defined struct containing debug operations array (or a pointer to it) too.
Then it would be enough to have a single pointer in ExtendedTypeInfo (which can also be considered an optimization).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer disposing solution.

@@ -105,6 +105,9 @@ internal class RTTIGeneratorVisitor(context: Context) : IrElementVisitorVoid {
}
}

fun dispose() {
generator.dispose()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not enough since there are more instances of RTTIGenerator.

@olonho olonho merged commit c0c8ed4 into master Feb 6, 2020
@olonho olonho deleted the rework_debug branch February 6, 2020 04:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants