The threads window displays the set of executing contexts on the target processor structured as a set of queues. The executing contexts are supplied to the threads window using a JavaScript program called threads.js that must be in the current active project. When the JavaScript program executes (when the application stops) it creates entries in the threads window that contain the name, priority and status of the thread together with the saved execution context (register state) of the thread. By double clicking on one of the entries in the threads window the debugger is located to it's saved execution context - you can put the debugger back into the default execution context using Show Next Statement.

#### Writing threads.js

The JavaScript program contained in threads.js must be have a named function called update which is called when the threads window is refreshed. The threads window is updated using the following JavaScript interface

    Threads.newqueue("queuename")
    Threads.add("threadname", threadpriority, "threadstate", registers)

The Threads.newqueue function takes a string argument and creates a new top level entry in the threads window. Subsequent threads that are added to this window will go under this.

The Threads.add function takes a string argument for the thread name, an integer argument for the thread priority, a string argument for the current state of the thread and finally an array (or null) containing the execution context of the thread (registers). The array containing the registers should contain the entries in the order they are displayed in the CPU registers display—typically this will be in register number order e.g. r0, r1, and so on.

To generate the thread lists you need to access the debugger from the JavaScript program. To do this you can use the JavaScript interface

    Debug.evaluate("expression");

which will evaluate the string argument as a debug expression and return the result. The returned result will be an object if you evaluate an expression that denotes a structure or an array. If the expression denotes an structure then each field can be accessed using the JavaScript array notation, for example:

    c = Debug.evaluate("complex");
    i = c["i"];
    j = c["j"];

Because JavaScript is a dynamic language, you can write the above in a more natural fashion:

    c = Debug.evaluate("complex");
    i = c.i;
    j = c.j;

You can access arbitrary memory locations using C style casts, for example:

    v = Debug.evaluate("*(unsigned*)0x200");

and similarly you can cast to user-defined types:

    v = Debug.evaluate("*(Complex*)0x200");

Note that you should ensure that the JavaScript program will terminate as if it goes into an endless loop then the debugger, and consequently CrossStudio, will become unresponsive and you will need to kill CrossStudio using a task manager.