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 class UtilLLDB, which provides lldb utility
16methods.'''
17
18from __future__ import absolute_import
19
20from . import util_constants
21
22try:
23    import lldb
24except ImportError:
25    print('unable to import lldb')
26    print('please run "lldb -P" and add to $PYTHONPATH')
27    quit(util_constants.RC_TEST_FATAL)
28
29
30class UtilLLDB(object):
31    '''Provides utility methods to interface with lldb's python bindings.'''
32
33    @staticmethod
34    def start():
35        '''Initialise the lldb debugger framework.'''
36        lldb.SBDebugger_Initialize()
37
38    @staticmethod
39    def stop():
40        '''Terminate the lldb debugger framework.
41
42        Raises:
43            AssertionError: If an assertion fails.
44        '''
45        assert lldb
46        lldb.SBDebugger_Terminate()
47
48    @staticmethod
49    def create_debugger():
50        '''Create an lldb debugger instance.
51
52        Returns:
53            The SBDebugger instance that was created.
54
55        Raises:
56            AssertionError: If an assertion fails.
57        '''
58        assert lldb
59        inst = lldb.SBDebugger_Create()
60        inst.SetAsync(False)
61        return inst
62
63    @staticmethod
64    def destroy_debugger(dbg):
65        '''Destroy the lldb debugger instance.
66
67        Args:
68            dbg: Instance of SBDebugger that is to be destroyed.
69
70        Raises:
71            AssertionError: If an assertion fails.
72        '''
73        assert lldb
74        lldb.SBDebugger_Destroy(dbg)
75
76    @staticmethod
77    def get_module():
78        '''Get the lldb module.
79
80        Returns:
81            The lldb module.
82
83        Raises:
84            AssertionError: If an assertion fails.
85        '''
86        assert lldb
87        return lldb
88