<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/92419>92419</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [lldb] TargetList::CreateTargetInternal() selects wrong platform
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lldb
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          slydiman
      </td>
    </tr>
</table>

<pre>
    The `lldb/test/API/python_api/debugger/TestDebuggerAPI.py` test failed in case of Windows host (x86_64) and Linux target (Aarch64).

`target1 = self.dbg.CreateTarget(exe, None, "remote-linux", False, error)` calls TargetList::CreateTargetInternal() in llvm-project/lldb/source/Target/TargetList.cpp, line 76

platform_options contains `remote-linux`, `triple_str is empty`, user_exe_path is a path to ELF created from elf.yaml (x86_64).

`platform_arch` is updated to `x86_64` in TargetList.cpp, line 169:
```
// Only one arch and none was specified.
prefer_platform_arch = true;
platform_arch = matching_module_spec.GetArchitecture();
```
and platform_sp is updated to `host` in TargetList.cpp, line 226:
```
// 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, {}, ArchSpec::CompatibleMatch, nullptr)) {
  platform_sp = platform_list.GetOrCreate(platform_arch, {}, &fixed_platform_arch);
  if (platform_sp)
 platform_list.SetSelectedPlatform(platform_sp);
}
```
Next call `target2 = self.dbg.CreateTarget(exe, None, "remote-linux", False, error)` will create the platform remote-linux in TargetList.cpp, line 98:
```
// 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;
}
```
If the user specifies an explicit platform in the CreateTarget function, that platform should really take precedence over anything else.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VlGP2joT_TXmZVQUzBLIAw90KdVK_dpKW6mPyHEmxN917Mh2Fvj3V-OEbML27uo-3KpaMGOPz5w5Mx7hvToZxC1bfWar_Uy0obJu6_W1ULUws9wW1-2vCoGlidZFzvghoA-MH3Y_nxg_NNdQWXMUjWL8UGDenk7oGD_8Qh_2_XL382neXFmaAB2FUiiNBSgDUngEW8JvZQp79lBZH4DxzWWTHtMHxjMQpoBvyrQXCMKdMFp3wskqmucs2bNk1_9Nk27PAthyDx51OS_y0_zRoQj4K5oY3-AFGX-E79bET8a5w9oG_KTpGsY5_XoQ2kczOmcd4xmBl0JrD52jb8oHttyx5W7s_skEdEZoxjcEXhnQ-qX-1Dj7f5TEWc-gt62TSCz1qA6vXueyaehmrQzCOh1H2GgRSuvqo22CssaDtCYIZTwlZxJFmsTY0iQ41Wg8-uBAecC6Cdfe2Hp0R7zgsRGhIqOA-C1Y-PLtADKGVUDpbA1E5VXUepKbe_IHdJQe4kt5aJsiegmWwPRHyWTgHyJepBnxenPa_--W_MD4AX4YfQVrEOieKBBDq7Pw4BuUqlRY9NgahyW64wRZFEdwLbLl5ztaB3MtgqyUOR1rW7REX4Ny_hXDzslKBZShddjleHByh5VgDX5985YLkvr7THCefsDEU0nyjXRzDsobxtcBXoRWBXm5xnLh07zEjWRVAWoUxsMZoRIvCMJMnOMFZRtErhFKpRHOKlQgwCtz0h33NybgXClZga9sqwvIkaR1ywAx9tyghFJdsJhmYuBOURwbxhcjxj6x5Zcn_2jrRgSVa7yj_i6oR2Drz2y9p2-3K_vyHDz8j5JKG0yrdRNiVfMsHowoYJIwksGw1pSbrxh-uK7a3wfAePqnaEdiAehCHl1I5s42vfUZwzNqlAGLn73h7cFBhOv9H_XyHS8hti8YmiT_j5rkWWndNw8IFQ7RwNjBO6rPNh-IvkMKAgyeX72rkvrXbTXpBXTZBMmte1KNFhb7sok1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