\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#383: Injecting Code into Suspended Coroutines}
\date{December 03, 2010}
\author{Alexandre Ferrieux, Miguel Sofer}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#383}&\textbf{Injecting Code into Suspended Coroutines}\\\hline
Author&

Alexandre Ferrieux \tipmail{alexandre.ferrieux}{gmail.com}
\par
Miguel Sofer \tipmail{msofer}{users.sourceforge.net}
\\
Created&Friday, \(\text{3}^{\text{rd}}\) December 2010\\
Type&Project\\
Tcl Version&\(8.7\)\\
State&Draft\\
Vote&Pending\\
Version&\tipversion$Revision: 1.4 $\\
Keywords&debugging, coroutine, yielded\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This proposes a new command, \textbf{coroinject}, that allows a programmer to inject arbitrary code into a suspended coroutine, for execution on next resumption.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

When debugging complex coroutines --- with many yield points and possibly rich state in local variables --- sometimes one would like to inspect their state ``from the outside'', i.e., at a point where they are suspended.

A typical situation is that of a big, single-threaded, event+coro system, where the coro happily enables/disables fileevents along its life, and the fileevents are one way to resume the coro. At a given point (bug), things get stalled, with the fileevents disabled. The obvious questions are:
\begin{enumerate}
\item{}
``where'' is the coro (at which yield site)? and
\setcounter{enumi}{1}
\item{}
what are the values of its local variables?
\end{enumerate}

Both these questions can be answered with the new \textbf{coroinject} primitive. The idea is to force a resumption of the coro along with an ``immediate execution of extra code'' directive, where the extra code says ``dump the call stack with \textbf{info level} and \textbf{info frame}'', or ``dump the locals'', etc.

Another use would be to inject ``\textbf{return -code return}'', as an alternative to renaming to \{\} for terminating the coro in a way that respects its \textbf{catch}/\textbf{finally} termination handlers. Alternatively, returning with an error code will have the effect of gathering call stack information in the \textbf{-errorstack} options dictionary entry.

At the other end of the spectrum, the injected code can be completely transparent: either with a forced resumption and injected code ending with \textbf{yield}, or merely waiting for normal resumption when the app sees fit, and injected code falling back to normal coro code.

Note that the feature is similar to a proc-entry trace, but coroutine resumption is not currently a trace target. Also, it is an intrinsically ``one-shot'' mechanism, which makes it a better fit for its debugging purposes.

\section{Definition}

The new command

\begin{quote}
\textbf{coroinject} \textit{coroname} \textit{cmd} ?\textit{arg1 ...}?
\end{quote}

prepends to the code to be executed on resumption of the currently suspended coroutine, \textit{coroname}, the following code:

\begin{quote}
\textit{cmd} \textit{arg1...} \textit{resumearg}
\end{quote}

where \textit{resumearg} is the single argument passed to the resumption command \textit{coroname}. In turn, the result from the execution of \textit{cmd} will be seen by the coroutine's code as the result of \textbf{yield}.

Note that:
\begin{enumerate}
\item{}
Resumption itself must be done separately, by calling \textit{coroname} later,
\setcounter{enumi}{1}
\item{}
If \textbf{coroinject} is called several times on the same \textit{coroname} before resuming it, the commands pile up in LIFO order.
\setcounter{enumi}{2}
\item{}
In combination, the appending of \textit{resumearg} and the use of the result of \textit{cmd} to provide the result of \textbf{yield}, will allow the following style of fully transparent injection:
\begingroup\small\begin{verbatim}
    proc probe {x y resumearg} {do things $x $y;return $resumearg}
    coroinject C probe foo bar
\end{verbatim}
\endgroup
\end{enumerate}

\section{Naming}

Of course, the proposed \textbf{coroinject} is a placeholder for a suitable name. Alternatives that also make sense are: \textbf{::tcl::coroinject} and \textbf{interp coroinject}. Constructive bikeshedding welcome.

\section{Reference Implementation}

The current \textbf{::tcl::unsupported::inject} implements most of the functionality described here, minus the \textit{resumearg} passing. It will be updated to include it if consensus gathers on this style.

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
