reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
"""
Test some target commands: create, list, select, variable.
"""

from __future__ import print_function
import os
import stat
import tempfile

import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class targetCommandTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line numbers for our breakpoints.
        self.line_b = line_number('b.c', '// Set break point at this line.')
        self.line_c = line_number('c.c', '// Set break point at this line.')

    def buildB(self):
        db = {'C_SOURCES': 'b.c', 'EXE': self.getBuildArtifact('b.out')}
        self.build(dictionary=db)
        self.addTearDownCleanup(dictionary=db)

    def buildAll(self):
        da = {'C_SOURCES': 'a.c', 'EXE': self.getBuildArtifact('a.out')}
        self.build(dictionary=da)
        self.addTearDownCleanup(dictionary=da)

        self.buildB()

        dc = {'C_SOURCES': 'c.c', 'EXE': self.getBuildArtifact('c.out')}
        self.build(dictionary=dc)
        self.addTearDownCleanup(dictionary=dc)

    def test_target_command(self):
        """Test some target commands: create, list, select."""
        self.buildAll()
        self.do_target_command()

    def test_target_variable_command(self):
        """Test 'target variable' command before and after starting the inferior."""
        d = {'C_SOURCES': 'globals.c', 'EXE': self.getBuildArtifact('globals')}
        self.build(dictionary=d)
        self.addTearDownCleanup(dictionary=d)

        self.do_target_variable_command('globals')

    def test_target_variable_command_no_fail(self):
        """Test 'target variable' command before and after starting the inferior."""
        d = {'C_SOURCES': 'globals.c', 'EXE': self.getBuildArtifact('globals')}
        self.build(dictionary=d)
        self.addTearDownCleanup(dictionary=d)

        self.do_target_variable_command_no_fail('globals')

    def do_target_command(self):
        """Exercise 'target create', 'target list', 'target select' commands."""
        exe_a = self.getBuildArtifact("a.out")
        exe_b = self.getBuildArtifact("b.out")
        exe_c = self.getBuildArtifact("c.out")

        self.runCmd("target list")
        output = self.res.GetOutput()
        if output.startswith("No targets"):
            # We start from index 0.
            base = 0
        else:
            # Find the largest index of the existing list.
            import re
            pattern = re.compile("target #(\d+):")
            for line in reversed(output.split(os.linesep)):
                match = pattern.search(line)
                if match:
                    # We will start from (index + 1) ....
                    base = int(match.group(1), 10) + 1
                    #print("base is:", base)
                    break

        self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
        lldbutil.run_break_set_by_file_and_line(
            self, 'b.c', self.line_b, num_expected_locations=1, loc_exact=True)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
        lldbutil.run_break_set_by_file_and_line(
            self, 'c.c', self.line_c, num_expected_locations=1, loc_exact=True)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target list")

        self.runCmd("target select %d" % base)
        self.runCmd("thread backtrace")

        self.runCmd("target select %d" % (base + 2))
        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
                    substrs=['c.c:%d' % self.line_c,
                             'stop reason = breakpoint'])

        self.runCmd("target select %d" % (base + 1))
        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
                    substrs=['b.c:%d' % self.line_b,
                             'stop reason = breakpoint'])

        self.runCmd("target list")

    def do_target_variable_command(self, exe_name):
        """Exercise 'target variable' command before and after starting the inferior."""
        self.runCmd("file " + self.getBuildArtifact(exe_name),
                    CURRENT_EXECUTABLE_SET)

        self.expect(
            "target variable my_global_char",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                "my_global_char",
                "'X'"])
        self.expect(
            "target variable my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_global_str',
                '"abc"'])
        self.expect(
            "target variable my_static_int",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_static_int',
                '228'])
        self.expect("target variable my_global_str_ptr", matching=False,
                    substrs=['"abc"'])
        self.expect("target variable *my_global_str_ptr", matching=True,
                    substrs=['"abc"'])
        self.expect(
            "target variable *my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=['a'])

        self.runCmd("b main")
        self.runCmd("run")

        self.expect(
            "target variable my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_global_str',
                '"abc"'])
        self.expect(
            "target variable my_static_int",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_static_int',
                '228'])
        self.expect("target variable my_global_str_ptr", matching=False,
                    substrs=['"abc"'])
        self.expect("target variable *my_global_str_ptr", matching=True,
                    substrs=['"abc"'])
        self.expect(
            "target variable *my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=['a'])
        self.expect(
            "target variable my_global_char",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                "my_global_char",
                "'X'"])

        self.runCmd("c")

        # rdar://problem/9763907
        # 'target variable' command fails if the target program has been run
        self.expect(
            "target variable my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_global_str',
                '"abc"'])
        self.expect(
            "target variable my_static_int",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_static_int',
                '228'])
        self.expect("target variable my_global_str_ptr", matching=False,
                    substrs=['"abc"'])
        self.expect("target variable *my_global_str_ptr", matching=True,
                    substrs=['"abc"'])
        self.expect(
            "target variable *my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=['a'])
        self.expect(
            "target variable my_global_char",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                "my_global_char",
                "'X'"])

    def do_target_variable_command_no_fail(self, exe_name):
        """Exercise 'target variable' command before and after starting the inferior."""
        self.runCmd("file " + self.getBuildArtifact(exe_name),
                    CURRENT_EXECUTABLE_SET)

        self.expect(
            "target variable my_global_char",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                "my_global_char",
                "'X'"])
        self.expect(
            "target variable my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_global_str',
                '"abc"'])
        self.expect(
            "target variable my_static_int",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_static_int',
                '228'])
        self.expect("target variable my_global_str_ptr", matching=False,
                    substrs=['"abc"'])
        self.expect("target variable *my_global_str_ptr", matching=True,
                    substrs=['"abc"'])
        self.expect(
            "target variable *my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=['a'])

        self.runCmd("b main")
        self.runCmd("run")

        # New feature: you don't need to specify the variable(s) to 'target vaiable'.
        # It will find all the global and static variables in the current
        # compile unit.
        self.expect("target variable",
                    substrs=['my_global_char',
                             'my_global_str',
                             'my_global_str_ptr',
                             'my_static_int'])

        self.expect(
            "target variable my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_global_str',
                '"abc"'])
        self.expect(
            "target variable my_static_int",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                'my_static_int',
                '228'])
        self.expect("target variable my_global_str_ptr", matching=False,
                    substrs=['"abc"'])
        self.expect("target variable *my_global_str_ptr", matching=True,
                    substrs=['"abc"'])
        self.expect(
            "target variable *my_global_str",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=['a'])
        self.expect(
            "target variable my_global_char",
            VARIABLES_DISPLAYED_CORRECTLY,
            substrs=[
                "my_global_char",
                "'X'"])

    @no_debug_info_test
    def test_target_stop_hook_disable_enable(self):
        self.buildB()
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)

        self.expect("target stop-hook disable 1", error=True, substrs=['unknown stop hook id: "1"'])
        self.expect("target stop-hook disable blub", error=True, substrs=['invalid stop hook id: "blub"'])
        self.expect("target stop-hook enable 1", error=True, substrs=['unknown stop hook id: "1"'])
        self.expect("target stop-hook enable blub", error=True, substrs=['invalid stop hook id: "blub"'])

    @no_debug_info_test
    def test_target_stop_hook_delete(self):
        self.buildB()
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)

        self.expect("target stop-hook delete 1", error=True, substrs=['unknown stop hook id: "1"'])
        self.expect("target stop-hook delete blub", error=True, substrs=['invalid stop hook id: "blub"'])

    @no_debug_info_test
    def test_target_list_args(self):
        self.expect("target list blub", error=True,
                    substrs=["the 'target list' command takes no arguments"])

    @no_debug_info_test
    def test_target_select_no_index(self):
        self.expect("target select", error=True,
                    substrs=["'target select' takes a single argument: a target index"])

    @no_debug_info_test
    def test_target_select_invalid_index(self):
        self.runCmd("target delete --all")
        self.expect("target select 0", error=True,
                    substrs=["index 0 is out of range since there are no active targets"])
        self.buildB()
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.expect("target select 1", error=True,
                    substrs=["index 1 is out of range, valid target indexes are 0 - 0"])


    @no_debug_info_test
    def test_target_create_multiple_args(self):
        self.expect("target create a b", error=True,
                    substrs=["'target create' takes exactly one executable path"])

    @no_debug_info_test
    def test_target_create_nonexistent_core_file(self):
        self.expect("target create -c doesntexist", error=True,
                    substrs=["core file 'doesntexist' doesn't exist"])

    # Write only files don't seem to be supported on Windows.
    @skipIfWindows
    @no_debug_info_test
    def test_target_create_unreadable_core_file(self):
        tf = tempfile.NamedTemporaryFile()
        os.chmod(tf.name, stat.S_IWRITE)
        self.expect("target create -c '" + tf.name + "'", error=True,
                    substrs=["core file '", "' is not readable"])

    @no_debug_info_test
    def test_target_create_nonexistent_sym_file(self):
        self.expect("target create -s doesntexist doesntexisteither", error=True,
                    substrs=["invalid symbol file path 'doesntexist'"])

    @skipIfWindows
    @no_debug_info_test
    def test_target_create_invalid_core_file(self):
        invalid_core_path = os.path.join(self.getSourceDir(), "invalid_core_file")
        self.expect("target create -c '" + invalid_core_path + "'", error=True,
                    substrs=["Unable to find process plug-in for core file '"])


    # Write only files don't seem to be supported on Windows.
    @skipIfWindows
    @no_debug_info_test
    def test_target_create_unreadable_sym_file(self):
        tf = tempfile.NamedTemporaryFile()
        os.chmod(tf.name, stat.S_IWRITE)
        self.expect("target create -s '" + tf.name + "' no_exe", error=True,
                    substrs=["symbol file '", "' is not readable"])

    @no_debug_info_test
    def test_target_delete_all(self):
        self.buildAll()
        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.expect("target delete --all")
        self.expect("target list", substrs=["No targets."])

    @no_debug_info_test
    def test_target_delete_by_index(self):
        self.buildAll()
        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)
        self.expect("target delete 3", error=True,
                    substrs=["target index 3 is out of range, valid target indexes are 0 - 2"])

        self.runCmd("target delete 1")
        self.expect("target list", matching=False, substrs=["b.out"])
        self.runCmd("target delete 1")
        self.expect("target list", matching=False, substrs=["c.out"])

        self.expect("target delete 1", error=True,
                    substrs=["target index 1 is out of range, the only valid index is 0"])

        self.runCmd("target delete 0")
        self.expect("target list", matching=False, substrs=["a.out"])

        self.expect("target delete 0", error=True, substrs=["no targets to delete"])
        self.expect("target delete 1", error=True, substrs=["no targets to delete"])

    @no_debug_info_test
    def test_target_delete_by_index_multiple(self):
        self.buildAll()
        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)

        self.expect("target delete 0 1 2 3", error=True,
                    substrs=["target index 3 is out of range, valid target indexes are 0 - 2"])
        self.expect("target list", substrs=["a.out", "b.out", "c.out"])

        self.runCmd("target delete 0 1 2")
        self.expect("target list", matching=False, substrs=["a.out", "c.out"])

    @no_debug_info_test
    def test_target_delete_selected(self):
        self.buildAll()
        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("file " + self.getBuildArtifact("c.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("target select 1")
        self.runCmd("target delete")
        self.expect("target list", matching=False, substrs=["b.out"])
        self.runCmd("target delete")
        self.runCmd("target delete")
        self.expect("target list", substrs=["No targets."])
        self.expect("target delete", error=True, substrs=["no target is currently selected"])

    @no_debug_info_test
    def test_target_modules_search_paths_clear(self):
        self.buildB()
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("target modules search-paths add foo bar")
        self.runCmd("target modules search-paths add foz baz")
        self.runCmd("target modules search-paths clear")
        self.expect("target list", matching=False, substrs=["bar", "baz"])

    @no_debug_info_test
    def test_target_modules_search_paths_query(self):
        self.buildB()
        self.runCmd("file " + self.getBuildArtifact("b.out"), CURRENT_EXECUTABLE_SET)
        self.runCmd("target modules search-paths add foo bar")
        self.expect("target modules search-paths query foo", substrs=["bar"])
        # Query something that doesn't exist.
        self.expect("target modules search-paths query faz", substrs=["faz"])

        # Invalid arguments.
        self.expect("target modules search-paths query faz baz", error=True,
                    substrs=["query requires one argument"])