Sunteți pe pagina 1din 27

ni.

com

ni.com

Handy Tips & Tricks for Debugging


LabWindows/CVI Code
Johannes Turner
National Instruments

ni.com

Agenda
1.

2.
3.
4.

5.
6.

ni.com

Catching bugs at compile time


Catching bugs at run time
LabWindows/CVI Debugging Tools
Debugging Release Executables
Summary
Q&A

About Me

Working at National Instruments Romania (NIR) located in Cluj-Napoca.


With NI for 5 years.
Worked in CVI R&D since the CVI 2012 SP2 release.
LabWindows/CVI User Group Community interaction:

LabWindows/CVI Discussion Forums


LabWindows/CVI Idea Exchange

Interests: source code editors, music.

ni.com

Catching Bugs at Compile Time: Compiler Warnings

Customize using predefined warning level in Options Build Options.

ni.com

Catching Bugs at Compile Time: Compiler Warnings

Customize warnings for each warning level preset.

ni.com

Catching Bugs at Compile Time: Compiler Warnings

Assignments in conditionals
Uninitialized local variables
Unreferenced local variables
Unreachable code
Pointer and sign conversions
Improper char array initialization
Static array bounds checking
Dangling else constructs
Macro redefinition
Operator precedence problems

ni.com

Catching Bugs at Compile Time: Compiler Warnings

Improper char array initialization

char foo[2] = "bar";


int bar[10];
return bar[10];

Operator precedence problems

ni.com

Dangling else constructs

Static array bounds checking

int i;
i = 100 << 10+2;

if (a == 1)
if (b == 1)
a = 42;
else
b = 42;

Catching Bugs at Compile Time: Compiler Warnings

Assignments in conditionals
Uninitialized local variables
Unreferenced local variables
Unreachable code
Pointer and sign conversions
Improper char array initialization
Static array bounds checking
Dangling else constructs
Macro redefinition
Operator precedence problems

ni.com

10

Catching Bugs at Compile Time: Example 1

Using and customizing LabWindows/CVI compiler warnings when


diagnosing issues in your application.

ni.com

11

Catching Bugs at Run Time: Automatic Checking

Invalid pointer arithmetic or array indexing


Dereferencing invalid or freed pointers
Use of uninitialized local variables
Library function argument type checking

printf, SetCtrlAttribute

Library functions return error values

ni.com

Run Break On Library Errors

12

Catching Bugs at Run Time: Manual Checking

Asserts

Standard C macro (assert.h): assert(condition);


Tips:
o
o

ni.com

Test expectations and program invariants


Explicitly enforce assumptions your code makes

13

Catching Bugs at Run Time: Manual Checking

Error checking!

Check function return values for errors


Null-check memory allocations
errChk, nullChk
o

int Initialize (int panel, int ctrl) {


int error = 0;
errChk(SetCtrlVal(panel, ctrl, Hello, world!));

Error:
return error;
}

Automatic run-time checking is not a substitute for checking error


conditions in your code!

ni.com

14

Debugging Tools: The Bread and Butter

Inspect and modify variables

Examine stack trace

Variables window

Detect when values change

Variables window
Editable data tooltips

Watch expressions
Break On Change

Detect special cases

ni.com

Conditional breakpoints

Regular watch expression


Watch expression with Break On Change enabled
Watch expression with Break On Change enabled (HW).
Watch expression with Break On Change enabled (SW).

15

Debugging Tools: Other Handy Features

Change the point of execution

Inspect/modify memory

Memory window

Examine arrays graphically

Set Next Statement

Graphical Array View

Redraw UI while suspended

ni.com

Run Refresh User Interface (Ctrl + R)

16

Catching Bugs at Run-Time Time: Example 1 (Cont.)

Using the Graphical Array View


Debugging common C language pitfalls.

ni.com

17

More LabWindows/CVI-Specific Debugging Tips

Get more insight about symbols


with Watch Window Specific Type
1.

2.

ni.com

Add symbol to Watch Window


(Add Watch Expression
menu item)
Right-click watchpoint
Specific Type

18

Debugging Release Executables

Which EXE or DLL did the crash occur in?

Where in the EXE or DLL did the crash occur?


1.

2.

Look at the module list in the LabWindows/CVI debugger


Generate a map file for the EXE or DLL
Look up the address in the map file

Trace the execution of your EXE or DLL

ni.com

DebugPrintf

19

Debugging Release Executables: Example 2

List of loaded modules


Map files

ni.com

20

Debugging Release Executables

Understanding why does my project work in the debug configuration


but does not in the release configuration?

Data initialization in debug builds.


Code generation optimization
o

Conditional compilation
o
o

ni.com

Options Build Options Optimization level


Examine the preprocessor output
Compare different preprocessor outputs
Examine values and trace macro expansion: #pragma message, #error

Race conditions
Different timing of processed events
Communication with hardware
21

Debugging Release Executables: Example 2 (Cont.)

Analyzing the preprocessor output

ni.com

22

Other Advanced Debugging Tools

Windows Sysinternals

ni.com

http://www.sysinternals.com
DebugView
ProcessExplorer
Many more

23

Summary

Early detection of bugs at compile time using compiler warnings


Catching bugs at run time
Using the debugger
Debugging release executables

ni.com

24

Questions and Answers

ni.com

Thank you!

ni.com

ni.com

27

S-ar putea să vă placă și