]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/lldb-mi/MICmdCmdEnviro.cpp
Vendor import of lldb trunk r351319 (just before the release_80 branch
[FreeBSD/FreeBSD.git] / tools / lldb-mi / MICmdCmdEnviro.cpp
1 //===-- MICmdCmdEnviro.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // Overview:    CMICmdCmdEnvironmentCd          implementation.
11
12 // In-house headers:
13 #include "MICmdCmdEnviro.h"
14 #include "MICmdArgValFile.h"
15 #include "MICmnLLDBDebugSessionInfo.h"
16 #include "MICmnLLDBDebugger.h"
17 #include "MICmnMIResultRecord.h"
18 #include "MICmnMIValueConst.h"
19
20 //++
21 //------------------------------------------------------------------------------------
22 // Details: CMICmdCmdEnvironmentCd constructor.
23 // Type:    Method.
24 // Args:    None.
25 // Return:  None.
26 // Throws:  None.
27 //--
28 CMICmdCmdEnvironmentCd::CMICmdCmdEnvironmentCd()
29     : m_constStrArgNamePathDir("pathdir") {
30   // Command factory matches this name with that received from the stdin stream
31   m_strMiCmd = "environment-cd";
32
33   // Required by the CMICmdFactory when registering *this command
34   m_pSelfCreatorFn = &CMICmdCmdEnvironmentCd::CreateSelf;
35 }
36
37 //++
38 //------------------------------------------------------------------------------------
39 // Details: CMICmdCmdEnvironmentCd destructor.
40 // Type:    Overrideable.
41 // Args:    None.
42 // Return:  None.
43 // Throws:  None.
44 //--
45 CMICmdCmdEnvironmentCd::~CMICmdCmdEnvironmentCd() {}
46
47 //++
48 //------------------------------------------------------------------------------------
49 // Details: The invoker requires this function. The parses the command line
50 // options
51 //          arguments to extract values for each of those arguments.
52 // Type:    Overridden.
53 // Args:    None.
54 // Return:  MIstatus::success - Functional succeeded.
55 //          MIstatus::failure - Functional failed.
56 // Throws:  None.
57 //--
58 bool CMICmdCmdEnvironmentCd::ParseArgs() {
59   m_setCmdArgs.Add(new CMICmdArgValFile(m_constStrArgNamePathDir, true, true));
60   CMICmdArgContext argCntxt(m_cmdData.strMiCmdOption);
61   return ParseValidateCmdOptions();
62 }
63
64 //++
65 //------------------------------------------------------------------------------------
66 // Details: The invoker requires this function. The command does work in this
67 // function.
68 //          The command is likely to communicate with the LLDB SBDebugger in
69 //          here.
70 // Type:    Overridden.
71 // Args:    None.
72 // Return:  MIstatus::success - Functional succeeded.
73 //          MIstatus::failure - Functional failed.
74 // Throws:  None.
75 //--
76 bool CMICmdCmdEnvironmentCd::Execute() {
77   CMICMDBASE_GETOPTION(pArgPathDir, File, m_constStrArgNamePathDir);
78   const CMIUtilString &strWkDir(pArgPathDir->GetValue());
79   CMICmnLLDBDebugger &rDbg(CMICmnLLDBDebugger::Instance());
80   lldb::SBDebugger &rLldbDbg = rDbg.GetTheDebugger();
81   bool bOk = rLldbDbg.SetCurrentPlatformSDKRoot(strWkDir.c_str());
82   if (bOk) {
83     const CMIUtilString &rStrKeyWkDir(
84         m_rLLDBDebugSessionInfo.m_constStrSharedDataKeyWkDir);
85     if (!m_rLLDBDebugSessionInfo.SharedDataAdd<CMIUtilString>(rStrKeyWkDir,
86                                                               strWkDir)) {
87       SetError(CMIUtilString::Format(MIRSRC(IDS_DBGSESSION_ERR_SHARED_DATA_ADD),
88                                      m_cmdData.strMiCmd.c_str(),
89                                      rStrKeyWkDir.c_str()));
90       bOk = MIstatus::failure;
91     }
92   } else
93     SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_FNFAILED),
94                                    m_cmdData.strMiCmd.c_str(),
95                                    "SetCurrentPlatformSDKRoot()"));
96
97   lldb::SBTarget sbTarget = m_rLLDBDebugSessionInfo.GetTarget();
98   if (sbTarget.IsValid()) {
99     lldb::SBLaunchInfo sbLaunchInfo = sbTarget.GetLaunchInfo();
100     sbLaunchInfo.SetWorkingDirectory(strWkDir.c_str());
101     sbTarget.SetLaunchInfo(sbLaunchInfo);
102   }
103
104   return bOk;
105 }
106
107 //++
108 //------------------------------------------------------------------------------------
109 // Details: The invoker requires this function. The command prepares a MI Record
110 // Result
111 //          for the work carried out in the Execute().
112 // Type:    Overridden.
113 // Args:    None.
114 // Return:  MIstatus::success - Functional succeeded.
115 //          MIstatus::failure - Functional failed.
116 // Throws:  None.
117 //--
118 bool CMICmdCmdEnvironmentCd::Acknowledge() {
119   const CMIUtilString &rStrKeyWkDir(
120       m_rLLDBDebugSessionInfo.m_constStrSharedDataKeyWkDir);
121   CMIUtilString strWkDir;
122   const bool bOk = m_rLLDBDebugSessionInfo.SharedDataRetrieve<CMIUtilString>(
123       rStrKeyWkDir, strWkDir);
124   if (bOk) {
125     const CMICmnMIValueConst miValueConst(strWkDir);
126     const CMICmnMIValueResult miValueResult("path", miValueConst);
127     const CMICmnMIResultRecord miRecordResult(
128         m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done,
129         miValueResult);
130     m_miResultRecord = miRecordResult;
131     return MIstatus::success;
132   }
133
134   SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_SHARED_DATA_NOT_FOUND),
135                                  m_cmdData.strMiCmd.c_str(),
136                                  rStrKeyWkDir.c_str()));
137   return MIstatus::failure;
138 }
139
140 //++
141 //------------------------------------------------------------------------------------
142 // Details: Required by the CMICmdFactory when registering *this command. The
143 // factory
144 //          calls this function to create an instance of *this command.
145 // Type:    Static method.
146 // Args:    None.
147 // Return:  CMICmdBase * - Pointer to a new command.
148 // Throws:  None.
149 //--
150 CMICmdBase *CMICmdCmdEnvironmentCd::CreateSelf() {
151   return new CMICmdCmdEnvironmentCd();
152 }