Debugging Under The Hood Tim Chew Consultant Arch Signal Sdn Bhd.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Error-handling using exceptions
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Finding and Debugging Errors
16/27/2015 3:38 AM6/27/2015 3:38 AM6/27/2015 3:38 AMTesting and Debugging Testing The process of verifying the software performs to the specifications.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Dr. Pedro Mejia Alvarez Software Testing Slide 1 Software Testing: Building Test Cases.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Exceptions COMPSCI 105 S Principles of Computer Science.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
ASP.NET Programming with C# and SQL Server First Edition Chapter 6 Debugging and Error Handling.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
VB.Net - Exceptions Copyright © Martin Schray
Object Oriented Software Development 8. Exceptions, testing and debugging.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Problem of the Day  Why are manhole covers round?
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
Introduction to Exception Handling and Defensive Programming.
Automatic Diagnosis and Response to Memory Corruption Vulnerabilities Presenter: Jianyong Dai Jun Xu, Peng Ning, Chongkyung Kil, Yan Zhai, Chris Bookhot.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015.
Module 6: Debugging a Windows CE Image.  Overview Debug Zones IDE Debug Setup IDE Debug Commands Platform Builder Integrated Kernel Debugger Other Debugging.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Unit - V. Debugging GNU Debugger helps you in getting information about the following: 1.If a core dump happened, then what statement or expression did.
5.01 Understand Different Types of Programming Errors
Debugging What coders (programmers) do to find the errors (“bugs”) in their own programs “Bugs” – Admiral Grace Hopper, developer of the world’s first.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
1 Advanced.Net Debugging Using Visual Studio, R# and OzCode IT Week, Summer 2015.
Bugs CS100 how to prevent them, how to find them and how to terminate them.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Harvard Mark I Howard Aiken was a pioneer in computing and a creator of conceptual design for IBM in the 1940s. He envisioned an electro-mechanical computing.
Exception testing Aistis Karkauskas, IFM-2/2. Introduction  Exceptions – anomalous or exceptional events requiring special processing – often changing.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
1/21/2018 6:52 AM © Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN.
5.01 Understand Different Types of Programming Errors
Testing and Debugging.
Computer Programming I
LESSON 20.
Chapter 14: Exception Handling
5.01 Understand Different Types of Programming Errors
When your program crashes
Module 6: Debugging a Windows CE Image
Chapter 3 Debugging Section 3.4
Defensive Programming
Presentation transcript:

Debugging Under The Hood Tim Chew Consultant Arch Signal Sdn Bhd

Agenda Introduction Coding Tips Techniques Tools & Utilities Cases

Introduction

What is debugging? Attaching a debugger to your application and finding bugs Setting breakpoints in the debugger and watching variables

Introduction Two points of views: 1.Finding bugs in your program 2.Finding out why a program/application does what it does

Introduction Finding bugs in your program Narrower perspective Finding out why a program/application does what it does Wider perspective Opens the mind to a greater variety of techniques and avenues

Introduction Where’s the source? You may not always have the source code, and you still have to fix it Even if you don’t have the source code, the root or cause of the bug may still have to be determined

Introduction A little history… The computing term ‘bug’ is popularly attributed to Grace Hopper (Grandma Cobol) in the 1940’s A moth was found stuck in the Mark II computer at Harvard

Introduction

Coding Tips

Asserts Debug output Debug break Is the debugger attached? Compiler directives Parameters checking Exceptions

Coding Tips: Asserts An assert checks for a condition, and outputs a diagnostic message including the call stack if the condition is false. “Assert everything” ~ John Robbins

Coding Tips: Asserts Only assert that which would otherwise be a programming error In other words: If the assertion is false, it is a programming error Never assert expressions that are expected as part of normal data/code functionality Asserts only defined in debug builds (Compiler symbol: DEBUG)

Coding Tips: Debug Output “When in doubt, printf!” Write messages to the debugger Win32: OutputDebugString().NET: Debug.WriteXxx(), Trace.WriteXxx()

Coding Tips: Debug Output Writing to the console requires access to the standard output (stdout). Commonly, available in: –Debugger –Console You will not always be able to debug your program with a debugger attached –The debugger may throw off the speed of execution due to diagnostics which may prevent a bug from manifesting

Coding Tips: Debug Output –To inspect variables, you will need to break the execution, which may prevent a bug from manifesting. Common in UI and input device related bugs Printing messages is often useful to determine the order of the occurrence of specific 'events' Use DebugView from Sysinternals: –No need to attach debugger to process –Application can execute 'normally' Debug vs. Trace

Coding Tips: Debug Output Debug Defined in DEBUG builds Compiled when DEBUG symbol is defined Trace Defined in RELEASE builds

Coding Tips: Debug Break Hard breakpoint Win32: DebugBreak().NET: Debugger.Break() Useful when a debugger cannot be attached during execution Causes the system to prompt for attaching debugger at the point of the break Processor interrupt: INT 3

Coding Tips: Debugger Attached? Can be used to make the program behave differently: –Debugger not attached Output debug string –Debugger is attached Cause a break Windows NT has code which uses this technique when handling heap corruption

Coding Tips: Compiler Directives.NET #if #elif #else #endif Conditional-compile can only check for whether a symbol is defined –#if DEBUG –#if !SILVERLIGHT

Coding Tips: Check Parameters Validating parameters appropriately reduces potential for bugs You may still want to check certain expressions that have been asserted Should I check for null? Yes Should I check the validity of all objects? Use common sense Can the.NET allocator (new) fail? Yes! Do Memory squeezing!

Coding Tips: Exceptions Easy to abuse Try/catch blocks –Should I wrap everything in a try/catch block? NO –Only protect code where exceptions are expected Should I put gigantic try/catch gloves over my entire code base? –Do not try to cover for poor programming with exception handlers –Bad code that crashes SHOULD be allowed crash –It is not only easier to find bugs this way, but exceptions reduces code performance Unwinding objects and stack is expensive

Coding Tips: Exceptions Exceptions should only be thrown when an unexpected or invalid (an exceptional) event occurs that prevents code from continuing normally Exception handling introduces overhead and lowers performance and should not be used to control normal program flow (in place of conditional processing) It can also be difficult to maintain code that abuses exception handling in this way Ultimately: use Common Sense

Coding Tips Demo Asserts Debug strings Debug breaks Debugger presence Compiler directives

Techniques

Visual Studio IDE Debugger Tool windows: –Call Stack –Breakpoints –Immediate Window –Output –Watch –Modules –Threads –Registers –Memory

Techniques: No Source Code Knowledge of basic Intel x86 assembly required Set breakpoints in system and other binaries (DLL) Find loaded modules PE (Portable Executable) export table (EAT) Relative Virtual Address (RVA) System rebasing of DLL's Inspection of parameters on stack Function return value Use memory window in VS debugger to view data

Techniques Multithreaded Bugs Generally occur due to: Poor synchronization of access to resources Tip: Avoid multithreading access to primary display device

Demo Step into Win32 binaries –Find Win32 DLL exported functions Step into.NET assemblies –Microsoft.NET assemblies –Other.NET assemblies

Tools & Utilities Visual Studio IDE Debugger PEBrowsePro ( file-viewer.html) –PE Structure –Disassembler DebugView ( us/sysinternals/bb aspx) –Capture system debug strings ProcessExplorer ( us/sysinternals/bb aspx) –Examine system processes Spy++ –Examine system application windows

Cases

The worst kinds of bugs Optimization bugs Compiler bugs

C# Compiler bug Can compile the following expression correctly… a ^= b ^= a ^= b; But fails to generate the correct code! … Since 2003!

Microsoft.NET Common Language Runtime – Work Station (mscorwks.dll) Bug in module loading Potential root cause: deadlock when process made to load DLL immediately after startup (during startup process) Thread does not signal (exit) if waited on infinitely If thread (handle) is closed, process crashes

Cases Description: Stopped working Problem signature: Problem Event Name: APPCRASH Application Name: MyApp.exe Application Version: Application Timestamp: 49f5e887 Fault Module Name: mscorwks.dll 719EFF8A call dword ptr ds:[71EB3244h] 719EFF90 mov esi,eax > 719EFF92 mov eax,dword ptr [esi+0Ch]

Questions

Contact Thank you! Tim Chew Business: Personal: Blog: Slides: