~ubuntu-branches/ubuntu/trusty/llvm-toolchain-snapshot/trusty-201310232150

« back to all changes in this revision

Viewing changes to lldb/source/Interpreter/ScriptInterpreterPython.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-27 15:01:57 UTC
  • mfrom: (0.10.1) (0.9.1) (0.8.1) (0.7.1) (0.6.1) (0.5.2)
  • Revision ID: package-import@ubuntu.com-20130527150157-tdkrsjpuvht7v0qx
Tags: 1:3.4~svn182733-1~exp1
* New snapshot release (3.4 release)
* Add a symlink of libLLVM-3.4.so.1 to usr/lib/llvm-3.4/lib/libLLVM-3.4.so
    to fix make the llvm-config-3.4 --libdir work (Closes: #708677)
  * Various packages rename to allow co installations:
    * libclang1 => libclang1-3.4
    * libclang1-dbg => libclang1-3.4-dbg
    * libclang-dev => libclang-3.4-dev
    * libclang-common-dev => libclang-common-3.4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
277
277
    if (script_interpreter->m_script_lang != eScriptLanguagePython)
278
278
        return 0;
279
279
    
280
 
    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
281
 
    
282
280
    switch (notification)
283
281
    {
284
282
        case eInputReaderActivate:
843
841
    if (script_interpreter->m_script_lang != eScriptLanguagePython)
844
842
        return 0;
845
843
    
846
 
    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
847
 
    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
848
 
    
849
844
    switch (notification)
850
845
    {
851
846
    case eInputReaderActivate:
852
847
        {
 
848
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
849
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
853
850
            if (!batch_mode)
854
851
            {
855
852
                out_stream->Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n");
1251
1248
 
1252
1249
static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.";
1253
1250
 
 
1251
static const char *g_bkpt_command_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.\n"
 
1252
                                                        "def function(frame,bp_loc,internal_dict):\n"
 
1253
                                                        "    \"\"\"frame: the SBFrame for the location at which you stopped\n"
 
1254
                                                        "       bp_loc: an SBBreakpointLocation for the breakpoint location information\n"
 
1255
                                                        "       internal_dict: an LLDB support object not to be used\"\"\"";
 
1256
 
1254
1257
size_t
1255
1258
ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback
1256
1259
(
1263
1266
{
1264
1267
    static StringList commands_in_progress;
1265
1268
    
1266
 
    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1267
 
    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1268
 
    
1269
1269
    switch (notification)
1270
1270
    {
1271
1271
    case eInputReaderActivate:
1272
1272
        {
 
1273
            
 
1274
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1275
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1273
1276
            commands_in_progress.Clear();
1274
1277
            if (!batch_mode)
1275
1278
            {
1276
 
                out_stream->Printf ("%s\n", g_reader_instructions);
 
1279
                out_stream->Printf ("%s\n", g_bkpt_command_reader_instructions);
1277
1280
                if (reader.GetPrompt())
1278
1281
                    out_stream->Printf ("%s", reader.GetPrompt());
1279
1282
                out_stream->Flush ();
1285
1288
        break;
1286
1289
 
1287
1290
    case eInputReaderReactivate:
1288
 
        if (reader.GetPrompt() && !batch_mode)
1289
1291
        {
1290
 
            out_stream->Printf ("%s", reader.GetPrompt());
1291
 
            out_stream->Flush ();
 
1292
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1293
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
 
1294
            if (reader.GetPrompt() && !batch_mode)
 
1295
            {
 
1296
                out_stream->Printf ("%s", reader.GetPrompt());
 
1297
                out_stream->Flush ();
 
1298
            }
1292
1299
        }
1293
1300
        break;
1294
1301
 
1297
1304
        
1298
1305
    case eInputReaderGotToken:
1299
1306
        {
 
1307
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1308
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1300
1309
            std::string temp_string (bytes, bytes_len);
1301
1310
            commands_in_progress.AppendString (temp_string.c_str());
1302
1311
            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1320
1329
 
1321
1330
    case eInputReaderDone:
1322
1331
        {
 
1332
            bool batch_mode = notification == eInputReaderDone ? 
 
1333
                reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode() :
 
1334
                true;
1323
1335
            BreakpointOptions *bp_options = (BreakpointOptions *)baton;
1324
1336
            std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1325
1337
            data_ap->user_source.AppendList (commands_in_progress);
1336
1348
                    }
1337
1349
                    else if (!batch_mode)
1338
1350
                    {
 
1351
                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1339
1352
                        out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1340
1353
                        out_stream->Flush();
1341
1354
                    }
1344
1357
                {
1345
1358
                            if (!batch_mode)
1346
1359
                    {
 
1360
                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1347
1361
                        out_stream->Printf ("Warning:  Unable to find script intepreter; no command attached to breakpoint.\n");
1348
1362
                        out_stream->Flush();
1349
1363
                    }
1369
1383
{
1370
1384
    static StringList commands_in_progress;
1371
1385
    
1372
 
    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1373
 
    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1374
 
    
1375
1386
    switch (notification)
1376
1387
    {
1377
1388
    case eInputReaderActivate:
1378
1389
        {
 
1390
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1391
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
 
1392
            
1379
1393
            commands_in_progress.Clear();
1380
1394
            if (!batch_mode)
1381
1395
            {
1391
1405
        break;
1392
1406
 
1393
1407
    case eInputReaderReactivate:
1394
 
        if (reader.GetPrompt() && !batch_mode)
1395
1408
        {
1396
 
            out_stream->Printf ("%s", reader.GetPrompt());
1397
 
            out_stream->Flush ();
 
1409
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1410
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
 
1411
            if (reader.GetPrompt() && !batch_mode)
 
1412
            {
 
1413
                out_stream->Printf ("%s", reader.GetPrompt());
 
1414
                out_stream->Flush ();
 
1415
            }
1398
1416
        }
1399
1417
        break;
1400
1418
 
1403
1421
        
1404
1422
    case eInputReaderGotToken:
1405
1423
        {
 
1424
            StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
 
1425
            bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1406
1426
            std::string temp_string (bytes, bytes_len);
1407
1427
            commands_in_progress.AppendString (temp_string.c_str());
1408
1428
            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1426
1446
 
1427
1447
    case eInputReaderDone:
1428
1448
        {
 
1449
            bool batch_mode = notification == eInputReaderDone ?
 
1450
                reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode() :
 
1451
                true;
1429
1452
            WatchpointOptions *wp_options = (WatchpointOptions *)baton;
1430
1453
            std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1431
1454
            data_ap->user_source.AppendList (commands_in_progress);
1442
1465
                    }
1443
1466
                    else if (!batch_mode)
1444
1467
                    {
 
1468
                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1445
1469
                        out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1446
1470
                        out_stream->Flush();
1447
1471
                    }
1450
1474
                {
1451
1475
                            if (!batch_mode)
1452
1476
                    {
 
1477
                        StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1453
1478
                        out_stream->Printf ("Warning:  Unable to find script intepreter; no command attached to breakpoint.\n");
1454
1479
                        out_stream->Flush();
1455
1480
                    }
1478
1503
                bp_options,                 // baton
1479
1504
                eInputReaderGranularityLine, // token size, for feeding data to callback function
1480
1505
                "DONE",                     // end token
1481
 
                "> ",                       // prompt
 
1506
                "    ",                     // prompt
1482
1507
                true);                      // echo input
1483
1508
    
1484
1509
        if (err.Success())
2574
2599
 
2575
2600
    {
2576
2601
        FileSpec target_file(pathname, true);
2577
 
        
2578
 
        // TODO: would we want to reject any other value?
2579
 
        if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
2580
 
            target_file.GetFileType() == FileSpec::eFileTypeUnknown)
2581
 
        {
2582
 
            error.SetErrorString("invalid pathname");
2583
 
            return false;
2584
 
        }
2585
 
        
2586
 
        const char* directory = target_file.GetDirectory().GetCString();
2587
2602
        std::string basename(target_file.GetFilename().GetCString());
 
2603
        
 
2604
        StreamString command_stream;
2588
2605
 
2589
2606
        // Before executing Pyton code, lock the GIL.
2590
2607
        Locker py_lock (this,
2591
2608
                        Locker::AcquireLock      | (init_session ? Locker::InitSession     : 0),
2592
2609
                        Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
2593
2610
        
2594
 
        // now make sure that Python has "directory" in the search path
2595
 
        StreamString command_stream;
2596
 
        command_stream.Printf("if not (sys.path.__contains__('%s')):\n    sys.path.insert(1,'%s');\n\n",
2597
 
                              directory,
2598
 
                              directory);
2599
 
        bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
2600
 
        if (!syspath_retval)
2601
 
        {
2602
 
            error.SetErrorString("Python sys.path handling failed");
 
2611
        if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
 
2612
            target_file.GetFileType() == FileSpec::eFileTypeUnknown)
 
2613
        {
 
2614
            // if not a valid file of any sort, check if it might be a filename still
 
2615
            // dot can't be used but / and \ can, and if either is found, reject
 
2616
            if (strchr(pathname,'\\') || strchr(pathname,'/'))
 
2617
            {
 
2618
                error.SetErrorString("invalid pathname");
 
2619
                return false;
 
2620
            }
 
2621
            basename = pathname; // not a filename, probably a package of some sort, let it go through
 
2622
        }
 
2623
        else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory ||
 
2624
                 target_file.GetFileType() == FileSpec::eFileTypeRegular ||
 
2625
                 target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink)
 
2626
        {
 
2627
            const char* directory = target_file.GetDirectory().GetCString();
 
2628
            
 
2629
            // now make sure that Python has "directory" in the search path
 
2630
            StreamString command_stream;
 
2631
            command_stream.Printf("if not (sys.path.__contains__('%s')):\n    sys.path.insert(1,'%s');\n\n",
 
2632
                                  directory,
 
2633
                                  directory);
 
2634
            bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
 
2635
            if (!syspath_retval)
 
2636
            {
 
2637
                error.SetErrorString("Python sys.path handling failed");
 
2638
                return false;
 
2639
            }
 
2640
            
 
2641
            // strip .py or .pyc extension
 
2642
            ConstString extension = target_file.GetFileNameExtension();
 
2643
            if (extension)
 
2644
            {
 
2645
                if (::strcmp(extension.GetCString(), "py") == 0)
 
2646
                    basename.resize(basename.length()-3);
 
2647
                else if(::strcmp(extension.GetCString(), "pyc") == 0)
 
2648
                    basename.resize(basename.length()-4);
 
2649
            }
 
2650
        }
 
2651
        else
 
2652
        {
 
2653
            error.SetErrorString("no known way to import this module specification");
2603
2654
            return false;
2604
2655
        }
2605
2656
        
2606
 
        // strip .py or .pyc extension
2607
 
        ConstString extension = target_file.GetFileNameExtension();
2608
 
        if (extension)
2609
 
        {
2610
 
            if (::strcmp(extension.GetCString(), "py") == 0)
2611
 
                basename.resize(basename.length()-3);
2612
 
            else if(::strcmp(extension.GetCString(), "pyc") == 0)
2613
 
                basename.resize(basename.length()-4);
2614
 
        }
2615
 
        
2616
2657
        // check if the module is already import-ed
2617
2658
        command_stream.Clear();
2618
2659
        command_stream.Printf("sys.getrefcount(%s)",basename.c_str());