Quantcast
Channel: Knowledge Junk » Automated Debugging
Viewing all articles
Browse latest Browse all 6

Automated Debugging: CS259 Problem Set 1

$
0
0

#!/usr/bin/env python
# Simple debugger
# See instructions around line 34
import sys
import readline

# Our buggy program
def remove_html_markup(s):
tag   = False
quote = False
out   = “”

for c in s:

if c == ‘<‘ and not quote:

tag = True

elif c == ‘>’ and not quote:

tag = False

elif c == ‘”‘ or c == “‘” and tag:

quote = not quote

elif not tag:

out = out + c

return out

# main program that runs the buggy program
def main():
print remove_html_markup(‘xyz’)
print remove_html_markup(‘”<b>foo</b>”‘)
print remove_html_markup(“‘<b>foo</b>'”)

# globals
breakpoints = {9: True}
stepping = False

def debug(command, my_locals):

global stepping
global breakpoints

if command.find(‘ ‘) > 0:

arg = command.split(‘ ‘)[1]

else:

arg = None

if command.startswith(‘s’):     # step

stepping = True
return True

elif command.startswith(‘c’):   # continue

stepping = False
return True

elif command.startswith(‘p’):    # print

#YOUR CODE HERE

elif command.startswith(‘q’):   # quit

sys.exit(0)

else:

print “No such command”, repr(command)

return False

commands = [“p”, “s”, “p tag”, “p foo”, “q”]

def input_command():

#command = raw_input(“(my-spyder) “)
global commands
command = commands.pop(0)
return command

def traceit(frame, event, trace_arg):

global stepping

if event == ‘line’:
if stepping or breakpoints.has_key(frame.f_lineno):

resume = False

while not resume:

print event, frame.f_lineno, frame.f_code.co_name, frame.f_locals
command = input_command()
resume = debug(command, frame.f_locals)

return traceit

 

# Using the tracer
sys.settrace(traceit)
main()
sys.settrace(None)

1. Add Print Command

*** INSTRUCTIONS ***
Improve and expand this function to accept a print command ‘p <arg>’. If the print command has no argument, print out the dictionary that holds all variables. Print the value of the supplied variable in a form ‘var = repr(value)’, if the ‘p’ is followed by an argument, or print ‘No such variable:’, arg if no such variable exists.

In this assignment, you are to build a response to a command from user that starts with ‘p’which will do a print statement of a variable if given or all the variables found during the tracing. Now, looking at a glance of the code, we need to add our code below this condition statement:

elif command.startswith(‘p’):    # print

Now, a basic logic that if user give an argument after he type ‘p’ then we need to find the variable given in the argument. Otherwise, we just report that the variable does not exist. So, lets just say there are two main condition: if ‘arg’ contains 2 tokens or 1 tokens. So let’s do that:

if length == 1:

#PRINT ALL VARIABLES

 

elif length == 2:

 

#PRINT A SPECIFIC VARIABLE

Now, to print all the variables, we have a dictionary list called ‘my_locals’ that contains all the variables we encountered during the trace. So, first task is easy, what about the second one. Well, we have to check if the variable exists from my_locals then if it exists, print out the variable and the value of it. So, here is the final code:

length = len(command.split(‘ ‘))

if length == 1:

print my_locals

elif length == 2:

if arg in my_locals:

print arg + ” = ” + repr(my_locals[arg])

else:

print ‘No such variable:’, arg

2. Add Breakpoint Command

“””
Our debug function
Improve and expand this function to accept  a breakpoint command ‘b <line>’. Add the line number to the breakpoints dictionary or print ‘You must supply a line number’  if ‘b’ is not followed by a line number.
“””

This one is easy, we have a variable ‘breakpoint’ and put an element according to the command pass. For example, b 5 means that we need to insert to the ‘breakpoint’ variable the integer: 5 with the value of True. So, let’s do that:

elif command.startswith(‘b’):    # breakpoint

if length == 1:

print “You must supply a line number”

else:

breakpoints[(int)(arg)] = True;

3. Add Watchpoint Command

“””
Our debug function
Improve and expand the debug function to accept  a watchpoint command ‘w <var name>’. Add the variable name to the watchpoints dictionary or print ‘You must supply a variable name’  if ‘w’ is not followed by a string.
“””

Same instruction, different variables to be put in. Now, we have to put the elements into the variable ‘watchpoint’ according to the command pass. Easy, right?

elif command.startswith(‘w’):    # watch variable

if length == 1:

print “You must supply a variable name”

else:

watchpoints[arg] = True;



Viewing all articles
Browse latest Browse all 6

Trending Articles