aboutsummaryrefslogtreecommitdiff
path: root/source/API/SBPlatform.cpp
blob: b8dc01ba7811b83199b90786edae5e4a796eeaf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//===-- SBPlatform.cpp ------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SBPlatform.h"
#include "lldb/API/SBError.h"
#include "lldb/API/SBFileSpec.h"
#include "lldb/API/SBLaunchInfo.h"
#include "lldb/API/SBUnixSignals.h"
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/Error.h"
#include "lldb/Host/File.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Platform.h"

#include <functional>

using namespace lldb;
using namespace lldb_private;

//----------------------------------------------------------------------
// PlatformConnectOptions
//----------------------------------------------------------------------
struct PlatformConnectOptions {
    PlatformConnectOptions(const char *url = NULL) :
        m_url(),
        m_rsync_options(),
        m_rsync_remote_path_prefix(),
        m_rsync_enabled(false),
        m_rsync_omit_hostname_from_remote_path(false),
        m_local_cache_directory ()
    {
        if (url && url[0])
            m_url = url;
    }
    
    ~PlatformConnectOptions()
    {
    }

    std::string m_url;
    std::string m_rsync_options;
    std::string m_rsync_remote_path_prefix;
    bool m_rsync_enabled;
    bool m_rsync_omit_hostname_from_remote_path;
    ConstString m_local_cache_directory;
};

//----------------------------------------------------------------------
// PlatformShellCommand
//----------------------------------------------------------------------
struct PlatformShellCommand {
    PlatformShellCommand(const char *shell_command = NULL) :
        m_command(),
        m_working_dir(),
        m_status(0),
        m_signo(0),
        m_timeout_sec(UINT32_MAX)
    {
        if (shell_command && shell_command[0])
            m_command = shell_command;
    }
    
    ~PlatformShellCommand()
    {
    }
    
    std::string m_command;
    std::string m_working_dir;
    std::string m_output;
    int m_status;
    int m_signo;
    uint32_t m_timeout_sec;
};
//----------------------------------------------------------------------
// SBPlatformConnectOptions
//----------------------------------------------------------------------
SBPlatformConnectOptions::SBPlatformConnectOptions (const char *url) :
    m_opaque_ptr(new PlatformConnectOptions(url))
{
    
}

SBPlatformConnectOptions::SBPlatformConnectOptions(const SBPlatformConnectOptions &rhs) :
    m_opaque_ptr(new PlatformConnectOptions())
{
    *m_opaque_ptr = *rhs.m_opaque_ptr;
}
    
SBPlatformConnectOptions::~SBPlatformConnectOptions ()
{
    delete m_opaque_ptr;
}

void
SBPlatformConnectOptions::operator=(const SBPlatformConnectOptions &rhs)
{
    *m_opaque_ptr = *rhs.m_opaque_ptr;
}

const char *
SBPlatformConnectOptions::GetURL()
{
    if (m_opaque_ptr->m_url.empty())
        return NULL;
    return m_opaque_ptr->m_url.c_str();
}
    
void
SBPlatformConnectOptions::SetURL(const char *url)
{
    if (url && url[0])
        m_opaque_ptr->m_url = url;
    else
        m_opaque_ptr->m_url.clear();
}

bool
SBPlatformConnectOptions::GetRsyncEnabled()
{
    return m_opaque_ptr->m_rsync_enabled;
}
    
void
SBPlatformConnectOptions::EnableRsync (const char *options,
                                       const char *remote_path_prefix,
                                       bool omit_hostname_from_remote_path)
{
    m_opaque_ptr->m_rsync_enabled = true;
    m_opaque_ptr->m_rsync_omit_hostname_from_remote_path = omit_hostname_from_remote_path;
    if (remote_path_prefix && remote_path_prefix[0])
        m_opaque_ptr->m_rsync_remote_path_prefix = remote_path_prefix;
    else
        m_opaque_ptr->m_rsync_remote_path_prefix.clear();

    if (options && options[0])
        m_opaque_ptr->m_rsync_options = options;
    else
        m_opaque_ptr->m_rsync_options.clear();

}

void
SBPlatformConnectOptions::DisableRsync ()
{
    m_opaque_ptr->m_rsync_enabled = false;
}
    
const char *
SBPlatformConnectOptions::GetLocalCacheDirectory()
{
    return m_opaque_ptr->m_local_cache_directory.GetCString();
}
    
void
SBPlatformConnectOptions::SetLocalCacheDirectory(const char *path)
{
    if (path && path[0])
        m_opaque_ptr->m_local_cache_directory.SetCString(path);
    else
        m_opaque_ptr->m_local_cache_directory = ConstString();
}

//----------------------------------------------------------------------
// SBPlatformShellCommand
//----------------------------------------------------------------------
SBPlatformShellCommand::SBPlatformShellCommand (const char *shell_command) :
    m_opaque_ptr(new PlatformShellCommand(shell_command))
{
}

SBPlatformShellCommand::SBPlatformShellCommand (const SBPlatformShellCommand &rhs) :
    m_opaque_ptr(new PlatformShellCommand())
{
    *m_opaque_ptr = *rhs.m_opaque_ptr;
}

SBPlatformShellCommand::~SBPlatformShellCommand()
{
    delete m_opaque_ptr;
}

void
SBPlatformShellCommand::Clear()
{
    m_opaque_ptr->m_output = std::string();
    m_opaque_ptr->m_status = 0;
    m_opaque_ptr->m_signo = 0;
}

const char *
SBPlatformShellCommand::GetCommand()
{
    if (m_opaque_ptr->m_command.empty())
        return NULL;
    return m_opaque_ptr->m_command.c_str();
}

void
SBPlatformShellCommand::SetCommand(const char *shell_command)
{
    if (shell_command && shell_command[0])
        m_opaque_ptr->m_command = shell_command;
    else
        m_opaque_ptr->m_command.clear();
}

const char *
SBPlatformShellCommand::GetWorkingDirectory ()
{
    if (m_opaque_ptr->m_working_dir.empty())
        return NULL;
    return m_opaque_ptr->m_working_dir.c_str();
}

void
SBPlatformShellCommand::SetWorkingDirectory (const char *path)
{
    if (path && path[0])
        m_opaque_ptr->m_working_dir = path;
    else
        m_opaque_ptr->m_working_dir.clear();
}

uint32_t
SBPlatformShellCommand::GetTimeoutSeconds ()
{
    return m_opaque_ptr->m_timeout_sec;
}

void
SBPlatformShellCommand::SetTimeoutSeconds (uint32_t sec)
{
    m_opaque_ptr->m_timeout_sec = sec;
}

int
SBPlatformShellCommand::GetSignal ()
{
    return m_opaque_ptr->m_signo;
}

int
SBPlatformShellCommand::GetStatus ()
{
    return m_opaque_ptr->m_status;
}

const char *
SBPlatformShellCommand::GetOutput ()
{
    if (m_opaque_ptr->m_output.empty())
        return NULL;
    return m_opaque_ptr->m_output.c_str();
}

//----------------------------------------------------------------------
// SBPlatform
//----------------------------------------------------------------------
SBPlatform::SBPlatform () :
    m_opaque_sp ()
{
    
}

SBPlatform::SBPlatform (const char *platform_name) :
    m_opaque_sp ()
{
    Error error;
    if (platform_name && platform_name[0])
        m_opaque_sp = Platform::Create (ConstString(platform_name), error);
}

SBPlatform::~SBPlatform()
{
}

bool
SBPlatform::IsValid () const
{
    return m_opaque_sp.get() != NULL;
}

void
SBPlatform::Clear ()
{
    m_opaque_sp.reset();
}

const char *
SBPlatform::GetName ()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
        return platform_sp->GetName().GetCString();
    return NULL;
}

lldb::PlatformSP
SBPlatform::GetSP () const
{
    return m_opaque_sp;
}

void
SBPlatform::SetSP (const lldb::PlatformSP& platform_sp)
{
    m_opaque_sp = platform_sp;
}

const char *
SBPlatform::GetWorkingDirectory()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
        return platform_sp->GetWorkingDirectory().GetCString();
    return NULL;
}

bool
SBPlatform::SetWorkingDirectory(const char *path)
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        if (path)
            platform_sp->SetWorkingDirectory(FileSpec{path, false});
        else
            platform_sp->SetWorkingDirectory(FileSpec{});
        return true;
    }
    return false;
}

SBError
SBPlatform::ConnectRemote (SBPlatformConnectOptions &connect_options)
{
    SBError sb_error;
    PlatformSP platform_sp(GetSP());
    if (platform_sp && connect_options.GetURL())
    {
        Args args;
        args.AppendArgument(connect_options.GetURL());
        sb_error.ref() = platform_sp->ConnectRemote(args);
    }
    else
    {
        sb_error.SetErrorString("invalid platform");
    }
    return sb_error;
}

void
SBPlatform::DisconnectRemote ()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
        platform_sp->DisconnectRemote();
}

bool
SBPlatform::IsConnected()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
        platform_sp->IsConnected();
    return false;
}

const char *
SBPlatform::GetTriple()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        ArchSpec arch(platform_sp->GetSystemArchitecture());
        if (arch.IsValid())
        {
            // Const-ify the string so we don't need to worry about the lifetime of the string
            return ConstString(arch.GetTriple().getTriple().c_str()).GetCString();
        }
    }
    return NULL;
}

const char *
SBPlatform::GetOSBuild()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        std::string s;
        if (platform_sp->GetOSBuildString(s))
        {
            if (!s.empty())
            {
                // Const-ify the string so we don't need to worry about the lifetime of the string
                return ConstString(s.c_str()).GetCString();
            }
        }
    }
    return NULL;
}

const char *
SBPlatform::GetOSDescription()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        std::string s;
        if (platform_sp->GetOSKernelDescription(s))
        {
            if (!s.empty())
            {
                // Const-ify the string so we don't need to worry about the lifetime of the string
                return ConstString(s.c_str()).GetCString();
            }
        }
    }
    return NULL;
}

const char *
SBPlatform::GetHostname ()
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
        return platform_sp->GetHostname();
    return NULL;
}

uint32_t
SBPlatform::GetOSMajorVersion ()
{
    uint32_t major, minor, update;
    PlatformSP platform_sp(GetSP());
    if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
        return major;
    return UINT32_MAX;
    
}

uint32_t
SBPlatform::GetOSMinorVersion ()
{
    uint32_t major, minor, update;
    PlatformSP platform_sp(GetSP());
    if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
        return minor;
    return UINT32_MAX;
}

uint32_t
SBPlatform::GetOSUpdateVersion ()
{
    uint32_t major, minor, update;
    PlatformSP platform_sp(GetSP());
    if (platform_sp && platform_sp->GetOSVersion(major, minor, update))
        return update;
    return UINT32_MAX;
}

SBError
SBPlatform::Get (SBFileSpec &src,
                 SBFileSpec &dst)
{
    SBError sb_error;
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        sb_error.ref() = platform_sp->GetFile(src.ref(), dst.ref());
    }
    else
    {
        sb_error.SetErrorString("invalid platform");
    }
    return sb_error;
}

SBError
SBPlatform::Put (SBFileSpec &src,
                 SBFileSpec &dst)
{
  return ExecuteConnected(
      [&](const lldb::PlatformSP& platform_sp)
      {
          if (src.Exists())
          {
              uint32_t permissions = src.ref().GetPermissions();
              if (permissions == 0)
              {
                  if (src.ref().GetFileType() == FileSpec::eFileTypeDirectory)
                      permissions = eFilePermissionsDirectoryDefault;
                  else
                      permissions = eFilePermissionsFileDefault;
              }

              return platform_sp->PutFile(src.ref(), dst.ref(), permissions);
          }

          Error error;
          error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
          return error;
      });
}

SBError
SBPlatform::Install (SBFileSpec &src,
                     SBFileSpec &dst)
{
  return ExecuteConnected(
      [&](const lldb::PlatformSP& platform_sp)
      {
          if (src.Exists())
              return platform_sp->Install(src.ref(), dst.ref());

          Error error;
          error.SetErrorStringWithFormat("'src' argument doesn't exist: '%s'", src.ref().GetPath().c_str());
          return error;
      });
}


SBError
SBPlatform::Run (SBPlatformShellCommand &shell_command)
{
    return ExecuteConnected(
        [&](const lldb::PlatformSP& platform_sp)
        {
            const char *command = shell_command.GetCommand();
            if (!command)
                return Error("invalid shell command (empty)");

            const char *working_dir = shell_command.GetWorkingDirectory();
            if (working_dir == NULL)
            {
                working_dir = platform_sp->GetWorkingDirectory().GetCString();
                if (working_dir)
                    shell_command.SetWorkingDirectory(working_dir);
            }
            return platform_sp->RunShellCommand(command,
                                                FileSpec{working_dir, false},
                                                &shell_command.m_opaque_ptr->m_status,
                                                &shell_command.m_opaque_ptr->m_signo,
                                                &shell_command.m_opaque_ptr->m_output,
                                                shell_command.m_opaque_ptr->m_timeout_sec);
        });
}

SBError
SBPlatform::Launch (SBLaunchInfo &launch_info)
{
    return ExecuteConnected(
        [&](const lldb::PlatformSP& platform_sp)
        {
            return platform_sp->LaunchProcess(launch_info.ref());
        });
}

SBError
SBPlatform::Kill (const lldb::pid_t pid)
{
    return ExecuteConnected(
        [&](const lldb::PlatformSP& platform_sp)
        {
            return platform_sp->KillProcess(pid);
        });
}

SBError
SBPlatform::ExecuteConnected (const std::function<Error(const lldb::PlatformSP&)>& func)
{
    SBError sb_error;
    const auto platform_sp(GetSP());
    if (platform_sp)
    {
        if (platform_sp->IsConnected())
            sb_error.ref() = func(platform_sp);
        else
            sb_error.SetErrorString("not connected");
    }
    else
        sb_error.SetErrorString("invalid platform");

  return sb_error;
}

SBError
SBPlatform::MakeDirectory (const char *path, uint32_t file_permissions)
{
    SBError sb_error;
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        sb_error.ref() = platform_sp->MakeDirectory(FileSpec{path, false}, file_permissions);
    }
    else
    {
        sb_error.SetErrorString("invalid platform");
    }
    return sb_error;
}

uint32_t
SBPlatform::GetFilePermissions (const char *path)
{
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        uint32_t file_permissions = 0;
        platform_sp->GetFilePermissions(FileSpec{path, false}, file_permissions);
        return file_permissions;
    }
    return 0;
    
}

SBError
SBPlatform::SetFilePermissions (const char *path, uint32_t file_permissions)
{
    SBError sb_error;
    PlatformSP platform_sp(GetSP());
    if (platform_sp)
    {
        sb_error.ref() = platform_sp->SetFilePermissions(FileSpec{path, false}, file_permissions);
    }
    else
    {
        sb_error.SetErrorString("invalid platform");
    }
    return sb_error;
    
}

SBUnixSignals
SBPlatform::GetUnixSignals() const
{
    if (auto platform_sp = GetSP())
        return SBUnixSignals{platform_sp};

    return {};
}