[codegen][debug info] Introduce -Xdebug-info-version flag

- emit DW_LANG_Kotlin in debug info
- change Dwarf version to 4.
This commit is contained in:
Vasily Levchenko
2018-10-18 18:37:32 +03:00
parent e365fb8f40
commit c94b1b5db9
6 changed files with 39 additions and 10 deletions
@@ -189,6 +189,7 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
put(FRIEND_MODULES, arguments.friendModules!!.split(File.pathSeparator).filterNot(String::isEmpty))
put(BITCODE_EMBEDDING_MODE, selectBitcodeEmbeddingMode(this, arguments, outputKind))
put(DEBUG_INFO_VERSION, arguments.debugInfoFormatVersion.toInt())
}
}
}
@@ -152,6 +152,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
)
var friendModules: String? = null
@Argument(value = "-Xdebug-info-version", description = "generate debug info of given version (1, 2)")
var debugInfoFormatVersion: String = "1" /* command line parser doesn't accept kotlin.Int type */
override fun configureAnalysisFlags(collector: MessageCollector): MutableMap<AnalysisFlag<*>, Any> =
super.configureAnalysisFlags(collector).also {
val useExperimental = it[AnalysisFlags.useExperimental] as List<*>
@@ -100,6 +100,9 @@ class KonanConfigKeys {
= CompilerConfigurationKey.create("verify ir")
val VERBOSE_PHASES: CompilerConfigurationKey<List<String>>
= CompilerConfigurationKey.create("verbose backend phases")
val DEBUG_INFO_VERSION: CompilerConfigurationKey<Int>
= CompilerConfigurationKey.create("debug info format version")
}
}
@@ -9,12 +9,13 @@ import kotlinx.cinterop.allocArrayOf
import kotlinx.cinterop.memScoped
import llvm.*
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.konan.KonanVersion
import org.jetbrains.kotlin.backend.konan.KonanConfig
import org.jetbrains.kotlin.backend.konan.KonanConfigKeys
import org.jetbrains.kotlin.backend.konan.irasdescriptors.FunctionDescriptor
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.SourceManager.FileEntry
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName
import org.jetbrains.kotlin.konan.KonanVersion
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
@@ -23,18 +24,39 @@ import org.jetbrains.kotlin.types.TypeUtils
internal object DWARF {
val producer = "konanc ${KonanVersion.CURRENT} / kotlin-compiler: ${KotlinVersion.CURRENT}"
/* TODO: from LLVM sources is unclear what runtimeVersion corresponds to term in terms of dwarf specification. */
val runtimeVersion = 2
val dwarfVersionMetaDataNodeName = "Dwarf Version".mdString()
val dwarfDebugInfoMetaDataNodeName = "Debug Info Version".mdString()
val dwarfVersion = 2 /* TODO: configurable? like gcc/clang -gdwarf-2 and so on. */
val debugInfoVersion = 3 /* TODO: configurable? */
const val debugInfoVersion = 3 /* TODO: configurable? */
/**
* This is the value taken from [DIFlags.FlagFwdDecl], to mark type declaration as
* forward one.
*/
val flagsForwardDeclaration = 4
const val flagsForwardDeclaration = 4
fun runtimeVersion(config: KonanConfig) = when (config.debugInfoVersion()) {
2 -> 0
1 -> 2 /* legacy :/ */
else -> TODO("unsupported debug info format version")
}
/**
* Note: Kotlin language constant appears in DWARF v6, while modern linker fails to links DWARF other then [2;4],
* that why we emit version 4 actually.
*/
fun dwarfVersion(config : KonanConfig) = when (config.debugInfoVersion()) {
1 -> 2
2 -> 4 /* likely the most of the future kotlin native debug info format versions will emit DWARF v4 */
else -> TODO("unsupported debug info format version")
}
fun language(config: KonanConfig) = when (config.debugInfoVersion()) {
1 -> DwarfLanguage.DW_LANG_C89.value
else -> DwarfLanguage.DW_LANG_Kotlin.value
}
}
fun KonanConfig.debugInfoVersion():Int = configuration[KonanConfigKeys.DEBUG_INFO_VERSION] ?: 1
internal class DebugInfo internal constructor(override val context: Context):ContextUtils {
val files = mutableMapOf<String, DIFileRef>()
val subprograms = mutableMapOf<LLVMValueRef, DISubprogramRef>()
@@ -119,7 +141,7 @@ internal fun generateDebugInfoHeader(context: Context) {
* !6 = !{!"Apple LLVM version 8.0.0 (clang-800.0.38)"}
*/
val llvmTwo = Int32(2).llvm
val dwarfVersion = node(llvmTwo, DWARF.dwarfVersionMetaDataNodeName, Int32(DWARF.dwarfVersion).llvm)
val dwarfVersion = node(llvmTwo, DWARF.dwarfVersionMetaDataNodeName, Int32(DWARF.dwarfVersion(context.config)).llvm)
val nodeDebugInfoVersion = node(llvmTwo, DWARF.dwarfDebugInfoMetaDataNodeName, Int32(DWARF.debugInfoVersion).llvm)
val llvmModuleFlags = "llvm.module.flags"
LLVMAddNamedMetadataOperand(context.llvmModule, llvmModuleFlags, dwarfVersion)
@@ -305,5 +305,5 @@ internal enum class DwarfLanguage(val value:Int) {
DW_LANG_Mips_Assembler(0x8001),
DW_LANG_GOOGLE_RenderScript(0x8e57),
DW_LANG_BORLAND_Delphi(0xb000),
DW_LANG_Kotlin(0x0001) /* manually added, should be changed someday. */
DW_LANG_Kotlin(0x0026)
}
@@ -1907,13 +1907,13 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
val path = this.fileEntry.name.toFileAndFolder()
DICreateCompilationUnit(
builder = context.debugInfo.builder,
lang = DwarfLanguage.DW_LANG_Kotlin.value,
lang = DWARF.language(context.config),
File = path.file,
dir = path.folder,
producer = DWARF.producer,
isOptimized = 0,
flags = "",
rv = DWARF.runtimeVersion)
rv = DWARF.runtimeVersion(context.config))
DICreateFile(context.debugInfo.builder, path.file, path.folder)!!
}
}