1 //===-- PlatformLinux.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 #include "PlatformLinux.h" 11 12 // C Includes 13 #include <stdio.h> 14 #include <sys/utsname.h> 15 16 // C++ Includes 17 // Other libraries and framework includes 18 // Project includes 19 #include "lldb/Core/Error.h" 20 #include "lldb/Core/Debugger.h" 21 #include "lldb/Core/Module.h" 22 #include "lldb/Core/ModuleList.h" 23 #include "lldb/Core/PluginManager.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Host/FileSpec.h" 26 #include "lldb/Host/Host.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Target/Process.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 static uint32_t g_initialize_count = 0; 34 35 Platform * 36 PlatformLinux::CreateInstance () 37 { 38 return new PlatformLinux(true); 39 } 40 41 const char * 42 PlatformLinux::GetPluginNameStatic() 43 { 44 return "plugin.platform.linux"; 45 } 46 47 const char * 48 PlatformLinux::GetShortPluginNameStatic (bool is_host) 49 { 50 if (is_host) 51 return Platform::GetHostPlatformName (); 52 else 53 return "remote-linux"; 54 } 55 56 const char * 57 PlatformLinux::GetPluginDescriptionStatic (bool is_host) 58 { 59 if (is_host) 60 return "Local Linux user platform plug-in."; 61 else 62 return "Remote Linux user platform plug-in."; 63 } 64 65 void 66 PlatformLinux::Initialize () 67 { 68 if (g_initialize_count++ == 0) 69 { 70 #if defined(__linux__) 71 PlatformSP default_platform_sp (new PlatformLinux(true)); 72 default_platform_sp->SetSystemArchitecture (Host::GetArchitecture()); 73 Platform::SetDefaultPlatform (default_platform_sp); 74 #endif 75 PluginManager::RegisterPlugin(PlatformLinux::GetShortPluginNameStatic(false), 76 PlatformLinux::GetPluginDescriptionStatic(false), 77 PlatformLinux::CreateInstance); 78 } 79 } 80 81 void 82 PlatformLinux::Terminate () 83 { 84 if (g_initialize_count > 0) 85 { 86 if (--g_initialize_count == 0) 87 { 88 PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 89 } 90 } 91 } 92 93 Error 94 PlatformLinux::ResolveExecutable (const FileSpec &exe_file, 95 const ArchSpec &exe_arch, 96 lldb::ModuleSP &exe_module_sp) 97 { 98 Error error; 99 // Nothing special to do here, just use the actual file and architecture 100 101 char exe_path[PATH_MAX]; 102 FileSpec resolved_exe_file (exe_file); 103 104 if (IsHost()) 105 { 106 // If we have "ls" as the exe_file, resolve the executable location based on 107 // the current path variables 108 if (!resolved_exe_file.Exists()) 109 { 110 exe_file.GetPath(exe_path, sizeof(exe_path)); 111 resolved_exe_file.SetFile(exe_path, true); 112 } 113 114 if (!resolved_exe_file.Exists()) 115 resolved_exe_file.ResolveExecutableLocation (); 116 117 if (resolved_exe_file.Exists()) 118 error.Clear(); 119 else 120 { 121 exe_file.GetPath(exe_path, sizeof(exe_path)); 122 error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 123 } 124 } 125 else 126 { 127 if (m_remote_platform_sp) 128 { 129 error = m_remote_platform_sp->ResolveExecutable (exe_file, 130 exe_arch, 131 exe_module_sp, 132 NULL); 133 } 134 else 135 { 136 // We may connect to a process and use the provided executable (Don't use local $PATH). 137 138 if (resolved_exe_file.Exists()) 139 error.Clear(); 140 else 141 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 142 } 143 } 144 145 if (error.Success()) 146 { 147 if (exe_arch.IsValid()) 148 { 149 error = ModuleList::GetSharedModule (resolved_exe_file, 150 exe_arch, 151 NULL, 152 NULL, 153 0, 154 exe_module_sp, 155 NULL, 156 NULL, 157 NULL); 158 159 if (exe_module_sp->GetObjectFile() == NULL) 160 { 161 exe_module_sp.reset(); 162 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain the architecture %s", 163 exe_file.GetDirectory().AsCString(""), 164 exe_file.GetDirectory() ? "/" : "", 165 exe_file.GetFilename().AsCString(""), 166 exe_arch.GetArchitectureName()); 167 } 168 } 169 else 170 { 171 // No valid architecture was specified, ask the platform for 172 // the architectures that we should be using (in the correct order) 173 // and see if we can find a match that way 174 StreamString arch_names; 175 ArchSpec platform_arch; 176 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, platform_arch); ++idx) 177 { 178 error = ModuleList::GetSharedModule (resolved_exe_file, 179 platform_arch, 180 NULL, 181 NULL, 182 0, 183 exe_module_sp, 184 NULL, 185 NULL, 186 NULL); 187 // Did we find an executable using one of the 188 if (error.Success()) 189 { 190 if (exe_module_sp && exe_module_sp->GetObjectFile()) 191 break; 192 else 193 error.SetErrorToGenericError(); 194 } 195 196 if (idx > 0) 197 arch_names.PutCString (", "); 198 arch_names.PutCString (platform_arch.GetArchitectureName()); 199 } 200 201 if (error.Fail() || !exe_module_sp) 202 { 203 error.SetErrorStringWithFormat ("'%s%s%s' doesn't contain any '%s' platform architectures: %s", 204 exe_file.GetDirectory().AsCString(""), 205 exe_file.GetDirectory() ? "/" : "", 206 exe_file.GetFilename().AsCString(""), 207 GetShortPluginName(), 208 arch_names.GetString().c_str()); 209 } 210 } 211 } 212 213 return error; 214 } 215 216 Error 217 PlatformLinux::GetFile (const FileSpec &platform_file, 218 const UUID *uuid_ptr, FileSpec &local_file) 219 { 220 if (IsRemote()) 221 { 222 if (m_remote_platform_sp) 223 return m_remote_platform_sp->GetFile (platform_file, uuid_ptr, local_file); 224 } 225 226 // Default to the local case 227 local_file = platform_file; 228 return Error(); 229 } 230 231 232 //------------------------------------------------------------------ 233 /// Default Constructor 234 //------------------------------------------------------------------ 235 PlatformLinux::PlatformLinux (bool is_host) : 236 Platform(is_host), // This is the local host platform 237 m_remote_platform_sp () 238 { 239 } 240 241 //------------------------------------------------------------------ 242 /// Destructor. 243 /// 244 /// The destructor is virtual since this class is designed to be 245 /// inherited from by the plug-in instance. 246 //------------------------------------------------------------------ 247 PlatformLinux::~PlatformLinux() 248 { 249 } 250 251 bool 252 PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 253 { 254 bool success = false; 255 if (IsHost()) 256 { 257 success = Platform::GetProcessInfo (pid, process_info); 258 } 259 else 260 { 261 if (m_remote_platform_sp) 262 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 263 } 264 return success; 265 } 266 267 bool 268 PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 269 { 270 if (idx == 0) 271 { 272 arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture); 273 return arch.IsValid(); 274 } 275 return false; 276 } 277 278 void 279 PlatformLinux::GetStatus (Stream &strm) 280 { 281 struct utsname un; 282 283 if (uname(&un)) { 284 strm << "Linux"; 285 return; 286 } 287 288 strm << un.sysname << ' ' << un.release << ' ' << un.version << '\n'; 289 } 290 291 size_t 292 PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 293 BreakpointSite *bp_site) 294 { 295 ArchSpec arch = target.GetArchitecture(); 296 const uint8_t *trap_opcode = NULL; 297 size_t trap_opcode_size = 0; 298 299 switch (arch.GetCore()) 300 { 301 default: 302 assert(false && "CPU type not supported!"); 303 break; 304 305 case ArchSpec::eCore_x86_32_i386: 306 case ArchSpec::eCore_x86_64_x86_64: 307 { 308 static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 309 trap_opcode = g_i386_breakpoint_opcode; 310 trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 311 } 312 break; 313 } 314 315 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 316 return trap_opcode_size; 317 return 0; 318 } 319 320 Error 321 PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info) 322 { 323 Error error; 324 325 if (IsHost()) 326 { 327 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 328 { 329 const bool is_localhost = true; 330 if (!launch_info.ConvertArgumentsForLaunchingInShell (error, is_localhost)) 331 return error; 332 } 333 error = Platform::LaunchProcess (launch_info); 334 } 335 else 336 { 337 error.SetErrorString ("the platform is not currently connected"); 338 } 339 return error; 340 } 341 342 lldb::ProcessSP 343 PlatformLinux::Attach(ProcessAttachInfo &attach_info, 344 Debugger &debugger, 345 Target *target, 346 Listener &listener, 347 Error &error) 348 { 349 lldb::ProcessSP process_sp; 350 if (IsHost()) 351 { 352 if (target == NULL) 353 { 354 TargetSP new_target_sp; 355 FileSpec emptyFileSpec; 356 ArchSpec emptyArchSpec; 357 358 error = debugger.GetTargetList().CreateTarget (debugger, 359 emptyFileSpec, 360 emptyArchSpec, 361 false, 362 m_remote_platform_sp, 363 new_target_sp); 364 target = new_target_sp.get(); 365 } 366 else 367 error.Clear(); 368 369 if (target && error.Success()) 370 { 371 debugger.GetTargetList().SetSelectedTarget(target); 372 373 process_sp = target->CreateProcess (listener, 374 attach_info.GetProcessPluginName(), 375 NULL); 376 377 if (process_sp) 378 error = process_sp->Attach (attach_info); 379 } 380 } 381 else 382 { 383 if (m_remote_platform_sp) 384 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 385 else 386 error.SetErrorString ("the platform is not currently connected"); 387 } 388 return process_sp; 389 } 390