15bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Copyright (C) 2016 The Android Open Source Project
25bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
35bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Licensed under the Apache License, Version 2.0 (the "License");
45bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# you may not use this file except in compliance with the License.
55bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# You may obtain a copy of the License at
65bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
75bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#      http://www.apache.org/licenses/LICENSE-2.0
85bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
95bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Unless required by applicable law or agreed to in writing, software
105bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# distributed under the License is distributed on an "AS IS" BASIS,
115bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# See the License for the specific language governing permissions and
135bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# limitations under the License.
145bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond
15dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo'''Module that contains the test TestCoordinates.'''
16dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
17a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummondfrom __future__ import absolute_import
18a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond
19dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leofrom harness.test_base_remote import TestBaseRemote
20a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummondfrom harness.decorators import (
21a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    ordered_test,
22a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    cpp_only_test
23a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond)
24dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
25dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
26dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoclass TestCoordinates(TestBaseRemote):
27dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    '''Tests the inspection of coordinates.
28dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
29dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    Tests the inspection of the range and dimension of coordinates as well
30dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    as the current coordinates.'''
31dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
32a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    bundle_target = {
33a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        'java': 'JavaDebugWaitAttach',
34a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        'jni': 'JNIDebugWaitAttach',
35a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        'cpp': 'CppDebugWaitAttach'
36a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    }
37dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
38a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    def setup(self, android):
39dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''This test requires to be run on one thread.
40dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
41dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Args:
42dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            android: The android_util module.
43dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''
44dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        android.push_prop('debug.rs.max-threads', 1)
45dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
46a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    def teardown(self, android):
47dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''Reset the number of RS threads to the previous value.
48dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
49dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Args:
50dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            android: The android_util module.
51dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''
52dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        android.pop_prop('debug.rs.max-threads')
53dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
54a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    @ordered_test(0)
55a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    def test_inspect_coordinates(self):
56dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # pylint: disable=line-too-long
57dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('language renderscript status',
58dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['Runtime Library discovered',
59dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'Runtime Driver discovered'])
60dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
61dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('language renderscript kernel breakpoint set simple_kernel',
62dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['Breakpoint(s) created',
63dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          '(pending)'])
64dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
65dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # Check the initial conditions.
66dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self._lldb_continue()
67a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self._inspect_coordinates(0, 0, 0)
68dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
69dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # Check two more steps.
70dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self._lldb_continue()
71a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self._inspect_coordinates(1, 0, 0)
72dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self._lldb_continue()
73a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self._inspect_coordinates(2, 0, 0)
74dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
75dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # After eight more steps we should have advanced one step in the y dimension.
76dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        for _ in range(8):
77dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            self._lldb_continue()
78a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self._inspect_coordinates(2, 1, 0)
79a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond
80a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    @ordered_test('last')
81a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    @cpp_only_test()
82a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    def test_cpp_cleanup(self):
83a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self.try_command('breakpoint delete 1', ['1 breakpoints deleted'])
84a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond
85a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        self.try_command('process continue',
86a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond                         ['exited with status = 0'])
87dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
88dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    def _lldb_continue(self):
89dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''Try 'continue' lldb command. Expect to hit a breakpoint.'''
90dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('process continue',
91dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['resuming',
92dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'stopped',
93dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'stop reason = breakpoint'])
94dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
95a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    def _inspect_coordinates(self, x_coord, y_coord, z_coord):
96dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''Run lldb commands to inspect kernel size and coordinates
97dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        and match against expected values.
98dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
99dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Args:
100dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            (x_coord, y_coord, z_coord): The expected coordinates (int triple)
101dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
102dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        Raises:
103dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            TestFail: One of the lldb commands did not provide the expected
104dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                      output.
105dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        '''
106dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('language renderscript kernel coordinate',
107dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['Coordinate: (%d, %d, %d)'
108dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          % (x_coord, y_coord, z_coord)])
109dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
110dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('frame select 1',
111dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['librs.simple.so`simple_kernel.expand',
112dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         'at generated.rs:1'])
113dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
114dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # Inspect the invocation length, should be the same every time.
115dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('expr p->dim',
116dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['x = 8',
117dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'y = 8',
118dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'z = 0'])
119dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
120dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # The X coordinate is in the rsIndex variable.
121dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('expr rsIndex',
122dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          ['= ' + str(x_coord)])
123dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
124dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # Inspect the Y and Z coordinates.
125dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        self.try_command('expr p->current',
126dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                         ['x = ' + str(0),
127dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'y = ' + str(y_coord),
128dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo                          'z = ' + str(z_coord)])
129