Docsity

Debugging and Profiling: Understanding Breakpoints, Signals, and Valgrind - Prof. Westley , Study notes of Programming Languages

Partial preview of the text

Download Debugging and Profiling: Understanding Breakpoints, Signals, and Valgrind - Prof. Westley and more Study notes Programming Languages in PDF only on Docsity!

Profilers and Debuggers Profilers and Debuggers

Introductory Material

• First, who doesn’t know assembly language?

– You’ll get to answer all the assembly questions.

Yes, really.

• Lecture Style:

– “Sit on the table” and pose questions. So, wake

up!

• Lecture Goal:

– After the lecture you’ll think, “Wow, that was all

really obvious. I could have done that.”

Lecture Outline

• Debugging

– Signals

– How Debuggers Works

– Breakpoints

– Advanced Tools

• Profiling

– Event-based

– Statistical

What is a Debugger?

“A software tool that is used to detect the

source of program or script errors, by

performing step-by-step execution of

application code and viewing the content

of code variables.”

-MSDN

Signals

• A signal is an asynchronous notification sent

to a process about an event:

– User pressed Ctrl-C (or did kill %pid )

– Exceptions (divide by zero, null pointer)

– From the OS ( SIGPIPE )

• You can install a signal handler – a procedure

that will be executed when the signal occurs.

– Signal handlers are vulnerable to race conditions.

Why?

Signal Example

#include <stdio.h>

#include <signal.h>

int global = 11;

int my_handler() {

printf("In signal handler, global = %d\n",

global);

exit(1);

void main() {

int * pointer = NULL;

signal(SIGSEGV, my_handler) ;

global = 33;

* pointer = 0;

global = 55;

printf("Outside, global = %d\n", global);

• What does this

program print?

Building a Debugger

#include <stdio.h>
#include <signal.h>
#define BREAKPOINT *(0)=
int global = 11;
int debugger_signal_handler() {
printf(“debugger prompt: \n”);
// debugger code goes here!
void main() {
signal(SIGSEGV, debugger_signal_handler) ;
global = 33;
BREAKPOINT;
global = 55;
printf("Outside, global = %d\n", global);

• We can then get

breakpoints and

interactive

debugging

– Attach to target

– Set up signal

handler

– Add in exception-

causing

instructions

– Inspect globals,

etc.

Reality

• We’re not really changing

the source code

• Instead, we modify the

assembly

• We can’t insert instructions

– Because labels are already

set at known constant offsets

• Instead we change them

.file "example.c" .globl _global .data .align 4 _global: .long 11 .def ___main .section .rdata,"dr" LC0: .ascii "Outside, global = %d\12\0" .text .globl _main .def _main _main: pushl %ebp movl %esp, %ebp subl $24, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax call __alloca call ___main movl $33, _global movl $55, _global movl _global, %eax movl %eax, 4(%esp) movl $LC0, (%esp) call _printf leave ret .def _printf

Software Breakpoint Recipe

• Debugger has already attached and set up its

signal handler

• User wants a breakpoint at instruction X

• Store (X, old_instruction_at_X)

• Replace instruction at X with “*0=0”

– Pick something illegal that’s 1-byte long

• Signal handler replaces instruction at X with

stored old_instruction_at_X

• Give user interactive debugging prompt

Advanced Breakpoints

• Get register and local values by walking the stack

• Optimization: hardware breakpoints

– Special register: if PC value = HBP register value, signal

an exception

– Faster than software, works on ROMs, only limited

number of breakpoints, etc.

• Feature: condition breakpoint : “break at

instruction X if some_variable = some_value”

• As before, but signal handler checks to see if

some_variable = some_value

– If so, present interactive debugging prompt

– If not, return to program immediately

Watchpoints

• You want to know when a variable changes

• A watchpoint is like a breakpoint, but it

signals when the value at location L changes,

regardless of the current instruction

• How could we implement this?

Watchpoint Implementation

• Software Watchpoints

– Put a breakpoint at every instruction (ouch!)

– Check the current value of L against a stored

value

– If different, give interactive debugging prompt

– If not, set next breakpoint and continue

• Hardware Watchpoints

– Special register holds L: if the value at address L

ever changes, the CPU raises an exception

Q: Movies (284 / 842)

• Name the movie described below

and either the general scientific

theory that Malcolm invokes or the

ambushing cold-blooded killers. In

this Oscar-winning 1993

Spielberg/Crichton extravaganza

involving cloning and theme parks,

Dr. Ian Malcolm correctly predicts

that things will not turn out well.

Q: Movie Music (430 / 842)

• What reason did Dick Van Dyke's

character, in a 1964 Disney film,

give for his father giving his

"nose a tweak" and telling him

he was bad?