Debugging Support for Aspect-Oriented Program Using Program Slicing and Call Graph Takashi Ishio, Shinji Kusumoto, Katsuro Inoue Osaka University {t-isio, kusumoto, inoue}@ist.osaka-u.ac.jp Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 1 Overview Aspect-Oriented Programming AOP’s advantage and disadvantages Difficulties in debugging AOP program Proposed Method Program Slicing extended for AOP Loop Detection based on Call Graph (not included in this presentation) Implementation Evaluation Conclusion Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 2 Aspect-Oriented Programming Key Idea: Separation of crosscutting concerns In OOP, programmers cannot encapsulate crosscutting concerns: logging, error handling, transactions, security, ... Code for object interaction is scattered to related classes. It is hard to manage scattered code. In AOP: A crosscutting concern == An aspect When a concern is changed, programmers modify one aspect instead of related classes. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 3 AspectJ, an AOP extension for Java AspectJ: an AOP extension for Java An aspect is defined as a set of advices. Advice: a procedure + a condition when the procedure is executed. A condition = before or after specific events, or instead of the events (around). Around advice uses proceed keyword to execute the original event. Events are specified by Pointcut Designators (PCDs) including: – Method Call and Execution – Field Assignment and Reference – Exception Handling A procedure is written in plain Java with thisJoinPoint object representing the event information. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 4 Simple Example of Aspect aspect LoggingExample { after(): execution(void *.foo(int)) { Logger.logs(thisJoinPoint.getSignature()); } } An advice knows when the advice is executed. Call statements in classes are removed. A.foo(int v) B.foo(int v) C.foo(int v) Logger.logs(“A.foo”); Logging Class Logger.logs(“C.foo”); A.foo(int v) B.foo(int v) when a method is executed, logger.logs(v) is called. Logging Aspect C.foo(int v) Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 5 Advantages of AOP AOP improves: Maintainability Programmers change one aspect instead of multiple classses. Reusability Programmers can reuse classes and aspects independently. – Reuse classes without aspect, or – Reuse aspects for other classes Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 6 Disadvantages of AOP AOP is useful, but ... several drawbacks exist. Fault localization is difficult since: A programmer needs to investigate related classes and aspects to understand the system behavior. When a class is affected by several aspects, the result is hard to predict. e.g. Logging + Transaction ??? A transaction process is logged, or Logging is transactional, or ... ? The result depends on the definition of aspects, or compiler/interpreter implementation Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 7 Our Approach Debugging Support esp. fault localization (investigation) task Extending Program Slicing for AOP Program Slicing is a technique to aid fault localization. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 8 Program Slicing Program Slicing extracts a slice of codes, which affects the value of a specific variable. 1: 2: 3: 4: 5: 6: a = 5; b = a + a; if (b > 0) { c = a; } d = b; a slice based on slice criteria(6, b) 1: 2: 3: 4: 5: 6: a = 5; b = a + a; if (b > 0) { c = a; } d = b; Program Slicing excludes unrelated codes to aid fault localization. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 9 Slice Calculation Process Phase 1: Extraction of dependence relations Data Dependence Data: assignment reference Control: conditional statement controlled block Phase 2: Construction of Program Dependence Graph node: a statement. edge: a dependence relation Phase 3: Traversal of PDG 1: a = 1; 2: c = 4; 3: b = a; a Control Dependence 4: if (a < 1) { 5: b = a; 6: } Program Dependence Graph traversal backward from a node corresponding a slice criterion slice criterion Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 10 DC-Slicing for OOP DC-Slicing: a slicing method combining static and dynamic information. control dependence relations static analysis data dependence relations dynamic analysis Dynamic information is used for data dependence analysis to distinguish object instances method call analysis to solve polymorhpic method calls Size and Cost: Size: Dynamic Slice < DC-slice < Static Slice Cost: Static Slice < DC-slice < Dynamic Slice Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 11 DC-Slicing Extended for AOP Basic Idea: Advice Execution is similar to Method Call An advice is executed when a condition is satisfied. A statement which satisfies a condition calls the advice. AspectJ Developement Tools plug-in indicates an advice execution as a marker. Extending PDG and Call Graph Advice Call Vertex and Advice Execution Edge A vertex is inserted into the place of the statement which calls an advice An edge connected to an advice body Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 12 Control Flow Modified by Aspect A method call statement a.foo(); Advices executed for each method call the part of around advice proceed a path a.foo(); without proceed after advice call after: call(A.foo()) {...} around: call(A.foo()) {...} the rest part of the around advice Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 13 Dynamic Elements in AOP Dynamic Pointcut: if, cflow if(expr): When expr is true, the advice is executed. cflow(PCD): all events reached from PCD cflow( execution(C.foo() ) ) == all events during C.foo() is executing. Converted to an advice call with “may be executed” control dependence relation. We use dynamic information to resolve dynamic pointcut. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 14 Implementation Slicing tool as an Eclipse plug-in Environment: Eclipse 2.1 + AspectJ 1.0.6 Integrated Function: PDG construction – [Compile]-[Rebuild All] constructs PDG – A call graph is also constructed. – A method call loop including advices is recorded as “infinite loop candidates”. Slice Calculation – is started by a button of a tool bar – calculates a slice and indicates a slice on the text editor. Dynamic Analysis is not integerated to IDE. Dynamic analysis code is also inserted using AspectJ. A programmer need to execute a program once. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 15 Screenshot Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 16 Experiment Debugging Experiment We have 12 students debug an AspectJ program. All students have used Java, but not AspectJ. devided into two groups; a group working with a program slice, another without the slice Environment: Eclipse 2.1 + AspectJ Development Tools Procedure: A lecture for using Eclipse Debugging a Java program using Eclipse. (PRE1) A lecture for AspectJ Write an AspectJ program using Eclipse. (PRE2) Debugging a AspectJ program using Eclipse. (DEBUG) Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 17 Debugged Program An AspectJ Program “Eval Expression” Input: an expression represented by a graph. An evaluation = graph traversal Output: (* (+ 2 3) (+ 2 3) ) = 25 5 classes (Graph nodes) and 4 aspects, 340 LOC Loop Detection, Caching, Print, Cleanup The program contains a bug. Print Aspect generates a String representation of a graph. But the generated string is incorrect in several test cases. Gives test cases to all students. Gives a program slice to one group (6 students). The variable contains the output is specified as a slice criterion. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 18 A fragment of program slice public aspect CachingAspect { // member of this Aspect static private Set workers = new HashSet(); // add a member to Worker class private boolean Worker.isAlreadyCalculated = false; pointcut work_call() : call(void Worker.work()); pointcut first_work_call() : work_call() && !cflowbelow(work_call()); void around(): work_call() { Worker w = (Worker)thisJoinPoint.getTarget(); if (w.isAlreadyCalculated) return; else { proceed(); w.isAlreadyCalculated = true; workers.add(w); } } // clear the flag when calculation process is finished after(): first_work_call() { for (Iterator it = workers.iterator(); it.hasNext(); ) { Worker w = (Worker)it.next(); w.isAlreadyCalculated = false; } workers.clear(); } When a node is already visited, return the value of the node. Otherwise, visit the node and set a “visited” flag. When a node is skipped, Print Aspect’s advice is also skipped. Reset flags after evaluation Excluded because flags do not affect the output. } Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 19 Result Effectiveness is evaluated based on working time. Program slicing is used only “DEBUG” task. Students working with a program slice completed DEBUG task faster than students without a slice. However, the effectiveness is not statistically confirmed. Average Working Time (Unit: Minutes) Group PRE1 (Java) 1 (without a slice) 2 (with a slice) 150 200 PRE2 (AspectJ) 186 210 DEBUG (AspectJ) 200 190 Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 20 Discussion From an interview to the students: A program slice is useful to investigate the problem. A slice is a good starting point to read a program. A slice is used to reduce the scope of investigation. “unrelated aspects” is very useful information. A program slice is not useful to fix a problem. A programmer must investigate the influence of the modification to fix a problem. Changes of classes and aspects may affect other aspects. Other methods such as impact analysis should be combined to support bug-fixing task. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 21 Conclusion Debugging Support for AOP Key Idea: Advice Execution == Method Call Loop Detection using Call Graph Application of Program Slicing Program Slicing indicates dependence relations changed by aspects is effective to localize a fault. Future Work Visualization of Inter-aspect relations for large-scale software Combining other technique to fix a fault, e.g. impact analysis Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 22 Any questions/comments ? Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 23 Applicability Our program slicing extension is based on Join Point Model. Dynamic slicing for Java byte-code is also applicable. However, join point shadows are embedded into the byte code. It is hard to untangle byte-code without aspect information. Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 24 Overview of Aspects Relation Two aspects included in the program slice. Print and Cleanup are dominated by Caching included in the Slice join point: ②around call visit a node recursive Caching call ① ③proceed ⑤after before, visit ④after after method body Cleanup LoopDetect Print Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 25 Collected Data Students submitted: What causes a problem How to fix a problem Modified Source Code Time requried to complete the work Thought about Program Slicing Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 26 Experiment 1: Applying tool Apply program slicing to 5 AspectJ design pattern implementation. Evaluate analysis cost Target: 5 Design Pattern implementation in AspectJ Observer, ChainOfResponsibility, ... Average size: about 500 lines of code Test: Execute a sample code, and calculates a slice specify a output variable as a slice criterion. Comparation to AJDT’s marker Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 27 Evaluation Program slicing reduces complexity Aspects added dependence relations. tracking by hands is costly since relations crosscutting many modules (files). Slice can indicates a lost of dependence relations. A statement skipped by an aspect (e.g. around advice) Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 28 Modified Dependence Relation A developer tracks: void f1() { x = f2(); : } int f2() { return doSomething(); } int f3() { return doSomething2(); } aspect redirectMethodCall { int around(): call(f2) { return f3(); An advice replaces a } method call } A calculated slice: void f1() { x = f2(); : } int f2() { return doSomething(); } int f3() { return doSomething2(); } aspect redirectMethodCall { int around(): call(f2) { return f3(); } } Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 29 Analysis Cost Time Static analysis = a traversal to AST generated by compiler. Dynamic analysis: executes a program with dynamic analysis. The performance depends on a target program and a test case. about 2 times longer than normal execution on the average. Memory The analysis cost depends on a number of join points affected by aspects. Analyze 10000 LOC Java Code 20MB + 1000 LOC Aspect Logging All Events 100MB required ! Too many method calls, executions, field set and get are Softwareextracted. Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 30 Dynamic Analysis Aspect We implement dynamic analysis using AspectJ. Dynamic analysis aspect records a position of the assignment statement when a new value is assigned to a field, extracts a dynamic data dependence relation when the field is referred, collects method-call information for each thread (multithreading), collects information when an exception is thrown and which handling clause caught the exception (exceptionhandling). Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 31 Call Graph Example 凡例 Aspect Class call An inifnite loop Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University 32