Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Sep 6, 2006


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links - Products and Services

Debugging and Editing Tools for Instrumentation Programmers with LabWindows/CVI

15 ratings | 3.40 out of 5
Print

Overview


Experienced C programmers expect a certain level of flexibility in their source code development and debugging tools. LabWindows/CVI, a data acquisition and instrument control software development package, has interactive code generation and execution tools that make it easy to use for casual programmers as well as advanced software engineers to create programs under Windows.

Although LabWindows/CVI simplifies many programming tasks, you will find its source code development and debugging tools as flexible as those found in many general-purpose, professional C programming environments. This application note outlines the debugging and editing capabilities built into the LabWindows/CVI integrated environment and serves as an introduction to powerful new techniques for your source code development and debugging efforts. This document applies to LabWindows/CVI versions 3 through 5. For information about source code development and debugging in later versions of LabWindows/CVI, refer to the product documentation.

Introduction


Traditionally, C programmers are used to doing things on their own. Because C is such a well known standard, a programmer can use a wide variety of tools from different vendors to complete a project. A C programmer is free to use the best source code editor, compiler/linker, and library combination to fit his or her particular needs.

When programmers use a variety of tools to build an application, they must ensure that these tools are compatible with one another. For novice developers, the procedure for combining multiple pieces together to form a complete program can be daunting. Integrated programming environments such as LabWindows/CVI have evolved to simplify the C development cycle of editing, compiling, linking, and debugging source code.

Multifile Program Development



C is a language designed for multifile development. You can divide your source code into multiple C files, then compile and link the files together to form a single project or application. By separating code into multiple files, programmers can keep their projects manageable, reuse certain files in many different projects, and more easily maintain projects involving large teams of programmers. LabWindows/CVI is designed to take advantage of the multifile development capabilities of C without requiring much effort by the end-user. Therefore, all of the source code editing and debugging tools and utilities in LabWindows/CVI work across multiple files.

LabWindows/CVI has a variety of development tools for building a C program. When you launch LabWindows/CVI, you first see the Project window as shown in Figure 1. The Project window keeps track of all of the files that make up the current project under development. These files can consist of source (.c) files, include (.h) files, user interface (.uir) files, object modules (.obj), or static and import libraries (.lib).


Figure 1. The Project Window

Notice the Project window keeps track of status information for each file in your project. For example, you can quickly determine the time and date the file was last saved, whether the file has been compiled, and whether the file was associated with an instrument.

The primary focus of this application note is the LabWindows/CVI Source window, which is used for editing and debugging source and include files, and the Variable displays in LabWindows/CVI.

You can open Source windows easily by double-clicking on a source filename in the Project window. Because LabWindows/CVI is a windowing environment (under Microsoft Windows or Sun Solaris), you can open as many Source windows as you want. Whether your project consists of two source files or 20, you can view all of the source files in separate Source windows, all open at the same time.

The LabWindows/CVI Source Editor



The illustration in Figure 2 is a sample source file displayed in a LabWindows/CVI Source window. The Source window is like many other source code editors -- you type in ANSI C source code windows directly from the keyboard. You have an assortment of standard editing commands available to help, such as cut, copy, paste, find, and replace. Each of these commands operates on a single file, or multiple source files, when finding or replacing text. The numerical callouts shown in Figure 2 highlight some additional editing capabilities in LabWindows/CVI.

[+] Enlarge Image
Figure 2. Sample Source File

1.When dealing with multiple source or include files in your project, you often must quickly verify information in one of the separate files referred to in your source code. To open another Source window with a particular file displayed, you place your cursor on the filename and type <Ctrl-U> (or select Open Quoted Text under the File menu).

2.You can display line numbers, if you want to.

3.When working with large files, you may need to refer back to particular areas of the source file during development or debugging. You can mark these often-viewed areas with a tag. You press <F2> to jump to the next tag. Tag jumps can be within a single file, or across multiple files.

4.When working with a large file, you may want to view two different parts of the file at the same time. You drag the double line at the top of a window down to form two subwindows.

5.If you are calling a function and want to view the function definition, you highlight the function name and press <Ctrl-I>. The Window automatically jumps to the function definition so you can view the parameter prototypes and the function source code. This command finds the function description across multiple source files, if required.

6.You can place programmatic breakpoints anywhere in your source code by clicking in the far left column of the Source window with the left mouse button. Breakpoints are discussed in detail later in this application note.


Find
The Find command is one example of the flexible, multifile editing tools in LabWindows/CVI. In addition to the capabilities shown in the dialog box in Figure 3, you can use regular expressions to search for particular groups or combinations of characters. For example, the expression in the Find dialog box shown below finds all cases of the words if or else in the set of selected files.

Figure 3. Find Dialog Box

Balance
Balance is another editing tool designed specifically for C source files. Writing programs in C involves setting apart sections of source code in curly brackets ({ }). Many times the source code can have functions with several layers of nested brackets, as shown in Figure 4.

[+] Enlarge Image

Figure 4. Balance Editing Tool

One of the most tedious syntax problems C programmers encounter is when functions contain a different number of left and right curly brackets. Balance is a simple tool to help locate sections of code that have unbalanced left and right brackets. When you place the input cursor within a set of curly brackets and press <Ctrl-B>, LabWindows/CVI highlights all text between the left and right bracket. You can repeat this step to highlight the next set of brackets, until you are out of bracket sets. An unbalanced number of left and right brackets becomes obvious during this process, helping you locate the problem precisely. Balance also works between sets of square brackets or parentheses.

Diff
Another common task for C programmers developing multifile projects is source code control. LabWindows/CVI has a built-in Diff option for finding and merging differences between source files, as shown in Figure 5.

[+] Enlarge Image
Figure 5.Diff Utility

LabWindows/CVI Debugging Utilities


The tools for debugging programs in LabWindows/CVI can be broken down into three different categories: breakpoints, step execution modes, and variable displays. Most debugging efforts follow a similar procedure. When a run-time error occurs, you break execution of the program and step through the source code while viewing the values of program variables and arrays to find the programming errors. LabWindows/CVI saves all source code information after you compile your program, so you can break execution and step through the code if needed.

User Protection
One of the biggest features of the C language, and perhaps the biggest source of troublesome programming errors, is the flexible control users have over memory management. Every variable, array, or string that a C programmer wants to use in a program must first be set up in memory. Such flexibility requires a responsible programmer to ensure that the integrity of these memory locations is maintained.

For example, a developer may declare 10 bytes in memory for a string array that will be filled with information entered by the end-user during program execution. But, what happens if the end-user types in 12 bytes of information for the string? If the developer does not allow for this, it is possible that 12 bytes will be written to the 10 element array. In reality, the array is filled with valid data, and the two bytes adjacent to the end of the array are unintentionally written over, becoming invalid. This memory corruption may affect the program adversely on the next line, or thousands of lines later during run time, or not at all.

These types of memory errors, which can take days or even weeks to locate and fix in large programs, are particularly dangerous because they happen only during program execution under certain conditions. This makes them hard to reproduce and locate. LabWindows/CVI has built-in user protection to prevent programmers from being victimized by run-time memory violations. If you try to write 12 bytes to a 10-byte array during program execution, LabWindows/CVI automatically breaks with an error message and highlights the array in question. With user protection, LabWindows/CVI eliminates some of the most tedious and hard-to-find bugs before they occur.

Breakpoints
In most cases, the first step in tracking down and fixing bugs in a program is single stepping through the source code to locate the particular line containing the offending operation. To do this, you must be able to break program execution and single step through the source code. LabWindows/CVI has four different types of breakpoints:
  • Manual breakpoints -- At any time during program execution, type <Ctrl-Alt-SysRq> to break execution and highlight the current line in your source file.
  • Programmatic breakpoints -- Place a breakpoint (identified by a red diamond in the left column of a Source window) on a particular line in your source code. When program execution gets to that line, the program breaks.
  • Library error breakpoints -- You can set an option to break program execution whenever a LabWindows/CVI library function returns an error during run time. With this option, the LabWindows/CVI environment can do simple return value error checking for you.
  • Conditional breakpoints -- You can edit any programmatic breakpoint to break only when certain conditions are true. For example, the dialog box in Figure 6 illustrates how you can configure a breakpoint to break every third occurrence of ((x  + y / z) > 10) and (input == FALSE) at line 24 of the program.



Figure 6. Edit Breakpoint Dialog Box

Step Execution Mode
When program execution reaches a breakpoint, LabWindows/CVI automatically displays a Source window with the next line to be executed highlighted in a red box. At this point, you have a number of execution options. The following list contains a short description of each option.
  • Continue -- Continues executing the source code until another breakpoint is found or the program halts.
  • Go To Cursor -- Executes the source code from the current line to the line where the input cursor is placed.
  • Step Over -- Executes the current line of source code. If the current line contains a function call, execute the entire function and return.
  • Step Into -- Executes the current line of source code. If the current line contains a function call, jump to the first line of the function and break.
  • Finish Function -- If you have stepped into a function, selecting Finish Function automatically executes down to the end of the function, so you do not have to manually single step through the rest of the function.
  • Terminate Execution -- Halts program execution.

Variable Displays
LabWindows/CVI includes a collection of utilities for viewing the current value of variables and arrays used in your program. The Variable Display window, shown in Figure 7, displays the current value of the variables used in the program when execution is halted. If you want to view or edit the individual values of an array or string, double-click on the array name. A separate array or string display appears with the individual array elements, as shown in the Array Display image in Figure 8. If the array data is a waveform and you are interested in viewing its shape, you can view its graphical representation as shown in Figure 9.


[+] Enlarge Image

Figure 7. Variable Display



[+] Enlarge Image

Figure 8. Array Display of SignalArray





Figure 9. Graphical Array View of SignalArray



Watch Window
The Watch window is similar to a Variable Display in appearance. The difference is that the Watch window can display and update variable values or expressions during program execution. You can place any valid C expression using variables that are defined in your program in the Watch window and view them during program execution. You can place variables or expressions in the Watch window and watch them change as your program runs, without having to break program execution. Or, you can set the watch point to break program execution when the value of the variable or expression changes.

The dialog box, illustrated in Figure 9, shows a watch point in the Watch window. In this example, the Watch window will display the value of the expression SignalArray[2*i + j], and the program automatically halts whenever the value of this expression changes.



Figure 9. Add/Edit Watch Expression Dialog Box

Conclusion


LabWindows/CVI is a software development system designed specifically for scientists and engineers, so the focus is on making programming easy. Because applications created in LabWindows/CVI are built with the ANSI C programming language, the C tools and utilities for LabWindows/CVI users must meet expectations of experienced C programmers. The integrated debugging and editing tools in LabWindows/CVI make development and debugging a streamlined process both for casual and advanced C programmers.
15 ratings | 3.40 out of 5
Print

Reader Comments | Submit a comment »

graphics from windows 3.x?
- Oct 20, 2003

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).