JDevlieghere created this revision.
JDevlieghere added a reviewer: jingham.
Herald added subscribers: teemperor, abidh.
JDevlieghere requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

- General cleanup in TargetList::CreateTargetInternal.
- Add the missing `--platform` option to target create so you can specify a 
different platform than the currently selected one.
- Fix the bogus error message that suggests passing a platform with 
`--platform` when no platforms support the given binary.
- Fix the bogus error message that tells the user to specify the platform with 
--platform when multiple platforms support the given binary.
- Improve test coverage.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D84809

Files:
  lldb/source/Commands/CommandObjectTarget.cpp
  lldb/source/Target/TargetList.cpp
  lldb/test/API/commands/target/basic/TestTargetCommand.py
  lldb/test/API/commands/target/basic/bogus.yaml

Index: lldb/test/API/commands/target/basic/bogus.yaml
===================================================================
--- /dev/null
+++ lldb/test/API/commands/target/basic/bogus.yaml
@@ -0,0 +1,962 @@
+--- !fat-mach-o
+FatHeader:
+  magic:           0xCAFEBABE
+  nfat_arch:       3
+FatArchs:
+  - cputype:         0x0000000C
+    cpusubtype:      0x00000009
+    offset:          0x0000000000004000
+    size:            1400
+    align:           14
+  - cputype:         0x0000000C
+    cpusubtype:      0x0000000B
+    offset:          0x0000000000008000
+    size:            1404
+    align:           14
+  - cputype:         0x0100000C
+    cpusubtype:      0x00000000
+    offset:          0x000000000000C000
+    size:            1584
+    align:           14
+Slices:
+  - !mach-o
+    FileHeader:
+      magic:           0xFEEDFACE
+      # Bogus
+      cputype:         0x00000003
+      cpusubtype:      0x00000009
+      filetype:        0x00000001
+      ncmds:           4
+      sizeofcmds:      924
+      flags:           0x00002000
+    LoadCommands:
+      - cmd:             LC_SEGMENT
+        cmdsize:         804
+        segname:         ''
+        vmaddr:          0
+        vmsize:          414
+        fileoff:         952
+        filesize:        414
+        maxprot:         7
+        initprot:        7
+        nsects:          11
+        flags:           0
+        Sections:
+          - sectname:        __text
+            segname:         __TEXT
+            addr:            0x0000000000000000
+            size:            0
+            offset:          0x000003B8
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x80000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_str
+            segname:         __DWARF
+            addr:            0x0000000000000000
+            size:            68
+            offset:          0x000003B8
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         636C616E672076657273696F6E20332E382E3020287472756E6B2032343337373629006661742D746573742E63002F496E707574730061726D76375F76617200696E7400
+          - sectname:        __debug_loc
+            segname:         __DWARF
+            addr:            0x0000000000000044
+            size:            0
+            offset:          0x000003FC
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_abbrev
+            segname:         __DWARF
+            addr:            0x0000000000000044
+            size:            44
+            offset:          0x000003FC
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         011101250E1305030E10061B0E0000023400030E49133F0C3A0B3B0B020A0000032400030E3E0B0B0B000000
+          - sectname:        __debug_info
+            segname:         __DWARF
+            addr:            0x0000000000000070
+            size:            56
+            offset:          0x00000428
+            align:           0
+            reloff:          0x00000558
+            nreloc:          1
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         340000000200000000000401000000000C0023000000000000002E0000000236000000300000000101170503000000000340000000050400
+            relocations:
+              - address:         0x0000002C
+                symbolnum:       0
+                pcrel:           false
+                length:          2
+                extern:          true
+                type:            0
+                scattered:       false
+                value:           0
+          - sectname:        __debug_ranges
+            segname:         __DWARF
+            addr:            0x00000000000000A8
+            size:            0
+            offset:          0x00000460
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __apple_names
+            segname:         __DWARF
+            addr:            0x00000000000000A8
+            size:            60
+            offset:          0x00000460
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000010000000C000000000000000100000001000600000000003A1F7A282C00000036000000010000001E00000000000000
+          - sectname:        __apple_objc
+            segname:         __DWARF
+            addr:            0x00000000000000E4
+            size:            36
+            offset:          0x0000049C
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_namespac
+            segname:         __DWARF
+            addr:            0x0000000000000108
+            size:            36
+            offset:          0x000004C0
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_types
+            segname:         __DWARF
+            addr:            0x000000000000012C
+            size:            71
+            offset:          0x000004E4
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         48534148010000000100000001000000140000000000000003000000010006000300050004000B00000000003080880B3400000040000000010000003000000024000000000000
+          - sectname:        __debug_line
+            segname:         __DWARF
+            addr:            0x0000000000000173
+            size:            43
+            offset:          0x0000052B
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         270000000200210000000101FB0E0D000101010100000001000001006661742D746573742E630000000000
+      - cmd:             LC_VERSION_MIN_IPHONEOS
+        cmdsize:         16
+        version:         327680
+        sdk:             0
+      - cmd:             LC_SYMTAB
+        cmdsize:         24
+        symoff:          1376
+        nsyms:           1
+        stroff:          1388
+        strsize:         12
+      - cmd:             LC_DYSYMTAB
+        cmdsize:         80
+        ilocalsym:       0
+        nlocalsym:       0
+        iextdefsym:      0
+        nextdefsym:      0
+        iundefsym:       0
+        nundefsym:       1
+        tocoff:          0
+        ntoc:            0
+        modtaboff:       0
+        nmodtab:         0
+        extrefsymoff:    0
+        nextrefsyms:     0
+        indirectsymoff:  0
+        nindirectsyms:   0
+        extreloff:       0
+        nextrel:         0
+        locreloff:       0
+        nlocrel:         0
+    LinkEditData:
+      NameList:
+        - n_strx:          1
+          n_type:          0x01
+          n_sect:          0
+          n_desc:          512
+          n_value:         4
+      StringTable:
+        - ''
+        - _armv7_var
+    DWARF:
+      debug_str:
+        - 'clang version 3.8.0 (trunk 243776)'
+        - fat-test.c
+        - '/Inputs'
+        - armv7_var
+        - int
+      debug_abbrev:
+        - Code:            0x0000000000000001
+          Tag:             DW_TAG_compile_unit
+          Children:        DW_CHILDREN_yes
+          Attributes:
+            - Attribute:       DW_AT_producer
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_language
+              Form:            DW_FORM_data2
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_stmt_list
+              Form:            DW_FORM_data4
+            - Attribute:       DW_AT_comp_dir
+              Form:            DW_FORM_strp
+        - Code:            0x0000000000000002
+          Tag:             DW_TAG_variable
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_type
+              Form:            DW_FORM_ref4
+            - Attribute:       DW_AT_external
+              Form:            DW_FORM_flag
+            - Attribute:       DW_AT_decl_file
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_decl_line
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_location
+              Form:            DW_FORM_block1
+        - Code:            0x0000000000000003
+          Tag:             DW_TAG_base_type
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_encoding
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_byte_size
+              Form:            DW_FORM_data1
+      debug_info:
+        - Length:          0x0000000000000034
+          Version:         2
+          AbbrOffset:      0x0000000000000000
+          AddrSize:        4
+          Entries:
+            - AbbrCode:        0x00000001
+              Values:
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000000C
+                - Value:           0x0000000000000023
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000002E
+            - AbbrCode:        0x00000002
+              Values:
+                - Value:           0x0000000000000036
+                - Value:           0x0000000000000030
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000017
+                - Value:           0x0000000000000005
+                  BlockData:       [ 0x03, 0x00, 0x00, 0x00, 0x00 ]
+            - AbbrCode:        0x00000003
+              Values:
+                - Value:           0x0000000000000040
+                - Value:           0x0000000000000005
+                - Value:           0x0000000000000004
+            - AbbrCode:        0x00000000
+              Values:          []
+      debug_line:
+        - Length:          39
+          Version:         2
+          PrologueLength:  33
+          MinInstLength:   1
+          DefaultIsStmt:   1
+          LineBase:        251
+          LineRange:       14
+          OpcodeBase:      13
+          StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
+          IncludeDirs:     []
+          Files:
+            - Name:            fat-test.c
+              DirIdx:          0
+              ModTime:         0
+              Length:          0
+          Opcodes:         []
+  - !mach-o
+    FileHeader:
+      magic:           0xFEEDFACE
+      # Bogus
+      cputype:         0x00000002
+      cpusubtype:      0x0000000B
+      filetype:        0x00000001
+      ncmds:           4
+      sizeofcmds:      924
+      flags:           0x00002000
+    LoadCommands:
+      - cmd:             LC_SEGMENT
+        cmdsize:         804
+        segname:         ''
+        vmaddr:          0
+        vmsize:          415
+        fileoff:         952
+        filesize:        415
+        maxprot:         7
+        initprot:        7
+        nsects:          11
+        flags:           0
+        Sections:
+          - sectname:        __text
+            segname:         __TEXT
+            addr:            0x0000000000000000
+            size:            0
+            offset:          0x000003B8
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x80000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_str
+            segname:         __DWARF
+            addr:            0x0000000000000000
+            size:            69
+            offset:          0x000003B8
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         636C616E672076657273696F6E20332E382E3020287472756E6B2032343337373629006661742D746573742E63002F496E707574730061726D7637735F76617200696E7400
+          - sectname:        __debug_loc
+            segname:         __DWARF
+            addr:            0x0000000000000045
+            size:            0
+            offset:          0x000003FD
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_abbrev
+            segname:         __DWARF
+            addr:            0x0000000000000045
+            size:            44
+            offset:          0x000003FD
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         011101250E1305030E10061B0E0000023400030E49133F0C3A0B3B0B020A0000032400030E3E0B0B0B000000
+          - sectname:        __debug_info
+            segname:         __DWARF
+            addr:            0x0000000000000071
+            size:            56
+            offset:          0x00000429
+            align:           0
+            reloff:          0x00000558
+            nreloc:          1
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         340000000200000000000401000000000C0023000000000000002E0000000236000000300000000101150503000000000341000000050400
+            relocations:
+              - address:         0x0000002C
+                symbolnum:       0
+                pcrel:           false
+                length:          2
+                extern:          true
+                type:            0
+                scattered:       false
+                value:           0
+          - sectname:        __debug_ranges
+            segname:         __DWARF
+            addr:            0x00000000000000A9
+            size:            0
+            offset:          0x00000461
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __apple_names
+            segname:         __DWARF
+            addr:            0x00000000000000A9
+            size:            60
+            offset:          0x00000461
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000010000000C000000000000000100000001000600000000006DAB1B392C00000036000000010000001E00000000000000
+          - sectname:        __apple_objc
+            segname:         __DWARF
+            addr:            0x00000000000000E5
+            size:            36
+            offset:          0x0000049D
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_namespac
+            segname:         __DWARF
+            addr:            0x0000000000000109
+            size:            36
+            offset:          0x000004C1
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_types
+            segname:         __DWARF
+            addr:            0x000000000000012D
+            size:            71
+            offset:          0x000004E5
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         48534148010000000100000001000000140000000000000003000000010006000300050004000B00000000003080880B3400000041000000010000003000000024000000000000
+          - sectname:        __debug_line
+            segname:         __DWARF
+            addr:            0x0000000000000174
+            size:            43
+            offset:          0x0000052C
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         270000000200210000000101FB0E0D000101010100000001000001006661742D746573742E630000000000
+      - cmd:             LC_VERSION_MIN_IPHONEOS
+        cmdsize:         16
+        version:         327680
+        sdk:             0
+      - cmd:             LC_SYMTAB
+        cmdsize:         24
+        symoff:          1376
+        nsyms:           1
+        stroff:          1388
+        strsize:         16
+      - cmd:             LC_DYSYMTAB
+        cmdsize:         80
+        ilocalsym:       0
+        nlocalsym:       0
+        iextdefsym:      0
+        nextdefsym:      0
+        iundefsym:       0
+        nundefsym:       1
+        tocoff:          0
+        ntoc:            0
+        modtaboff:       0
+        nmodtab:         0
+        extrefsymoff:    0
+        nextrefsyms:     0
+        indirectsymoff:  0
+        nindirectsyms:   0
+        extreloff:       0
+        nextrel:         0
+        locreloff:       0
+        nlocrel:         0
+    LinkEditData:
+      NameList:
+        - n_strx:          1
+          n_type:          0x01
+          n_sect:          0
+          n_desc:          512
+          n_value:         4
+      StringTable:
+        - ''
+        - _armv7s_var
+        - ''
+        - ''
+        - ''
+    DWARF:
+      debug_str:
+        - 'clang version 3.8.0 (trunk 243776)'
+        - fat-test.c
+        - '/Inputs'
+        - armv7s_var
+        - int
+      debug_abbrev:
+        - Code:            0x0000000000000001
+          Tag:             DW_TAG_compile_unit
+          Children:        DW_CHILDREN_yes
+          Attributes:
+            - Attribute:       DW_AT_producer
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_language
+              Form:            DW_FORM_data2
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_stmt_list
+              Form:            DW_FORM_data4
+            - Attribute:       DW_AT_comp_dir
+              Form:            DW_FORM_strp
+        - Code:            0x0000000000000002
+          Tag:             DW_TAG_variable
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_type
+              Form:            DW_FORM_ref4
+            - Attribute:       DW_AT_external
+              Form:            DW_FORM_flag
+            - Attribute:       DW_AT_decl_file
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_decl_line
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_location
+              Form:            DW_FORM_block1
+        - Code:            0x0000000000000003
+          Tag:             DW_TAG_base_type
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_encoding
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_byte_size
+              Form:            DW_FORM_data1
+      debug_info:
+        - Length:          0x0000000000000034
+          Version:         2
+          AbbrOffset:      0x0000000000000000
+          AddrSize:        4
+          Entries:
+            - AbbrCode:        0x00000001
+              Values:
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000000C
+                - Value:           0x0000000000000023
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000002E
+            - AbbrCode:        0x00000002
+              Values:
+                - Value:           0x0000000000000036
+                - Value:           0x0000000000000030
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000015
+                - Value:           0x0000000000000005
+                  BlockData:       [ 0x03, 0x00, 0x00, 0x00, 0x00 ]
+            - AbbrCode:        0x00000003
+              Values:
+                - Value:           0x0000000000000041
+                - Value:           0x0000000000000005
+                - Value:           0x0000000000000004
+            - AbbrCode:        0x00000000
+              Values:          []
+      debug_line:
+        - Length:          39
+          Version:         2
+          PrologueLength:  33
+          MinInstLength:   1
+          DefaultIsStmt:   1
+          LineBase:        251
+          LineRange:       14
+          OpcodeBase:      13
+          StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
+          IncludeDirs:     []
+          Files:
+            - Name:            fat-test.c
+              DirIdx:          0
+              ModTime:         0
+              Length:          0
+          Opcodes:         []
+  - !mach-o
+    FileHeader:
+      magic:           0xFEEDFACF
+      # Bogus
+      cputype:         0x00000001
+      cpusubtype:      0x00000000
+      filetype:        0x00000001
+      ncmds:           4
+      sizeofcmds:      1072
+      flags:           0x00002000
+      reserved:        0x00000000
+    LoadCommands:
+      - cmd:             LC_SEGMENT_64
+        cmdsize:         952
+        segname:         ''
+        vmaddr:          0
+        vmsize:          418
+        fileoff:         1104
+        filesize:        418
+        maxprot:         7
+        initprot:        7
+        nsects:          11
+        flags:           0
+        Sections:
+          - sectname:        __text
+            segname:         __TEXT
+            addr:            0x0000000000000000
+            size:            0
+            offset:          0x00000450
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x80000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_str
+            segname:         __DWARF
+            addr:            0x0000000000000000
+            size:            68
+            offset:          0x00000450
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         636C616E672076657273696F6E20332E382E3020287472756E6B2032343337373629006661742D746573742E63002F496E707574730061726D36345F76617200696E7400
+          - sectname:        __debug_loc
+            segname:         __DWARF
+            addr:            0x0000000000000044
+            size:            0
+            offset:          0x00000494
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __debug_abbrev
+            segname:         __DWARF
+            addr:            0x0000000000000044
+            size:            44
+            offset:          0x00000494
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         011101250E1305030E10061B0E0000023400030E49133F0C3A0B3B0B020A0000032400030E3E0B0B0B000000
+          - sectname:        __debug_info
+            segname:         __DWARF
+            addr:            0x0000000000000070
+            size:            60
+            offset:          0x000004C0
+            align:           0
+            reloff:          0x000005F4
+            nreloc:          1
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         380000000200000000000801000000000C0023000000000000002E000000023600000034000000010119090300000000000000000340000000050400
+            relocations:
+              - address:         0x0000002C
+                symbolnum:       1
+                pcrel:           false
+                length:          3
+                extern:          true
+                type:            0
+                scattered:       false
+                value:           0
+          - sectname:        __debug_ranges
+            segname:         __DWARF
+            addr:            0x00000000000000AC
+            size:            0
+            offset:          0x000004FC
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         ''
+          - sectname:        __apple_names
+            segname:         __DWARF
+            addr:            0x00000000000000AC
+            size:            60
+            offset:          0x000004FC
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000010000000C0000000000000001000000010006000000000077ADF9922C00000036000000010000001E00000000000000
+          - sectname:        __apple_objc
+            segname:         __DWARF
+            addr:            0x00000000000000E8
+            size:            36
+            offset:          0x00000538
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_namespac
+            segname:         __DWARF
+            addr:            0x000000000000010C
+            size:            36
+            offset:          0x0000055C
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         485341480100000001000000000000000C000000000000000100000001000600FFFFFFFF
+          - sectname:        __apple_types
+            segname:         __DWARF
+            addr:            0x0000000000000130
+            size:            71
+            offset:          0x00000580
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         48534148010000000100000001000000140000000000000003000000010006000300050004000B00000000003080880B3400000040000000010000003400000024000000000000
+          - sectname:        __debug_line
+            segname:         __DWARF
+            addr:            0x0000000000000177
+            size:            43
+            offset:          0x000005C7
+            align:           0
+            reloff:          0x00000000
+            nreloc:          0
+            flags:           0x02000000
+            reserved1:       0x00000000
+            reserved2:       0x00000000
+            reserved3:       0x00000000
+            content:         270000000200210000000101FB0E0D000101010100000001000001006661742D746573742E630000000000
+      - cmd:             LC_VERSION_MIN_IPHONEOS
+        cmdsize:         16
+        version:         327680
+        sdk:             0
+      - cmd:             LC_SYMTAB
+        cmdsize:         24
+        symoff:          1532
+        nsyms:           2
+        stroff:          1564
+        strsize:         20
+      - cmd:             LC_DYSYMTAB
+        cmdsize:         80
+        ilocalsym:       0
+        nlocalsym:       1
+        iextdefsym:      1
+        nextdefsym:      0
+        iundefsym:       1
+        nundefsym:       1
+        tocoff:          0
+        ntoc:            0
+        modtaboff:       0
+        nmodtab:         0
+        extrefsymoff:    0
+        nextrefsyms:     0
+        indirectsymoff:  0
+        nindirectsyms:   0
+        extreloff:       0
+        nextrel:         0
+        locreloff:       0
+        nlocrel:         0
+    LinkEditData:
+      NameList:
+        - n_strx:          12
+          n_type:          0x0E
+          n_sect:          1
+          n_desc:          0
+          n_value:         0
+        - n_strx:          1
+          n_type:          0x01
+          n_sect:          0
+          n_desc:          512
+          n_value:         4
+      StringTable:
+        - ''
+        - _arm64_var
+        - ltmp0
+        - ''
+        - ''
+    DWARF:
+      debug_str:
+        - 'clang version 3.8.0 (trunk 243776)'
+        - fat-test.c
+        - '/Inputs'
+        - arm64_var
+        - int
+      debug_abbrev:
+        - Code:            0x0000000000000001
+          Tag:             DW_TAG_compile_unit
+          Children:        DW_CHILDREN_yes
+          Attributes:
+            - Attribute:       DW_AT_producer
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_language
+              Form:            DW_FORM_data2
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_stmt_list
+              Form:            DW_FORM_data4
+            - Attribute:       DW_AT_comp_dir
+              Form:            DW_FORM_strp
+        - Code:            0x0000000000000002
+          Tag:             DW_TAG_variable
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_type
+              Form:            DW_FORM_ref4
+            - Attribute:       DW_AT_external
+              Form:            DW_FORM_flag
+            - Attribute:       DW_AT_decl_file
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_decl_line
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_location
+              Form:            DW_FORM_block1
+        - Code:            0x0000000000000003
+          Tag:             DW_TAG_base_type
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_encoding
+              Form:            DW_FORM_data1
+            - Attribute:       DW_AT_byte_size
+              Form:            DW_FORM_data1
+      debug_info:
+        - Length:          0x0000000000000038
+          Version:         2
+          AbbrOffset:      0x0000000000000000
+          AddrSize:        8
+          Entries:
+            - AbbrCode:        0x00000001
+              Values:
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000000C
+                - Value:           0x0000000000000023
+                - Value:           0x0000000000000000
+                - Value:           0x000000000000002E
+            - AbbrCode:        0x00000002
+              Values:
+                - Value:           0x0000000000000036
+                - Value:           0x0000000000000034
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000001
+                - Value:           0x0000000000000019
+                - Value:           0x0000000000000009
+                  BlockData:       [ 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                     0x00, 0x00, 0x00 ]
+            - AbbrCode:        0x00000003
+              Values:
+                - Value:           0x0000000000000040
+                - Value:           0x0000000000000005
+                - Value:           0x0000000000000004
+            - AbbrCode:        0x00000000
+              Values:          []
+      debug_line:
+        - Length:          39
+          Version:         2
+          PrologueLength:  33
+          MinInstLength:   1
+          DefaultIsStmt:   1
+          LineBase:        251
+          LineRange:       14
+          OpcodeBase:      13
+          StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
+          IncludeDirs:     []
+          Files:
+            - Name:            fat-test.c
+              DirIdx:          0
+              ModTime:         0
+              Length:          0
+          Opcodes:         []
+...
Index: lldb/test/API/commands/target/basic/TestTargetCommand.py
===================================================================
--- lldb/test/API/commands/target/basic/TestTargetCommand.py
+++ lldb/test/API/commands/target/basic/TestTargetCommand.py
@@ -115,6 +115,33 @@
 
         self.runCmd("target list")
 
+    @no_debug_info_test
+    def test_target_create_invalid_arch(self):
+        exe = self.getBuildArtifact("a.out")
+        self.expect("target create {} --arch doesntexist".format(exe), error=True,
+                    patterns=["error: invalid triple 'doesntexist'"])
+
+    @no_debug_info_test
+    def test_target_create_platform(self):
+        self.buildB()
+        exe = self.getBuildArtifact("b.out")
+        self.expect("target create {} --platform host".format(exe))
+
+    @no_debug_info_test
+    def test_target_create_unsupported_platform(self):
+        yaml = os.path.join(self.getSourceDir(), "bogus.yaml")
+        exe = self.getBuildArtifact("bogus")
+        self.yaml2obj(yaml, exe)
+        self.expect("target create {}".format(exe), error=True,
+                    patterns=['error: no matching platforms found for this file'])
+
+    @no_debug_info_test
+    def test_target_create_invalid_platform(self):
+        self.buildB()
+        exe = self.getBuildArtifact("b.out")
+        self.expect("target create {} --platform doesntexist".format(exe), error=True,
+                    patterns=['error: unable to find a plug-in for the platform named "doesntexist"'])
+
     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),
Index: lldb/source/Target/TargetList.cpp
===================================================================
--- lldb/source/Target/TargetList.cpp
+++ lldb/source/Target/TargetList.cpp
@@ -75,55 +75,49 @@
     const OptionGroupPlatform *platform_options, TargetSP &target_sp,
     bool is_dummy_target) {
   Status error;
-  PlatformSP platform_sp;
 
-  // This is purposely left empty unless it is specified by triple_cstr. If not
-  // initialized via triple_cstr, then the currently selected platform will set
-  // the architecture correctly.
+  // Let's start by looking at the selected platform.
+  PlatformSP platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
+
+  // This variable corresponds to the architecture specified by the triple
+  // string. If that string was empty the currently selected platform will
+  // determine the architecture.
   const ArchSpec arch(triple_str);
-  if (!triple_str.empty()) {
-    if (!arch.IsValid()) {
-      error.SetErrorStringWithFormat("invalid triple '%s'",
-                                     triple_str.str().c_str());
-      return error;
-    }
+  if (!triple_str.empty() && !arch.IsValid()) {
+    error.SetErrorStringWithFormat("invalid triple '%s'",
+                                   triple_str.str().c_str());
+    return error;
   }
 
   ArchSpec platform_arch(arch);
 
-  bool prefer_platform_arch = false;
-
-  CommandInterpreter &interpreter = debugger.GetCommandInterpreter();
-
-  // let's see if there is already an existing platform before we go creating
-  // another...
-  platform_sp = debugger.GetPlatformList().GetSelectedPlatform();
-
-  if (platform_options && platform_options->PlatformWasSpecified()) {
-    // Create a new platform if it doesn't match the selected platform
-    if (!platform_options->PlatformMatches(platform_sp)) {
-      const bool select_platform = true;
-      platform_sp = platform_options->CreatePlatformWithOptions(
-          interpreter, arch, select_platform, error, platform_arch);
-      if (!platform_sp)
-        return error;
-    }
+  // Create a new platform if a platform was specified in the platform options
+  // and doesn't match the selected platform.
+  if (platform_options && platform_options->PlatformWasSpecified() &&
+      !platform_options->PlatformMatches(platform_sp)) {
+    const bool select_platform = true;
+    platform_sp = platform_options->CreatePlatformWithOptions(
+        debugger.GetCommandInterpreter(), arch, select_platform, error,
+        platform_arch);
+    if (!platform_sp)
+      return error;
   }
 
+  bool prefer_platform_arch = false;
+
   if (!user_exe_path.empty()) {
-    ModuleSpecList module_specs;
-    ModuleSpec module_spec;
-    module_spec.GetFileSpec().SetFile(user_exe_path, FileSpec::Style::native);
+    ModuleSpec module_spec(FileSpec(user_exe_path, FileSpec::Style::native));
     FileSystem::Instance().Resolve(module_spec.GetFileSpec());
-
     // Resolve the executable in case we are given a path to a application
-    // bundle like a .app bundle on MacOSX
+    // bundle like a .app bundle on MacOSX.
     Host::ResolveExecutableInBundle(module_spec.GetFileSpec());
 
     lldb::offset_t file_offset = 0;
     lldb::offset_t file_size = 0;
+    ModuleSpecList module_specs;
     const size_t num_specs = ObjectFile::GetModuleSpecifications(
         module_spec.GetFileSpec(), file_offset, file_size, module_specs);
+
     if (num_specs > 0) {
       ModuleSpec matching_module_spec;
 
@@ -134,7 +128,7 @@
                     matching_module_spec.GetArchitecture())) {
               // If the OS or vendor weren't specified, then adopt the module's
               // architecture so that the platform matching can be more
-              // accurate
+              // accurate.
               if (!platform_arch.TripleOSWasSpecified() ||
                   !platform_arch.TripleVendorWasSpecified()) {
                 prefer_platform_arch = true;
@@ -160,108 +154,104 @@
             platform_arch = matching_module_spec.GetArchitecture();
           }
         }
+      } else if (arch.IsValid()) {
+        // A (valid) architecture was specified.
+        module_spec.GetArchitecture() = arch;
+        if (module_specs.FindMatchingModuleSpec(module_spec,
+                                                matching_module_spec)) {
+          prefer_platform_arch = true;
+          platform_arch = matching_module_spec.GetArchitecture();
+        }
       } else {
-        if (arch.IsValid()) {
-          module_spec.GetArchitecture() = arch;
-          if (module_specs.FindMatchingModuleSpec(module_spec,
-                                                  matching_module_spec)) {
-            prefer_platform_arch = true;
-            platform_arch = matching_module_spec.GetArchitecture();
-          }
-        } else {
-          // No architecture specified, check if there is only one platform for
-          // all of the architectures.
-
-          typedef std::vector<PlatformSP> PlatformList;
-          PlatformList platforms;
-          PlatformSP host_platform_sp = Platform::GetHostPlatform();
-          for (size_t i = 0; i < num_specs; ++i) {
-            ModuleSpec module_spec;
-            if (module_specs.GetModuleSpecAtIndex(i, module_spec)) {
-              // See if there was a selected platform and check that first
-              // since the user may have specified it.
-              if (platform_sp) {
-                if (platform_sp->IsCompatibleArchitecture(
-                        module_spec.GetArchitecture(), false, nullptr)) {
-                  platforms.push_back(platform_sp);
-                  continue;
-                }
-              }
-
-              // Next check the host platform it if wasn't already checked
-              // above
-              if (host_platform_sp &&
-                  (!platform_sp ||
-                   host_platform_sp->GetName() != platform_sp->GetName())) {
-                if (host_platform_sp->IsCompatibleArchitecture(
-                        module_spec.GetArchitecture(), false, nullptr)) {
-                  platforms.push_back(host_platform_sp);
-                  continue;
-                }
+        // No architecture specified, check if there is only one platform for
+        // all of the architectures.
+
+        typedef std::vector<PlatformSP> PlatformList;
+        PlatformList platforms;
+        PlatformSP host_platform_sp = Platform::GetHostPlatform();
+        for (size_t i = 0; i < num_specs; ++i) {
+          ModuleSpec module_spec;
+          if (module_specs.GetModuleSpecAtIndex(i, module_spec)) {
+            // See if there was a selected platform and check that first
+            // since the user may have specified it.
+            if (platform_sp) {
+              if (platform_sp->IsCompatibleArchitecture(
+                      module_spec.GetArchitecture(), false, nullptr)) {
+                platforms.push_back(platform_sp);
+                continue;
               }
+            }
 
-              // Just find a platform that matches the architecture in the
-              // executable file
-              PlatformSP fallback_platform_sp(
-                  Platform::GetPlatformForArchitecture(
-                      module_spec.GetArchitecture(), nullptr));
-              if (fallback_platform_sp) {
-                platforms.push_back(fallback_platform_sp);
+            // Next check the host platform it if wasn't already checked
+            // above
+            if (host_platform_sp &&
+                (!platform_sp ||
+                 host_platform_sp->GetName() != platform_sp->GetName())) {
+              if (host_platform_sp->IsCompatibleArchitecture(
+                      module_spec.GetArchitecture(), false, nullptr)) {
+                platforms.push_back(host_platform_sp);
+                continue;
               }
             }
-          }
 
-          Platform *platform_ptr = nullptr;
-          bool more_than_one_platforms = false;
-          for (const auto &the_platform_sp : platforms) {
-            if (platform_ptr) {
-              if (platform_ptr->GetName() != the_platform_sp->GetName()) {
-                more_than_one_platforms = true;
-                platform_ptr = nullptr;
-                break;
-              }
-            } else {
-              platform_ptr = the_platform_sp.get();
+            // Just find a platform that matches the architecture in the
+            // executable file
+            PlatformSP fallback_platform_sp(
+                Platform::GetPlatformForArchitecture(
+                    module_spec.GetArchitecture(), nullptr));
+            if (fallback_platform_sp) {
+              platforms.push_back(fallback_platform_sp);
             }
           }
+        }
 
+        Platform *platform_ptr = nullptr;
+        bool more_than_one_platforms = false;
+        for (const auto &the_platform_sp : platforms) {
           if (platform_ptr) {
-            // All platforms for all modules in the executable match, so we can
-            // select this platform
-            platform_sp = platforms.front();
-          } else if (!more_than_one_platforms) {
-            // No platforms claim to support this file
-            error.SetErrorString("No matching platforms found for this file, "
-                                 "specify one with the --platform option");
-            return error;
+            if (platform_ptr->GetName() != the_platform_sp->GetName()) {
+              more_than_one_platforms = true;
+              platform_ptr = nullptr;
+              break;
+            }
           } else {
-            // More than one platform claims to support this file, so the
-            // --platform option must be specified
-            StreamString error_strm;
-            std::set<Platform *> platform_set;
-            error_strm.Printf(
-                "more than one platform supports this executable (");
-            for (const auto &the_platform_sp : platforms) {
-              if (platform_set.find(the_platform_sp.get()) ==
-                  platform_set.end()) {
-                if (!platform_set.empty())
-                  error_strm.PutCString(", ");
-                error_strm.PutCString(the_platform_sp->GetName().GetCString());
-                platform_set.insert(the_platform_sp.get());
-              }
+            platform_ptr = the_platform_sp.get();
+          }
+        }
+
+        if (platform_ptr) {
+          // All platforms for all modules in the executable match, so we can
+          // select this platform
+          platform_sp = platforms.front();
+        } else if (!more_than_one_platforms) {
+          // No platforms claim to support this file.
+          error.SetErrorString("no matching platforms found for this file");
+          return error;
+        } else {
+          // More than one platform claims to support this file.
+          StreamString error_strm;
+          std::set<Platform *> platform_set;
+          error_strm.Printf(
+              "more than one platform supports this executable (");
+          for (const auto &the_platform_sp : platforms) {
+            if (platform_set.find(the_platform_sp.get()) ==
+                platform_set.end()) {
+              if (!platform_set.empty())
+                error_strm.PutCString(", ");
+              error_strm.PutCString(the_platform_sp->GetName().GetCString());
+              platform_set.insert(the_platform_sp.get());
             }
-            error_strm.Printf(
-                "), use the --platform option to specify a platform");
-            error.SetErrorString(error_strm.GetString());
-            return error;
           }
+          error_strm.Printf("), specify an architecture to disambiguate");
+          error.SetErrorString(error_strm.GetString());
+          return error;
         }
       }
     }
   }
 
   // If we have a valid architecture, make sure the current platform is
-  // compatible with that architecture
+  // compatible with that architecture.
   if (!prefer_platform_arch && arch.IsValid()) {
     if (!platform_sp->IsCompatibleArchitecture(arch, false, &platform_arch)) {
       platform_sp = Platform::GetPlatformForArchitecture(arch, &platform_arch);
@@ -269,8 +259,8 @@
         debugger.GetPlatformList().SetSelectedPlatform(platform_sp);
     }
   } else if (platform_arch.IsValid()) {
-    // if "arch" isn't valid, yet "platform_arch" is, it means we have an
-    // executable file with a single architecture which should be used
+    // If "arch" isn't valid, yet "platform_arch" is, it means we have an
+    // executable file with a single architecture which should be used.
     ArchSpec fixed_platform_arch;
     if (!platform_sp->IsCompatibleArchitecture(platform_arch, false,
                                                &fixed_platform_arch)) {
@@ -284,10 +274,9 @@
   if (!platform_arch.IsValid())
     platform_arch = arch;
 
-  error = TargetList::CreateTargetInternal(
+  return TargetList::CreateTargetInternal(
       debugger, user_exe_path, platform_arch, load_dependent_files, platform_sp,
       target_sp, is_dummy_target);
-  return error;
 }
 
 lldb::TargetSP TargetList::GetDummyTarget(lldb_private::Debugger &debugger) {
Index: lldb/source/Commands/CommandObjectTarget.cpp
===================================================================
--- lldb/source/Commands/CommandObjectTarget.cpp
+++ lldb/source/Commands/CommandObjectTarget.cpp
@@ -23,6 +23,7 @@
 #include "lldb/Interpreter/OptionGroupBoolean.h"
 #include "lldb/Interpreter/OptionGroupFile.h"
 #include "lldb/Interpreter/OptionGroupFormat.h"
+#include "lldb/Interpreter/OptionGroupPlatform.h"
 #include "lldb/Interpreter/OptionGroupString.h"
 #include "lldb/Interpreter/OptionGroupUInt64.h"
 #include "lldb/Interpreter/OptionGroupUUID.h"
@@ -214,6 +215,7 @@
             "Create a target using the argument as the main executable.",
             nullptr),
         m_option_group(), m_arch_option(),
+        m_platform_options(true), // Include the --platform option.
         m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename,
                     "Fullpath to a core file to use for this target."),
         m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0,
@@ -240,6 +242,7 @@
     m_arguments.push_back(arg);
 
     m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
+    m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1);
     m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
     m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
     m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
@@ -311,7 +314,8 @@
       llvm::StringRef arch_cstr = m_arch_option.GetArchitectureName();
       Status error(debugger.GetTargetList().CreateTarget(
           debugger, file_path, arch_cstr,
-          m_add_dependents.m_load_dependent_files, nullptr, target_sp));
+          m_add_dependents.m_load_dependent_files, &m_platform_options,
+          target_sp));
 
       if (target_sp) {
         // Only get the platform after we create the target because we might
@@ -442,6 +446,7 @@
 private:
   OptionGroupOptions m_option_group;
   OptionGroupArchitecture m_arch_option;
+  OptionGroupPlatform m_platform_options;
   OptionGroupFile m_core_file;
   OptionGroupFile m_symbol_file;
   OptionGroupFile m_remote_file;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to