MPLAB X Debugging Techniques

Similar documents
MPLAB X Debugging Techniques

MPLAB X Debugging Techniques

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

Chapter 15 Debugging

Program Correctness and Efficiency. Chapter 2

MPLAB SIM. MPLAB IDE Software Simulation Engine Microchip Technology Incorporated MPLAB SIM Software Simulation Engine

CMPE-013/L. Introduction to C Programming

Lecture 07 Debugging Programs with GDB

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

QUIZ. What is wrong with this code that uses default arguments?

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

MISRA-C. Subset of the C language for critical systems

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Introduction to Problem Solving and Programming in Python.

CSE 331 Software Design & Implementation. Hal Perkins Fall 2016 Debugging

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Chapter 5 Errors. Bjarne Stroustrup

Verification and Validation

Debugging INTRODUCTION DEBUGGER WHAT IS VBA'S DEBUGGING ENVIRONMENT?

Important From Last Time

Reviewing gcc, make, gdb, and Linux Editors 1

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Debugging with gdb and valgrind

Keil uvision development story (Adapted from (Valvano, 2014a))

Regression testing. Whenever you find a bug. Why is this a good idea?

Static Program Analysis Part 1 the TIP language

We do not teach programming

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

QUIZ. What are 3 differences between C and C++ const variables?

Chapter 8. Achmad Benny Mutiara

A Fast Review of C Essentials Part I

UNIT-4 (COMPILER DESIGN)

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

QUIZ. Source:

Secure Programming I. Steven M. Bellovin September 28,

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

BLM2031 Structured Programming. Zeyneb KURT

Semantic Analysis. Lecture 9. February 7, 2018

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

Oracle Developer Studio Code Analyzer

The First Real Bug. gdb. Computer Organization I McQuain

CMSC 132: OBJECT-ORIENTED PROGRAMMING II

The Mercury project. Zoltan Somogyi

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

PROGRAMMAZIONE I A.A. 2017/2018

18-600: Recitation #3

Basic concepts. Chapter Toplevel loop

About this exam review

CS201 Lecture 2 GDB, The C Library

Reliable programming

CSE 331 Software Design & Implementation

Part 5. Verification and Validation

Programming Languages Third Edition. Chapter 7 Basic Semantics

IDE for medical device software development. Hyun-Do Lee, Field Application Engineer

printf( Please enter another number: ); scanf( %d, &num2);

Systems software design. Software build configurations; Debugging, profiling & Quality Assurance tools

Chapter 9: Dealing with Errors

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Eliminating Memory Leaks in Symbian OS C++ Projects

Lecture 9: July 14, How to Think About Debugging

Debug Tool Fashion Accessories An showcase of development tool accessories designed to enhance your debug session

CS2141 Software Development using C/C++ Debugging

Changing the Embedded World TM. Module 3: Getting Started Debugging

Topics in Software Testing

EW The Source Browser might fail to start data collection properly in large projects until the Source Browser window is opened manually.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Java Programming. Zheng-Liang Lu. Java 308 Spring Department of Computer Science & Information Engineering National Taiwan University

CS 11 C track: lecture 6

H8 C-SPY. User Guide WINDOWS WORKBENCH VERSION

DETERMINISTICALLY TROUBLESHOOTING NETWORK DISTRIBUTED APPLICATIONS

This paper was presented at DVCon-Europe in November It received the conference Best Paper award based on audience voting.

MPLAB C1X Quick Reference Card

Development Tools. 8-Bit Development Tools. Development Tools. AVR Development Tools

Eliminate Memory Errors to Improve Program Stability

We are built to make mistakes, coded for error. Lewis Thomas

More C Pointer Dangers

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSE 331 Software Design and Implementation. Lecture 16 Debugging

CS2: Debugging in Java

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW

Page 1. Last Time. Today. Embedded Compilers. Compiler Requirements. What We Get. What We Want

embos Real Time Operating System CPU & Compiler specifics for RENESAS M16C CPUs and HEW workbench Document Rev. 1

So far, we know: Wednesday, October 4, Thread_Programming Page 1

Alan J. Perlis - Epigrams on Programming

Release Bugs solved

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Program Verification. Aarti Gupta

Learning to Program with Haiku

Allinea DDT Debugger. Dan Mazur, McGill HPC March 5,

Debugging Page 1. Current context. Monday, October 15, :38 PM

Name: Class: Date: 2. Today, a bug refers to any sort of problem in the design and operation of a program.

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Transcription:

TLS0102-001 MPLAB X Debugging Techniques The Debugging Process Author: Rob Ostapiuk, Stu Chandler Microchip Technology

Objectives! Describe a generic process for debugging a program! Describe at a high level various techniques that may be employed in the debug process! Implement these techniques in MPLAB X "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 2

Development Tools Data Flow Debug and Test Points Debuggable Code Instrumentation src Source Files Header Files Preprocessor Compiler Macros Compiler Flags Static Checkers Selected Libraries lib Linker Library Files Debug Executive hex Linker Options Code Instrumentation Debugger Programmer MPLAB REAL ICE ACTIVE STATUS FUNCTION RESET TM Memory View, Stack View, Profiling Inputs Input Test Data Outputs 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 3

The Four Steps of the Debugging Process BUILD SUCCESSFUL Now what?

The Four Steps The Scientific Method of Debugging Testing Stabilization Localization Correction "When the software is 95% complete, there's still 25% to go." Anonymous 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 5

The Debugging Process Step 1: Testing Definition Testing is the process of exercising a program by injecting a wide range of input values to uncover limitations of the program and to verify that the program behaves according to its design specifications.! Testing Toolbox! MPLAB X IDE Simulator (MPSIM)! MDB scriptable command line debugger! SCL (Simulator Command Language) Stimulus Scripts! In-Circuit Debugger (PICkit, ICD, Real ICE, etc.)! DMCI (Data Monitor and Control Interface) Plug-in! Try to break the program! Exercise all features with variety of inputs 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 6

The Debugging Process Step 2: Stabilization Definition Stabilization is the attempt to control conditions such that a bug may be reproduced on demand, even when using instrumentation (trace/log).! Stabilization Toolbox! Same as testing toolbox, plus! Breakpoints and watches! Instrumented Trace and Log macros! Instrumentation with printf() and similar functions! Determine the specific input values or range of input values that reliably produce incorrect outputs make the bug easily repeatable 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 7

Definition The Debugging Process Step 3: Localization Localization is the search for a bug's cause once its result has been identified and made repeatable on demand and is characterized by experimentation and data collection.! Localization Toolbox! Same as stabilization toolbox, plus! Call Stack! Default Interrupts and Traps! Collect and analyze data about bug! Construct a hypothesis describing bug's cause! Test hypothesis to narrow search for bug! Localize bug to a code segment, or variable 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 8

The Debugging Process Step 4: Correction Definition Correction is the task of fixing a bug once its source has been located. Often, correction is trivial but may be more complex if the bug is due to faulty program design.! Correction Toolbox! Your programming language knowledge! Good software design practices! Some fixes are trivial and low cost! Logical errors, language misuse, etc.! Some fixes are difficult and expensive! Fundamental program design flaw, misinterpretation or omission of program specifications, etc. 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 9

The Proximity Principle Cause and effect are closer than you think

The Proximity Principle and the types of proximity Definition Proximity Principle: The effect of a bug, is by some measure close to its cause, whether it be lexical, temporal, or referential.! Types of Proximity! Lexical The effect of a bug follows a short distance after its cause in the source code (compile time or runtime)! Temporal The effect of a bug follows immediately after its cause during execution of the program (run time)! Referential Two segments of code (perhaps separated by space and/or time) both reference the same item (e.g. variable) without any other references in between (run time) 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 11

Lexical Proximity Example void main(int) { foo(); bar(); }! The function calls to foo() and bar() are sequentially executed and are physically near each other in the code! Lexical proximity used for finding compile time errors other types exist only at run time 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 12

Temporal Proximity Example while (1) { } foo(); bar();! The first and last lines of the while loop are lexically distant (could have hundreds of lines between them)! The first line executes immediately after the last line at run time as the loop returns to the top 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 13

Referential Proximity Example void main(int) { x = foo(); bar(); if (x == 1) {! The references to x on the first and last lines are separated lexically and temporally! Because there are no references to x in between, the first and last lines are referentially close 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 14

Error Classification "Any sufficiently advanced bug is indistinguishable from a feature." - Rich Kulawiec

Error Classification Ranked by Ease of Automated Testing Methods Syntax and Lexical Logic (Semantic) Runtime (Execution) Latent "Computers are good at following instructions, but not at reading your mind" Donald Knuth 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 16

Syntax and Lexical Errors Definition Syntax Errors are the result of legal symbols being combined in illegal ways. Lexical Errors are the result of using symbols that are not legally part of the language.! TEST BASIS: Language Specification! Syntax and Lexical errors, while different are closely related and generally come from:! Misunderstanding how to use the language properly! Typographical Errors! Show up at compile time or at link time! Program will not build with these errors 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 17

Example: Syntax Error Syntax and Lexical Errors Examples int x = 5 int y = 6; int z = 0; Missing semicolon illegally combines first two lines into: int x = 5 int y = 6; z = x + y; Example: Lexical Error int x@ = 5; foo y = 42; @ is not used in ANSI C and is not allowed in identifiers foo is not a data type (unless defined with typedef) 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 18

Logic / Semantic Errors Definition Logic Errors are present when a program or function runs to completion, but produces an incorrect result. These are sometimes called Intent Errors.! TEST BASIS: Original Programming Specification! Logic errors may come from:! Design Error (beyond scope of this class)! Implementation Error (didn't follow spec)! Typographical Error (e.g. used & instead of &&)! Can be very painful and costly to fix if at the design or implementation level 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 19

Logic / Semantic Errors Examples! It's a simple design, what could possibly go wrong? SW1 ON OFF SW2 ON OFF THINGAMAJIG SW1 SW2 MODE OFF OFF DISABLED OFF ON PASSIVE ON OFF ACTIVE if ((sw1 = OFF) && (sw2 = OFF)) DisableThingamajig(); if (sw1 == ON) EnableThingamajig(ACTIVE); if (sw2 == ON) EnableThingamajig(PASSIVE);! Design error? [~1]! Implementation error(s)? [~1]! Typographical error(s)? [2] 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 20

Runtime / Execution Errors Definition Runtime Errors are errors that only become apparent when the program is running. They occur when a programmer asks in a perfectly legal way for something impossible or illogical to be done.! TEST BASIS: High-Level Language and Machine- Level Semantics (Effect of Operations)! Difficult to catch in C due to its lack of runtime range checking - program may run a long time before error is serious enough to be detected! Caused by things like exceeding array bounds! Manifests by things like divide by zero, illegal address, etc. often caught by hardware traps 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 21

Example: Runtime Error Runtime / Execution Errors Examples int *p; int x; Pointer is not initialized x = *p; Example: Runtime Error int MyArray[10]; Array bounds will be exceeded by 1 for (int i = 0; i = 10; i++) { MyArray[i] = 0; } 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 22

Latent Errors Definition Latent Errors are errors that only become apparent when specific data or inputs are used.! TEST BASIS: All possible input conditions! Extremely difficult to catch in some cases! May not show up for a long time, if ever! Example: inverse = 1/(x y); will cause a divide by zero error when x = y "Testing can only prove the presence of bugs, not their absence. Edsger W. Dijkstra (Turing Award Recipient-1972) 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 23

Error Classification Ranked by Ease of Localization Bug Stands Still Bug is Input Dependent Bug is Code Sensitive Bug is Environment Sensitive "To understand a program you must become both the machine and the program Alan Perlis (First Turing Award Recipient- 1966) 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 24

Bug Stands Still! Independent of everything around them! Symptoms will be present regardless of:! Program inputs! Startup conditions or path taken in code! Trace or log macros used in debugging! Easy to localize! Syntax / Lexical errors always stand still! Other types may stand still too 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 25

Input Dependent Bugs! Symptoms change when input changes! Difficult to localize and identify without the offending input data! Trace can be useful! Simulator or hardware debugger critical to finding error 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 26

Code Sensitive Bugs! Symptoms change when code changes! Classic example when program fails in release mode but works in debug mode or when debug statements added (e.g. printf, trace, log, etc.! Sometimes called "Heisenbugs" - The closer you get to them, the harder it is to make them appear 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 27

Environment Sensitive Bugs! Symptoms seem to change randomly, but are due to subtle changes in hardware environment! Example: Runs fine immediately after compiled, but fails on subsequent runs (system resets often fix problem)! Possible causes:! Uninitialized variables! Differences in state of peripheral! Mishandled interrupts 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 28

Ensure Debugging Info Is Generated (Should be set by default) Project Properties In the compiler settings, ensure that Generate debugging info is checked. This will generate an.elf file (or.coff file) to allow debugging of code in MPLAB X IDE. 2014 Microchip Technology Incorporated. All Rights Reserved. Slide 29