Main Content

Using the TLC Debugger

The TLC debugger helps you identify programming errors in your TLC code. Using the debugger, you can execute TLC code line-by-line, analyze and/or change variables in a specified block scope, and view the TLC call stack. The TLC debugger has a command-line interface that provides commands similar to standard debugging tools such as dbx or gdb.

Tips for Debugging TLC Code

Here are a few tips that will help you to debug your TLC code:

  1. To see the full TLC call stack, place the following statement in your TLC code before the line that is pointed to by the error message. This will be helpful in narrowing down your problem.

    %setcommandswitch "-v1"
  2. To trace the value of a variable in a function, place the following statement in your TLC file:

    %trace This is in my function %<variable>

    Your message will appear when the Target Language Compiler is run with the -v command switch, but not otherwise. You can use %warning instead of %trace to print variables, but you will need to remove or comment out such lines after you are through debugging.

  3. Use the TLC coverage log files to identify parts of your code that have not been reached.

Invoking the Debugger

Use the TLC debugger to identify bugs and potential problems in your TLC files. To use the TLC debugger:

  1. On the Configuration Parameters dialog box, select Retain .rtw file. This prevents the model.rtw file from being deleted after code generation.

  2. Select Start TLC debugger when generating code to invoke the TLC debugger when starting the code generation process.

    Selecting Start TLC debugger when generating code is equivalent to specifying the TLC option -dc on the Code Generation pane of the Configuration Parameters dialog box.

  3. Apply changes and start code generation by pressing Ctrl+B. This stops at the first line of executed TLC code, breaks into the TLC command-line debugger, and displays the following prompt:

    TLC_DEBUG>

You can now set breakpoints, explore the contents of code generator files, and explore variables in your TLC file using print, which, or whos.

An alternative way to invoke the TLC debugger is from the MATLAB® prompt. (This assumes you retained the model.rtw file in the project folder.) To avoid making mistakes, copy the tlc command output of the build process to the MATLAB Command Window, and issue it after appending -dc to that command line.

A complete list of command-line switches for the TLC debugger is available in the table Target Language Compiler Switches.

TLC Debugger Command Summary

The table TLC Debugger Commands summarizes the TLC debugger commands.

To obtain more detailed help on individual commands, use the syntax

help command

from within the TLC debugger, as in this example:

TLC-DEBUG> help clear

You can abbreviate a TLC debugger command to its shortest unique form. For example,

TLC-DEBUG> break warning

can be abbreviated to

TLC-DEBUG> br warning

To view a complete list of TLC debugger commands, type help at the TLC-DEBUG> prompt.

TLC Debugger Commands

CommandDescription
assign variable=value

Change a variable in the running program.

break ["filename":]line|error|warning|
trace|function

Set a breakpoint. See also %breakpoint Directive.

clear [breakpoint#|all]

Remove a breakpoint.

condition [breakpoint#] [expression]

Attach a condition to a breakpoint.

continue ["filename":]line|function

Continue from a breakpoint.

disable [breakpoint#]

Disable a breakpoint.

down [n]

Move down the stack.

enable [breakpoint#]

Enable a breakpoint.

finish

Break after completing the current function.

help [command]

Obtain help for a command.

ignore [breakpoint#]count

Set the ignore count of a breakpoint.

iostack

Display contents of I/O stack.

list start[,end]

List lines from the file from start to end.

loadstate "filename"

Load debugger breakpoint state from a file.

next

Single step without going into functions.

print expression

Print the value of a TLC expression. To print a record, you must specify a fully qualified scope such as CompiledModel.System[0].Block[0].

quit

Quit the TLC debugger. You can also exit the debugger by typing Ctrl+C at the prompt.

run "filename"

Run a batch file of command-line debugger commands.

savestate "filename"

Save debugger breakpoint state to a file.

status

Display a list of active breakpoints.

step

Step into.

stop ["filename":]line|error|warning|trace|
function

Set a breakpoint (same as break).

tbreak ["filename":]line|function

Set a temporary breakpoint.

thread [n]

Change the active thread to thread #n (0 is the main program’s thread number).

threads

List the currently active TLC execution threads.

tstop ["filename":]line|function

Set a temporary breakpoint.

up [n]

Move up the stack.

where

Show the currently active execution chains.

which name

Look up the name and display what scope it comes from.

whos [::|expression]

List the variables in the given scope.

%breakpoint Directive

As an alternative to the break command, you can embed breakpoints at locations in a TLC file by adding the directive

%breakpoint

Usage Notes

When using break or stop, use

  • error to break or stop on error.

  • warn to break or stop on warning.

  • trace to break or stop on trace.

For example, if you need to break on error, use

TLC_DEBUG> break error

When using clear, get the status of breakpoints using status and clear specific breakpoints. For example,

TLC-DEBUG> break "foo.tlc":46
TLC-DEBUG> break "foo.tlc":25
TLC-DEBUG> status
Breakpoints:
[1] break File: foo.tlc Line: 46
[2] break File: foo.tlc Line: 25
TLC-DEBUG> clear 2

In this example, clear 2 clears the second breakpoint.

Related Topics