+#ifdef DEBUG
+/* Set to skip the next plan. */
+bool io_plan_nodebug;
+/* The current connection to apply plan to. */
+struct io_conn *current;
+/* User-defined function to select which connection(s) to debug. */
+bool (*io_debug_conn)(struct io_conn *conn);
+/* Set when we wake up an connection we are debugging. */
+bool io_debug_wakeup;
+
+struct io_plan io_debug(struct io_plan plan)
+{
+ if (io_plan_nodebug) {
+ io_plan_nodebug = false;
+ return plan;
+ }
+
+ if (!io_debug_conn || !current)
+ return plan;
+
+ if (!io_debug_conn(current) && !io_debug_wakeup)
+ return plan;
+
+ io_debug_wakeup = false;
+ current->plan = plan;
+ backend_plan_changed(current);
+
+ /* Call back into the loop immediately. */
+ io_loop_return = io_loop();
+ return plan;
+}
+
+static void debug_io_wake(struct io_conn *conn)
+{
+ /* We want linear if we wake a debugged connection, too. */
+ if (io_debug_conn && io_debug_conn(conn))
+ io_debug_wakeup = true;
+}
+
+/* Counterpart to io_plan_no_debug(), called in macros in io.h */
+static void io_plan_debug_again(void)
+{
+ io_plan_nodebug = false;
+}
+#else
+static void debug_io_wake(struct io_conn *conn)
+{
+}
+static void io_plan_debug_again(void)
+{
+}
+#endif
+