Consider the following function, testfunc:

testfunc: {[] x: `aaa;
y: `bbb;
z:: `ccc;
-1 “finished!”;

Let’s demonstrate the placement of a breakpoint prior to the assignment of global variable z. Although there is no explicit support for breakpoints in q, insertion of non-compliant code, such as breakhere; shown below, does the job (don’t forget the trailing semicolon):

testfunc: {[] x: `aaa;
y: `bbb;
z:: `ccc;
-1 “finished!”

We are tricking q into throwing a signal ‘breakhere.

q)testfunc[] {[] x: `aaa;
y: `bbb;
z:: `ccc;
-1 “finished!”

At this point, we can examine the local stack.


kdb has suspended execution, leaving the remaining two lines of function testfunc unexecuted. : (colon) resumes execution.


See: global amend (::)

q simply does not seem to hold that information during run time. It knows the state of the stack and function undergoing execution prior to the error event, but cannot map the program counter to a specific line in the q code.

Although it can’t directly tell you the name of the function in which you’ve trapped, it does know the body of the current function via the variable .z.s (self). Using .z.s (self), you can use the following handy code to deduce the name of the function:

function_name: {[body] names: value “\\f”;
bodies: value each names;
matches: names where body ~/: bodies;
$[0 = count matches;
‘ “Not found (try a different namespace?)”;
  1 = count matches;
first matches;
/ else

Here’s how you use it:

q)f: {break}
q)f[] {break}
q))function_name .z.s

Things get more complicated if you put functions into different namespaces:

q)\d .foo {break}\d .
q)[] {break}
‘break`.[`function_name] .z.s

As you can see, q puts you into the namespace that was in effect at the time the function was defined. You may want to put function_name into a file that you load into every namespace so you don’t have to remember to type `.[`function_name].

Note that that namespace that q breaks into is not necessarily the namespace in which the function is defined:

q).foo.baz: {break}
q).foo.baz[] {break;}
q))function_name .z.s
‘Not found (try a different namespace?)
q))\d .foo`.[`function_name] .z.s

Especially interesting, regarding this last example, is that function_name returns the correct function even though bar and baz have identical bodies:
{break}[bar] ~ string baz
1b ~ baz

Like printf, 0N! is your friend. When you place 0N! before an expression, it prints the result of that expression to the console and then returns it:

q)1 + 0N! til 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

0N! does change the functionality of your code in one subtle, albeit usually harmless, way. If you place 0N! before a modifier-assignment operator (e.g. +:, -:), then the result of that assignment is no longer null; it is the value assigned:

q){x +: 47} 1
q){0N! x +: 47} 1

See also: .Q.s and .Q.s1