Download presentation
Presentation is loading. Please wait.
1
SwE 455 Program Slicing
2
Our Goals Debug your thousands lines of code easily by reducing the complexity of the program Write a robust program before testing your code Save your regression testing time by limiting the tests to only those that exercise the changed code
3
“Break your code into smaller pieces”
Approach “Break your code into smaller pieces”
4
Program Slicing What is it?
- A well-known program analysis and transformation technique that uses program statement dependence information to identify parts of a program that influence or are influenced by an initial set of program points of interest which is called the slice criteria - Introduced by Mark Weiser in his Ph.D. thesis (1979) What it essentially means is we first pick a statement to be our slice criteria. Next, we generally are interested in two kinds of situations: All other statements in the program that are affecting the criteria. (Help us to find a bug, criteria is the statement that produce incorrect output) All other statements that are affected by the criteria. (If now the criteria is the fixed statements that used to be buggy)
5
Program Slicing (cont’d)
indirectly relevant Resulting slice Slice Slicing Criterion Source program slicing criterion is provided by the user
6
Variants of Program Slicing
Many different variants of program slicing exist Static Slicing Backward Slicing Forward Slicing Dynamic Slicing Conditional Slicing Chopping Also Many Different tools, however Most program slicing tools are written for C but there are also some for C++ and Java Most of these have problems with dynamic binding, inheritance, polymorphism and performance
7
Example of backward slicing Static
Slice criterion <12,i> 1 main( ) 2 { 3 int i, sum; 4 sum = 0; 5 i = 1; 6 while(i <= 10) { 8 Sum = sum + 1; 9 ++ i; 10 } 11 Cout<< sum; 12 Cout<< i; 13 }
8
Backward Slice Example
public class SimpleExample { static int add(int a, int b){ return(a+b); } public static void main(final String[] arg){ int i = 1; int sum = 0; while (i < 11) { sum = add(sum, i); i = add(i, 1); System.out.println("sum = " + sum); System.out.println("i = " + i); A backward slice consists of all statements that affect a given point in the program. All other statements that influence the output of variable I. Slicing Criterion
9
Example of forward static slicing
Slice criterion <3,sum> 1 main( ) 2 { 3 int i, sum; 4 sum = 0; 5 i = 1; 6 while(i <= 10) 7 { 8 sum = sum + 1; i; 10 } 11 Cout<< sum; 12 Cout<< i; 13}
10
Forward Slice Example public class SimpleExample {
static int add(int a, int b){ return(a+b); } public static void main(final String[] arg){ int i = 1; int sum = 0; while (i < 11) { sum = add(sum, i); i = add(i, 1); System.out.println("sum = " + sum); System.out.println("i = " + i); Slicing Criterion A forward slice consists of all statements that are affected by a given point in the program. In this example, the forward slice indicates exactly which statements are influenced by the initialization of variable sum
11
What is program slicing?
Program slice must satisfy the following conditions Slice S(V,n) must be derived from P by deleting statements from P Slice S(V,n) must be syntactically correct For all executions of P, the value of V in the execution of S(V,n) just before the location n must be the same value of V in the execution of the program P just before location n
12
Example of program slicing
Original program: 1 begin 2 read(x,y) 3 total := 0.0 4 sum := 0.0 5 if x <= 1 then sum := y else begin read(z) total := x*y end 11 write(total, sum) 12 end. Slice criterion: <9, x> begin read(x,y) end. Slice criterion: <12, z> begin read(x,y) if x <= 1 then else read(z) end. Slice criterion: <12, total> begin read(x,y) total := 0 if x <= 1 then else total := x*y end.
13
Variants of program slicing Static slices
Slice criterion <p, V> Where p is a program point and V is a subset of program variables Program slice on the slicing criterion <p, V> is a subset of program statements that preserves the behavior of the original program at the program point p with respect to the program variables in V
14
Variants of program slicing Static slices
Slices derived from the source code for all possible input values No assumptions about input values May lead to relatively big slices Contains all statements that may affect a variable for every possible execution Current static methods can only compute approximations
15
Static slices example Slice criterion (12,i) 1 main( ) 2 {
3 int i, sum; 4 sum = 0; 5 i = 1; 6 while(i <= 10) { 8 sum = sum + 1; 9 ++ i; 10 } 11 Cout<< sum; 12 Cout<< i; 13 }
16
Example of dynamic slices
read (n) for I := 1 to n do a := 2 if c1==1 then if c2==1 then a := 4 else a := 6 z := a write (z) Assumptions Input n is 1 C1, c2 both true Execution history is 11, 21, 31, 41, 51, 61, 91, 22, 101 Slice criterion<1, 101, z>
17
Applications of program slices
Program debugging Was introduced by Mark Weiser as debugging aid Slicing visualizes control and data dependencies It highlights statements influencing the slice Testing: reduce cost of regression testing after modifications (only run those tests that needed) Integration : merging two programs A and B that both resulted from modifications to BASE
18
Applications of program slices
Program understanding Reverse engineering: comprehending the design by abstracting out of the source code the design decisions Software maintenance: changing source code without unwanted side effects Software quality assurance: validate interactions between safety-critical components
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.