add debug setting and thread it to the builder via the toolchain
diff --git a/kotlin/BUILD b/kotlin/BUILD
index d20fe7f..b69cba7 100644
--- a/kotlin/BUILD
+++ b/kotlin/BUILD
@@ -16,13 +16,26 @@
 load("//kotlin:toolchains.bzl", "define_kt_toolchain")
 load("//kotlin/internal:bootstrap.bzl", "kt_toolchain_ide_info")
 
+config_setting(
+    name = "builder_debug_setting",
+    values = {
+        "define": "kt_debug=1"
+    }
+)
+
 toolchain_type(
     name = "kt_toolchain_type",
     visibility = ["//visibility:public"]
 )
 
-define_kt_toolchain(name = "default_toolchain")
+define_kt_toolchain(
+    name = "default_toolchain",
+    debug=select({
+        "//kotlin:builder_debug_setting": 1,
+        "//conditions:default": 0
+    })
+)
 
 kt_toolchain_ide_info(name="kt_toolchain_ide_info")
 
-exports_files(["toolchains.bzl", "kotlin.bzl"], visibility=["//docs:__subpackages__"])
\ No newline at end of file
+exports_files(["toolchains.bzl", "kotlin.bzl"], visibility=["//docs:__subpackages__"])
diff --git a/kotlin/builder/proto/jars/libkotlin_model_proto-speed.jar b/kotlin/builder/proto/jars/libkotlin_model_proto-speed.jar
index 6b75802..1411450 100755
--- a/kotlin/builder/proto/jars/libkotlin_model_proto-speed.jar
+++ b/kotlin/builder/proto/jars/libkotlin_model_proto-speed.jar
Binary files differ
diff --git a/kotlin/builder/proto/kotlin_model.proto b/kotlin/builder/proto/kotlin_model.proto
index ead4726..5344fed 100644
--- a/kotlin/builder/proto/kotlin_model.proto
+++ b/kotlin/builder/proto/kotlin_model.proto
@@ -96,6 +96,8 @@
     repeated string friend_paths = 8;
     // The path of the primary output for the task, this is derived from the flagfile.
     string primary_output_path = 9;
+    // If this is true the builder will print out debug messages.
+    int32 debug = 10;
 }
 
 // Mested messages not marked with stable could be refactored.
diff --git a/kotlin/builder/src/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt b/kotlin/builder/src/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt
index 70ce4ae..8fd3bc0 100644
--- a/kotlin/builder/src/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt
+++ b/kotlin/builder/src/io/bazel/kotlin/builder/tasks/KotlinBuilder.kt
@@ -115,11 +115,13 @@
         PLUGINS("--kotlin_plugins"),
         FRIEND_PATHS("--kotlin_friend_paths"),
         OUTPUT_JDEPS("--kotlin_output_jdeps"),
+        DEBUG("--kotlin_debug"),
         TASK_ID("--kotlin_task_id");
     }
 
     private fun buildTaskInfo(argMap: ArgMap): CompilationTaskInfo.Builder =
         with(CompilationTaskInfo.newBuilder()) {
+            debug = argMap.mandatorySingle(KotlinBuilderFlags.DEBUG).toInt()
             label = argMap.mandatorySingle(JavaBuilderFlags.TARGET_LABEL)
             argMap.mandatorySingle(JavaBuilderFlags.RULE_KIND).split("_").also {
                 check(it.size == 3 && it[0] == "kt") { "invalid rule kind $it" }
diff --git a/kotlin/internal/compile.bzl b/kotlin/internal/compile.bzl
index 9ff4051..1d61f17 100644
--- a/kotlin/internal/compile.bzl
+++ b/kotlin/internal/compile.bzl
@@ -33,6 +33,7 @@
     args.add("--kotlin_api_version", toolchain.api_version)
     args.add("--kotlin_language_version", toolchain.language_version)
     args.add("--kotlin_passthrough_flags", "-Xcoroutines=%s" % toolchain.coroutines)
+    args.add("--kotlin_debug", toolchain.debug)
 
     return args
 
diff --git a/kotlin/toolchains.bzl b/kotlin/toolchains.bzl
index a0b9c5e..0e08ab8 100644
--- a/kotlin/toolchains.bzl
+++ b/kotlin/toolchains.bzl
@@ -71,6 +71,9 @@
             "1.2",
         ],
     ),
+    "debug": attr.int(
+        doc="if this is non zero then the builder will produce debug logging."
+    ),
     "coroutines": attr.string(
         default = "enable",
         values = [
@@ -111,6 +114,7 @@
         language_version = ctx.attr.language_version,
         api_version = ctx.attr.api_version,
         coroutines = ctx.attr.coroutines,
+        debug = ctx.attr.debug,
 
         jvm_target = ctx.attr.jvm_target,
 
@@ -141,8 +145,17 @@
   coroutines: the -Xcoroutines flag, enabled by default as it's considered production ready 1.2.0 onward.
 """
 
-def define_kt_toolchain(name, language_version=None, api_version=None, jvm_target=None, coroutines=None):
-    """Define a Kotlin JVM Toolchain, the name is used in the `toolchain` rule so can be used to register the toolchain in the WORKSPACE file."""
+def define_kt_toolchain(
+    name,
+    language_version=None,
+    api_version=None,
+    jvm_target=None,
+    coroutines=None,
+    debug=0
+):
+    """Define a Kotlin JVM Toolchain, the name is used in the `toolchain` rule so can be used to register the toolchain
+    in the WORKSPACE file.
+    """
     impl_name = name + "_impl"
     kt_toolchain(
         name = impl_name,
@@ -150,6 +163,7 @@
         api_version = api_version,
         jvm_target = jvm_target,
         coroutines = coroutines,
+        debug=debug,
         visibility = ["//visibility:public"]
     )
     native.toolchain(