Top Banner
Nevena Milojković Software Design and Evolution 10. Dynamic Analysis Based on the slides of Jorge Ressia
75

Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Mar 13, 2018

Download

Documents

vanphuc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Nevena Milojković

Software Design and Evolution

10. Dynamic Analysis

Based on the slides of Jorge Ressia

Page 2: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic Analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Roadmap

2

Page 3: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

3

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 4: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

What does this class do?

package org.jhotdraw.standard;public class CreationTool extends AbstractTool {private List fAddedFigures;private Figure fCreatedFigure; private Figure myAddedFigure; private Figure myPrototypeFigure; public CreationTool(DrawingEditor newDrawingEditor, Figure prototype) {super(newDrawingEditor); setPrototypeFigure(prototype);} protected CreationTool (DrawingEditor newDrawingEditor) {this(newDrawingEditor, null);} public void activate() {super.activate(); if (isUsable()) {getActiveView().setCursor(new AWTCursor (java.awt.Cursor.CROSSHAIR_CURSOR)); setAddedFigures (CollectionsFactory.current().createList());}public void deactivate() {setCreatedFigure(null); setAddedFigure(null); setAddedFigures(null); super.deactivate();} public void mouseDown(MouseEvent e, int x, int y) {super.mouseDown(e, x, y); setCreatedFigure(createFigure()); setAddedFigure (getActiveView().add(getCreatedFigure())); getAddedFigure().displayBox(new Point(getAnchorX(), getAnchorY()), new Point(getAnchorX(), getAnchorY()));} protected Figure createFigure() {if (getPrototypeFigure() == null) {throw new JHotDrawRuntimeException("No protoype defined");}return (Figure)getPrototypeFigure().clone();}}

CreationTool class from JHotDraw system4

Page 5: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

public abstract class AbstractFigure implements Figure {

private transient FigureChangeListener fListener;private List myDependendFigures;private static final long serialVersionUID = -10857585979273442L;private int abstractFigureSerializedDataVersion = 1;private int _nZ;

protected AbstractFigure() { myDependendFigures = CollectionsFactory.current().createList();

}

public void moveBy(int dx, int dy) {willChange();basicMoveBy(dx, dy);changed();

}

protected abstract void basicMoveBy(int dx, int dy);

public void displayBox(Point origin, Point corner) {willChange();basicDisplayBox(origin, corner);changed();

}

public abstract void basicDisplayBox(Point origin, Point corner);

public abstract Rectangle displayBox();

public abstract HandleEnumeration handles();

public FigureEnumeration figures() {return FigureEnumerator.getEmptyEnumeration();

} …}

What does this class do?

Page 6: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Finding Features

Software Feature:A distinguishing characteristic of a software item.

IEEE 8296

Page 7: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Finding Features

7

Page 8: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

What is Dynamic Analysis?

Dynamic analysis is the investigation of the properties of a software system during run-time.

8

Static analysis examines the program code alone.

Page 9: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

12.

Properties of a software system are represented by the system behaviour

System behaviour is established by methods

Page 10: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

12.

Why is static analysis not enough?

Page 11: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

UML example from JHotDraw

AttributeFigure

+basicDisplayBox(origin:Point, corner:Point)

RectangleFigure+basicDisplayBox(origin:Point, corner:Point)

PolygonFigure

+basicDisplayBox(origin:Point, corner:Point)

TextAreaFigure

+basicDisplayBox(origin:Point, corner:Point)

HTMLTextAreaFigurePolygonFigureGeometricAdapter

DiamondFigure

DiamondFigureGeometricAdapter

+ basicDisplayBox(origin: Point, corner: Point)+ displayBox(origin: Point, corner: Point)

AbstractFigure

basicDisplayBox(origin: Point, corner: Point)

<<interface>>Figure

+basicDisplayBox(origin:Point, corner:Point)

GraphicalCompositeFigure

CompositeFigure

+basicDisplayBox(origin:Point, corner:Point)

GroupFigure+basicDisplayBox(origin:Point, corner:Point)

PertFigure+basicDisplayBox(origin:Point, corner:Point)

StandardDrawing

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

Page 12: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Different static analysis techniques

CHARTACTAMTAFTAXTAk-CFA

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

Page 13: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Symbolic execution

> Assigning the symbolic values to the variables, rather than concrete

> Executing the program with symbolic values, and figuring out which value causes which program path to execute

Page 14: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

CHA algorithm

AttributeFigure

+basicDisplayBox(origin:Point, corner:Point)

RectangleFigure+basicDisplayBox(origin:Point, corner:Point)

PolygonFigure

+basicDisplayBox(origin:Point, corner:Point)

TextAreaFigure

+basicDisplayBox(origin:Point, corner:Point)

HTMLTextAreaFigurePolygonFigureGeometricAdapter

DiamondFigure

DiamondFigureGeometricAdapter

+ basicDisplayBox(origin: Point, corner: Point)+ displayBox(origin: Point, corner: Point)

AbstractFigure

basicDisplayBox(origin: Point, corner: Point)

<<interface>>Figure

+basicDisplayBox(origin:Point, corner:Point)

GraphicalCompositeFigure

CompositeFigure

+basicDisplayBox(origin:Point, corner:Point)

GroupFigure+basicDisplayBox(origin:Point, corner:Point)

PertFigure+basicDisplayBox(origin:Point, corner:Point)

StandardDrawing

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

Page 15: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

CHA algorithm

AttributeFigure

+basicDisplayBox(origin:Point, corner:Point)

RectangleFigure+basicDisplayBox(origin:Point, corner:Point)

PolygonFigure

+basicDisplayBox(origin:Point, corner:Point)

TextAreaFigure

+basicDisplayBox(origin:Point, corner:Point)

HTMLTextAreaFigurePolygonFigureGeometricAdapter

DiamondFigure

DiamondFigureGeometricAdapter

+ basicDisplayBox(origin: Point, corner: Point)+ displayBox(origin: Point, corner: Point)

AbstractFigure

basicDisplayBox(origin: Point, corner: Point)

<<interface>>Figure

+basicDisplayBox(origin:Point, corner:Point)

GraphicalCompositeFigure

CompositeFigure

+basicDisplayBox(origin:Point, corner:Point)

GroupFigure+basicDisplayBox(origin:Point, corner:Point)

PertFigure+basicDisplayBox(origin:Point, corner:Point)

StandardDrawing

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

Page 16: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

RTA algorithm

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

AttributeFigure

+basicDisplayBox(origin:Point, corner:Point)

RectangleFigure+basicDisplayBox(origin:Point, corner:Point)

PolygonFigure

+basicDisplayBox(origin:Point, corner:Point)

TextAreaFigure

+basicDisplayBox(origin:Point, corner:Point)

HTMLTextAreaFigurePolygonFigureGeometricAdapter

DiamondFigure

DiamondFigureGeometricAdapter

+ basicDisplayBox(origin: Point, corner: Point)+ displayBox(origin: Point, corner: Point)

AbstractFigure

basicDisplayBox(origin: Point, corner: Point)

<<interface>>Figure

+basicDisplayBox(origin:Point, corner:Point)

GraphicalCompositeFigure

CompositeFigure

+basicDisplayBox(origin:Point, corner:Point)

GroupFigure+basicDisplayBox(origin:Point, corner:Point)

PertFigure+basicDisplayBox(origin:Point, corner:Point)

StandardDrawing

Page 17: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

RTA algorithm

AttributeFigure

+basicDisplayBox(origin:Point, corner:Point)

RectangleFigure+basicDisplayBox(origin:Point, corner:Point)

PolygonFigure

+basicDisplayBox(origin:Point, corner:Point)

TextAreaFigure

+basicDisplayBox(origin:Point, corner:Point)

HTMLTextAreaFigurePolygonFigureGeometricAdapter

DiamondFigure

DiamondFigureGeometricAdapter

+ basicDisplayBox(origin: Point, corner: Point)+ displayBox(origin: Point, corner: Point)

AbstractFigure

basicDisplayBox(origin: Point, corner: Point)

<<interface>>Figure

+basicDisplayBox(origin:Point, corner:Point)

GraphicalCompositeFigure

CompositeFigure

+basicDisplayBox(origin:Point, corner:Point)

GroupFigure+basicDisplayBox(origin:Point, corner:Point)

PertFigure+basicDisplayBox(origin:Point, corner:Point)

StandardDrawing

new TextAreaFigure();new GroupFigure();…Figure f = fe.nextFigure();f.basicDisplayBox(partOrigin, corner);

Page 18: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Why Dynamic Analysis?

Gap between run-time structure and code structure in OO programs

Trying to understand one [structure] from the other is like trying to understand the dynamism of living ecosystems from the static taxonomy of plants and animals, and vice-versa.

— Erich Gamma et al., Design Patterns18

Page 19: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Application

> Software understanding> Software testing

Page 20: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

20

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 21: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Runtime Information Sources

> tracing method execution> tracing values of variables> tracing memory usage

21

Page 22: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Two ways of getting the information

> External—execute program and collect the information from

outside> Internal

—instrument program, and get the information from inside

22

Page 23: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

External View

> System.out.println

> Examine logs

> Analyse used resources> CPU and memory usage> Open files

23

Page 24: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Internal View

Log statementsin code Stack trace

Debugger

Many different tools are based on tracing: execution profilers, test coverage analysers, tools for reverse engineering…

Execution trace

24

Page 25: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Execution Tracing

How can we capture full program execution?

25

Trace entry and exit of methods

Additional information:- receiver and arguments- return values- fields assigning- class instantiations

Page 26: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Tracing Techniques

• Instrumentation approaches—Source code modification—Byte code modification—Wrapping methods (Smalltalk)

• Simulate execution (using debugger infrastructure)

26

Page 27: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Technical Challenges

> Instrumentation influences the behaviour of the > execution

> Overhead: increased execution time> Large amount of data

27

Page 28: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

28

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 29: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Feature Analysis

29

Page 30: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Loggers - low tech debugging

“…debugging statements stay with the program; debugging sessions are transient. “

Kerningham and Pike

public class Main {

public static void main(String[] args) {Clingon anAlien = new Clingon();System.out.println(“in main“); anAlien.spendLife();}

}

30

Page 31: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Smalltalk Mechanisms

> become: function> Method Wrappers> Anonymous Classes

31

Page 32: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

become: function

• primitive function• swaps the object pointers of the receiver and the argument and all variables in the system that used to point to the receiver now point to the argument, and vice-versa.

Page 33: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Method Wrappers

A MethodWrapper replaces an original CompiledMethod in the method dictionary of a class and wraps it by performing some before and after actions.

Page 34: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Anonymous Classes

To intercept the behaviour of the object, we need to create an anonymous subclass of its class and override a method whose behaviour we want to inspect.

Page 35: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Sub-method Feature Analysis

35

Page 36: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Sub-method Feature Analysis

Bytecode Instrumentation

36

Page 37: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Bytecode Instrumentation

Smalltalk

37

Page 38: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

> Smalltalk code:

> Symbolic Bytecode

Example: Number>>asInteger

Number>>asInteger"Answer an Integer nearestthe receiver toward zero."

^self truncated

9 <70> self10 <D0> send: truncated11 <7C> returnTop

38

Page 39: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Example: Step by Step

> 9 <70> self—The receiver (self) is pushed on the stack

> 10 <D0> send: truncated—Bytecode 208: send literal selector 1—Get the selector from the first literal—Start message lookup in the class of the object that is on

top of the stack—result is pushed on the stack

> 11 <7C> returnTop—return the object on top of the stack to the calling method

39

Page 40: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Reflectivity

> Reflectivity is a tool to annotate AST nodes with metalinks.

> A metalink is a message sent to an arbitrary object.

> A metalink can be executed before a node, instead a node, after a node.

http://smalltalkhub.com/#!/~RMoD/Reflectivity

Page 41: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

ByteSurgeon

> Enables runtime bytecode transformations for Smalltalk> Provides high-level API > Complements the reflective ability of Smalltalk with the

possibility to instrument method

> Runtime transformation needed for —Adaptation of running systems —Tracing / debugging

41

Page 42: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Example: Logging

> Goal: logging message send.> First way: Just edit the text:

42

Page 43: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging with ByteSurgeon

> Goal: Change the method without changing program text> Example:

43

Page 44: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging: Step by Step

44

Page 45: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging: Step by Step

> instrumentSend: —takes a block as an argument —evaluates it for all send bytecodes

45

Page 46: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging: Step by Step

> The block has one parameter: send > It is executed for each send bytecode in the method

46

Page 47: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging: Step by Step

> Objects describing bytecode understand how to insert code —insertBefor —insertAfter —replace

47

Page 48: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Logging: Step by Step

> The code to be inserted.> Double quoting for string inside string

– Transcript show: ’sending #test’

48

Page 49: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

ByteSurgeon Usage

> On Methods or Classes:

> Different instrument methods: —instrument: —instrumentSend: —instrumentTempVarRead:—instrumentTempVarStore: —instrumentTempVarAccess: —same for InstVar

49

Page 50: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Advanced ByteSurgeon

> Goal: extend a send with after logging

50

Page 51: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Advanced ByteSurgeon

> With ByteSurgeon, something like:

> How can we access the receiver of the send? > Solution: Metavariable

51

Page 52: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Advanced ByteSurgeon

> With Bytesurgeon, something like:

> How can we access the receiver of the send? > Solution: Metavariable

52

Page 53: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Bytecode Instrumentation

Java

53

Page 54: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Bytecode Manipulation

> Java—Javassist

– high-level API—ASM

– working on low-level

54

Page 55: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Javassist

class Point { int x, y; void move(int dx, int dy) { x += dx; y += dy; }}

55

Page 56: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Javassist

ClassPool pool = ClassPool.getDefault();CtClass cc = pool.get("Point");CtMethod m = cc.getDeclaredMethod("move");m.insertBefore("{ System.out.println($1); System.out.println($2); }");cc.writeFile();

56

Page 57: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Javassist

class Point { int x, y; void move(int dx, int dy) { { System.out.println(dx); System.out.println(dy); } x += dx; y += dy; }}

57

Page 58: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Javassist - Edit Body

CtMethod cm = ... ;cm.instrument( new ExprEditor() { public void edit(MethodCall m) throws CannotCompileException { if (m.getClassName().equals("Point") && m.getMethodName().equals("move")) m.replace("{ $1 = 0; $_ = $proceed($$); }"); } });

58

Page 59: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

59

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 60: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Reverse Engineering

Revers

e Eng

ineeri

ngstatic view

+

execution traces dynamic view

+ Dynamic Analysis

60

Page 61: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Dynamic Analysis for Program Comprehension

> Frequency Analysis [Ball, Zaidman]

> Runtime Coupling Metrics based on Web mining techniques to detect key classes in a trace [Zaidman 2005]

> Recovering high-level views from runtime data [Richner and Ducasse 1999]

61

Page 62: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Visualization of Runtime Behaviour

[JinSight, De Pauw 1993] 62

Page 63: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Dividing a trace into features

Feature 1 Feature 2 Feature n

63

Page 64: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Feature Identification is a technique to map features to source code.

“A feature is an observable unit of behaviour of a system triggered by the user” [Eisenbarth etal. 2003]

64

Page 65: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Feature-Centric Analysis:3 Complementary Perspectives

F1

F3

F2

F4

F5

Features Perspective

Features RelationshipsPerspective

Classes Perspective

65

Page 66: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Object Flow Analysis

Method execution traces do not reveal how… objects refer to each other… object references evolve

Trace and analyse object flow—Detect object dependencies

between features

66

Page 67: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

67

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 68: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

> Augment the notion of developers with run-time information

> Traditional IDEs lack information about sometimes purely dynamic relationship between source code artefacts

> The lack of the dynamic type of the receiver is one of the biggest obstacles in program comprehension

Page 69: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Hermion

> integrates dynamic information directly in the source code

> augments the static source code with type information for variables

> shows which methods get invoked at particular call sites in source code

> aggregates its dynamic information over different runs

http://scg.unibe.ch/archive/reports/Roet08dHermion.pdf

Page 70: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Hermion

Page 71: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Senseo

http://scg.unibe.ch/archive/papers/Roet09cSenseo.pdf

Page 72: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Roadmap

72

> Motivation> Sources of Runtime Information> Dynamic Analysis Techniques> Dynamic analysis in a Reverse Engineering Context> The Purpose of Dynamic Analysis> Conclusion

Page 73: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Dynamic vs. Static Analysis

Static analyses extract properties that hold for all possible program runs

Dynamic analysis provides more precise information …but only for the execution under consideration

Dynamic analysis cannot show that a program satisfies a particular property, but can detect violations of the property

73

Page 74: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

Conclusions: Pros and Cons

Dependent on input —Advantage: Input or features can be directly related to execution —Disadvantage: May fail to exercise certain important paths and poor choice of input may be unrepresentative

Broad scope: dynamic analyses follow long paths and may discover semantic dependencies between program entities widely separated in space and time

However, understanding dynamic behaviour of OO systems is difficult

Large number of executed methods Execution paths crosscut abstraction layers

74

Page 75: Software Design and Evolution 10. Dynamic Analysisscg.unibe.ch/download/lectures/sde/SDE-10DynamicAnalysis.pdfNevena Milojković Software Design and Evolution 10. Dynamic Analysis

http://creativecommons.org/licenses/by-sa/3.0/

Attribution-ShareAlike 3.0You are free:

▪ to copy, distribute, display, and perform the work▪ to make derivative works▪ to make commercial use of the work

Under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor.

Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

▪ For any reuse or distribution, you must make clear to others the license terms of this work.▪ Any of these conditions can be waived if you get permission from the copyright holder.

Your fair use and other rights are in no way affected by the above.