]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/tools/lldb-mi/MICmdCmdFile.cpp
Unbreak DRM KMS build by adding the needed compatibility field in the LinuxKPI.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / tools / lldb-mi / MICmdCmdFile.cpp
1 //===-- MICmdCmdFile.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // Overview:    CMICmdCmdFileExecAndSymbols     implementation.
10
11 // Third Party Headers:
12 #include "lldb/API/SBStream.h"
13
14 // In-house headers:
15 #include "MICmdArgValFile.h"
16 #include "MICmdArgValOptionLong.h"
17 #include "MICmdArgValOptionShort.h"
18 #include "MICmdArgValString.h"
19 #include "MICmdCmdFile.h"
20 #include "MICmnLLDBDebugSessionInfo.h"
21 #include "MICmnLLDBDebugger.h"
22 #include "MICmnMIResultRecord.h"
23 #include "MIUtilFileStd.h"
24
25 //++
26 // Details: CMICmdCmdFileExecAndSymbols constructor.
27 // Type:    Method.
28 // Args:    None.
29 // Return:  None.
30 // Throws:  None.
31 //--
32 CMICmdCmdFileExecAndSymbols::CMICmdCmdFileExecAndSymbols()
33     : m_constStrArgNameFile("file"), m_constStrArgNamedPlatformName("p"),
34       m_constStrArgNamedRemotePath("r") {
35   // Command factory matches this name with that received from the stdin stream
36   m_strMiCmd = "file-exec-and-symbols";
37
38   // Required by the CMICmdFactory when registering *this command
39   m_pSelfCreatorFn = &CMICmdCmdFileExecAndSymbols::CreateSelf;
40 }
41
42 //++
43 // Details: CMICmdCmdFileExecAndSymbols destructor.
44 // Type:    Overrideable.
45 // Args:    None.
46 // Return:  None.
47 // Throws:  None.
48 //--
49 CMICmdCmdFileExecAndSymbols::~CMICmdCmdFileExecAndSymbols() {}
50
51 //++
52 // Details: The invoker requires this function. The parses the command line
53 // options
54 //          arguments to extract values for each of those arguments.
55 // Type:    Overridden.
56 // Args:    None.
57 // Return:  MIstatus::success - Functional succeeded.
58 //          MIstatus::failure - Functional failed.
59 // Throws:  None.
60 //--
61 bool CMICmdCmdFileExecAndSymbols::ParseArgs() {
62   m_setCmdArgs.Add(new CMICmdArgValFile(m_constStrArgNameFile, true, true));
63   m_setCmdArgs.Add(
64       new CMICmdArgValOptionShort(m_constStrArgNamedPlatformName, false, true,
65                                   CMICmdArgValListBase::eArgValType_String, 1));
66   m_setCmdArgs.Add(new CMICmdArgValOptionShort(
67       m_constStrArgNamedRemotePath, false, true,
68       CMICmdArgValListBase::eArgValType_StringQuotedNumberPath, 1));
69   return ParseValidateCmdOptions();
70 }
71
72 //++
73 // Details: The invoker requires this function. The command does work in this
74 // function.
75 //          The command is likely to communicate with the LLDB SBDebugger in
76 //          here.
77 //          Synopsis: -file-exec-and-symbols file
78 //          Ref:
79 //          http://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-File-Commands.html#GDB_002fMI-File-Commands
80 // Type:    Overridden.
81 // Args:    None.
82 // Return:  MIstatus::success - Functional succeeded.
83 //          MIstatus::failure - Functional failed.
84 // Throws:  None.
85 //--
86 bool CMICmdCmdFileExecAndSymbols::Execute() {
87   CMICMDBASE_GETOPTION(pArgNamedFile, File, m_constStrArgNameFile);
88   CMICMDBASE_GETOPTION(pArgPlatformName, OptionShort,
89                        m_constStrArgNamedPlatformName);
90   CMICMDBASE_GETOPTION(pArgRemotePath, OptionShort,
91                        m_constStrArgNamedRemotePath);
92   CMICmdArgValFile *pArgFile = static_cast<CMICmdArgValFile *>(pArgNamedFile);
93   const CMIUtilString &strExeFilePath(pArgFile->GetValue());
94   bool bPlatformName = pArgPlatformName->GetFound();
95   CMIUtilString platformName;
96   if (bPlatformName) {
97     pArgPlatformName->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
98         platformName);
99   }
100   CMICmnLLDBDebugSessionInfo &rSessionInfo(
101       CMICmnLLDBDebugSessionInfo::Instance());
102   lldb::SBDebugger &rDbgr = rSessionInfo.GetDebugger();
103   lldb::SBError error;
104   const char *pTargetTriple = nullptr; // Let LLDB discover the triple required
105   const char *pTargetPlatformName = platformName.c_str();
106   const bool bAddDepModules = false;
107   lldb::SBTarget target =
108       rDbgr.CreateTarget(strExeFilePath.c_str(), pTargetTriple,
109                          pTargetPlatformName, bAddDepModules, error);
110   CMIUtilString strWkDir;
111   const CMIUtilString &rStrKeyWkDir(rSessionInfo.m_constStrSharedDataKeyWkDir);
112   if (!rSessionInfo.SharedDataRetrieve<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
113     strWkDir = CMIUtilFileStd::StripOffFileName(strExeFilePath);
114     if (!rSessionInfo.SharedDataAdd<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
115       SetError(CMIUtilString::Format(MIRSRC(IDS_DBGSESSION_ERR_SHARED_DATA_ADD),
116                                      m_cmdData.strMiCmd.c_str(),
117                                      rStrKeyWkDir.c_str()));
118       return MIstatus::failure;
119     }
120   }
121   if (!rDbgr.SetCurrentPlatformSDKRoot(strWkDir.c_str())) {
122
123     SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_FNFAILED),
124                                    m_cmdData.strMiCmd.c_str(),
125                                    "SetCurrentPlatformSDKRoot()"));
126     return MIstatus::failure;
127   }
128   if (pArgRemotePath->GetFound()) {
129     CMIUtilString remotePath;
130     pArgRemotePath->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
131         remotePath);
132     lldb::SBModule module = target.FindModule(target.GetExecutable());
133     if (module.IsValid()) {
134       module.SetPlatformFileSpec(lldb::SBFileSpec(remotePath.c_str()));
135     }
136   }
137   lldb::SBStream err;
138   if (error.Fail()) {
139     const bool bOk = error.GetDescription(err);
140     MIunused(bOk);
141   }
142   if (!target.IsValid()) {
143     SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INVALID_TARGET),
144                                    m_cmdData.strMiCmd.c_str(),
145                                    strExeFilePath.c_str(), err.GetData()));
146     return MIstatus::failure;
147   }
148   if (error.Fail()) {
149     SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_CREATE_TARGET),
150                                    m_cmdData.strMiCmd.c_str(), err.GetData()));
151     return MIstatus::failure;
152   }
153
154   return MIstatus::success;
155 }
156
157 //++
158 // Details: The invoker requires this function. The command prepares a MI Record
159 // Result
160 //          for the work carried out in the Execute().
161 // Type:    Overridden.
162 // Args:    None.
163 // Return:  MIstatus::success - Functional succeeded.
164 //          MIstatus::failure - Functional failed.
165 // Throws:  None.
166 //--
167 bool CMICmdCmdFileExecAndSymbols::Acknowledge() {
168   const CMICmnMIResultRecord miRecordResult(
169       m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
170   m_miResultRecord = miRecordResult;
171
172   return MIstatus::success;
173 }
174
175 //++
176 // Details: Required by the CMICmdFactory when registering *this command. The
177 // factory
178 //          calls this function to create an instance of *this command.
179 // Type:    Static method.
180 // Args:    None.
181 // Return:  CMICmdBase * - Pointer to a new command.
182 // Throws:  None.
183 //--
184 CMICmdBase *CMICmdCmdFileExecAndSymbols::CreateSelf() {
185   return new CMICmdCmdFileExecAndSymbols();
186 }
187
188 //++
189 // Details: If the MI Driver is not operating via a client i.e. Eclipse but say
190 // operating
191 //          on a executable passed in as a argument to the drive then what
192 //          should the driver
193 //          do on a command failing? Either continue operating or exit the
194 //          application.
195 //          Override this function where a command failure cannot allow the
196 //          driver to
197 //          continue operating.
198 // Type:    Overridden.
199 // Args:    None.
200 // Return:  bool - True = Fatal if command fails, false = can continue if
201 // command fails.
202 // Throws:  None.
203 //--
204 bool CMICmdCmdFileExecAndSymbols::GetExitAppOnCommandFailure() const {
205   return true;
206 }