1# Copyright (C) 2016 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15'''Module that contains the test TestBreakpointCoordinate.'''
16
17from __future__ import absolute_import
18
19from harness.test_base_remote import TestBaseRemote
20from harness.decorators import (
21    wimpy,
22    ordered_test,
23    cpp_only_test,
24)
25from harness.assert_mixins import CoordinateAssertionsMixin
26
27
28class TestBreakpointCoordinate(TestBaseRemote, CoordinateAssertionsMixin):
29    '''Tests breaking on a specific kernel invocation.
30
31    Uses the -c option to specify the coordinate.
32    '''
33
34    bundle_target = {
35        'java': 'Allocations',
36        'jni': 'JNIAllocations',
37        'cpp': 'CppAllocations'
38    }
39
40    def setup(self, android):
41        '''This test requires to be run on one thread.
42
43        Args:
44            android: The android_util module.
45        '''
46        android.push_prop('debug.rs.max-threads', 1)
47
48    def teardown(self, android):
49        '''Reset the number of RS threads to the previous value.
50
51        Args:
52            android: The android_util module.
53        '''
54        android.pop_prop('debug.rs.max-threads')
55
56    @wimpy
57    @ordered_test(0)
58    def test_breakpoint_coordinate_2d_swizzle_kernel(self):
59        # pylint: disable=line-too-long
60
61        # test conditional coordinate in two dimensions
62        # breakpoint 1
63        self.assert_coord_bp_set('swizzle_kernel', 3, 7)
64
65        # we will delete this breakpoint before we hit it.
66        # breakpoint 2
67        self.assert_coord_bp_set('swizzle_kernel', 199, 190)
68
69        self.assert_coord_stop('allocs', 'swizzle_kernel', x=3, y=7)
70
71        # check breakpoints that have been hit are disabled
72        self.try_command(
73            'breakpoint list',
74            [
75                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
76                "2: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1"
77            ]
78        )
79
80        # delete breakpoint on 199,199,0
81        self.try_command('breakpoint delete 2', ['1 breakpoints deleted'])
82
83        # check breakpoints that have been hit are disabled
84        self.try_command(
85            'breakpoint list',
86            ["1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled"]
87        )
88
89        # test conditional coordinate in a single dimension
90        # breakpoint 3
91        self.assert_coord_bp_set('square_kernel', 8)
92
93        # check breakpoints that have been hit are disabled
94        self.try_command(
95            'breakpoint list',
96            [
97                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
98                "3: RenderScript kernel breakpoint for 'square_kernel', locations = 1"
99            ]
100        )
101
102        self.assert_coord_stop('allocs', 'square_kernel', x=8)
103
104        # check breakpoints that have been hit are disabled
105        self.try_command(
106            'breakpoint list',
107            [
108                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
109                "3: RenderScript kernel breakpoint for 'square_kernel', locations = 1 Options: disabled"
110            ]
111        )
112
113    @wimpy
114    @ordered_test(1)
115    def test_breakpoint_coordinate_3d_add_half_kernel(self):
116        # test conditional coordinate in three dimensions
117        # breakpoint 4
118        self.assert_coord_bp_set('add_half_kernel', 0, 0, 1)
119        # test we can set more than one conditional kernel breakpoint
120        # and both will be hit;
121        # breakpoint 5
122        self.assert_coord_bp_set('add_half_kernel', 0, 1, 2)
123
124        # Now assert that the next two continue/stop cycles hit our conditionals
125        self.assert_coord_stop('allocs', 'add_half_kernel', x=0, y=0, z=1)
126        self.assert_coord_stop('allocs', 'add_half_kernel', x=0, y=1, z=2)
127
128        # check we can see the coordinate from a function invoked by the kernel
129        # breakpoint 6
130        self.try_command(
131            'break set -n half_helper',
132            ['librs.allocs.so`half_helper']
133        )
134
135        # continue till we hit breakpoint 6
136        self.assert_coord_stop('allocs', 'half_helper', x=0, y=1, z=2)
137
138        self.try_command(
139            'breakpoint list',
140            [
141                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
142                "3: RenderScript kernel breakpoint for 'square_kernel', locations = 1 Options: disabled",
143                "4: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled",
144                "5: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled",
145                "6: name = 'half_helper', locations = 1, resolved = 1, hit count = 1"
146            ]
147        )
148
149        self.try_command('breakpoint delete 3', ['1 breakpoints deleted'])
150
151        self.try_command(
152            'breakpoint list',
153            [
154                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
155                "4: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled",
156                "5: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled",
157                "6: name = 'half_helper', locations = 1, resolved = 1, hit count = 1"
158            ]
159        )
160
161        self.try_command('breakpoint delete 6', ['1 breakpoints deleted'])
162
163        self.try_command(
164            'breakpoint list',
165            [
166                "1: RenderScript kernel breakpoint for 'swizzle_kernel', locations = 1 Options: disabled",
167                "4: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled",
168                "5: RenderScript kernel breakpoint for 'add_half_kernel', locations = 1 Options: disabled"
169            ]
170        )
171
172    @cpp_only_test()
173    @ordered_test('last')
174    def test_cpp_cleanup(self):
175        self.try_command('breakpoint delete 4', ['1 breakpoints deleted'])
176        self.try_command('breakpoint delete 5', ['1 breakpoints deleted'])
177        self.try_command('process continue', ['exited with status = 0'])
178