summaryrefslogtreecommitdiffstats
path: root/scilab/modules/ast/src/cpp/ast/debuggervisitor.cpp
blob: 38438b132ae012952c5582f1c2f8e77587e17c1e (plain)
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
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

#include "debuggervisitor.hxx"
#include "debugmanager.hxx"
#include "printvisitor.hxx"
#include "execvisitor.hxx"
#include "threadId.hxx"
#include "macrofile.hxx"
#include "commentexp.hxx"
#include "UTF8.hxx"
#include "runner.hxx"

extern "C"
{
#include "filemanager_interface.h"
#include "FileExist.h"
}

static bool getMacroSourceFile(std::string* filename = nullptr);

namespace ast
{
void DebuggerVisitor::visit(const SeqExp  &e)
{
    std::list<Exp *>::const_iterator itExp;

    debugger::DebuggerManager* manager = debugger::DebuggerManager::getInstance();
    if(manager->isAborted())
    {
        // abort a running execution
        throw ast::InternalAbort();
    }

    for (const auto & exp : e.getExps())
    {
        if (exp->isCommentExp())
        {
            continue;
        }

        if (e.isBreakable())
        {
            exp->resetBreak();
            exp->setBreakable();
        }

        if (e.isContinuable())
        {
            exp->resetContinue();
            exp->setContinuable();
        }

        if (e.isReturnable())
        {
            exp->setReturnable();
        }

        //debugger check !
        int iBreakPoint = -1;
        if (ConfigVariable::getEnableDebug())
        {
            bool stopExecution = false;
            if (manager->isStepIn())
            {
                manager->resetStepIn();
                stopExecution = true;
                if(getMacroSourceFile() == false)
                {
                    stopExecution = false;
                    manager->setStepIn();
                }
            }
            else if (manager->isStepNext())
            {
                manager->resetStepNext();
                stopExecution = true;
                if(getMacroSourceFile() == false)
                {
                    stopExecution = false;
                    manager->setStepOut();
                }
            }
            else
            {
                const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
                //set information from debugger commands
                if (lWhereAmI.size() != 0 && manager->getBreakPointCount() != 0)
                {
                    debugger::Breakpoints& bps = manager->getAllBreakPoint();

                    int i = -1;
                    for (const auto & bp : bps)
                    {
                        ++i;
                        if (bp->isEnable() == false)
                        {
                            continue;
                        }

                        // look for a breakpoint on this line and update breakpoint information when possible
                        int iLine = exp->getLocation().first_line - ConfigVariable::getMacroFirstLines();
                        if (bp->hasMacro())
                        {
                            char* functionName = wide_string_to_UTF8(lWhereAmI.back().call->getName().data());
                            if (bp->getFunctioName().compare(functionName) == 0)
                            {
                                if (bp->getMacroLine() == 0)
                                {
                                    //first pass in macro.
                                    //update first line with real value
                                    bp->setMacroLine(iLine);
                                }

                                stopExecution = bp->getMacroLine() == iLine;
                            }

                            FREE(functionName);
                        }
                        else if (bp->hasFile())
                        {
                            if (bp->getFileLine() == exp->getLocation().first_line)
                            {
                                std::string pFileName;
                                bool hasSource = getMacroSourceFile(&pFileName);
                                if (hasSource && bp->getFileName() == pFileName)
                                {
                                    stopExecution = true;
                                    // set function information
                                    if (lWhereAmI.back().call->getFirstLine())
                                    {
                                        bp->setFunctionName(scilab::UTF8::toUTF8(lWhereAmI.back().call->getName()));
                                        bp->setMacroLine(iLine);
                                    }
                                }
                            }
                        }

                        if(stopExecution == false)
                        {
                            // no breakpoint for this line
                            continue;
                        }

                        // Set the begin of line if not yet done
                        if(bp->getBeginLine() == 0)
                        {
                            bp->setBeginLine(exp->getLocation().first_column);
                        }
                        // check if this exp is at the begin of the breakpoint line
                        else if(bp->getBeginLine() != exp->getLocation().first_column)
                        {
                            // stop only if we are at the line begins
                            stopExecution = false;
                            continue;
                        }

                        //check condition
                        if (bp->getConditionExp() != NULL)
                        {
                            //do not use debuggervisitor !
                            symbol::Context* pCtx = symbol::Context::getInstance();
                            try
                            {
                                ExecVisitor execCond;
                                //protect current env during condition execution
                                pCtx->scope_begin();
                                bp->getConditionExp()->accept(execCond);
                                types::InternalType* pIT = pCtx->getCurrentLevel(symbol::Symbol(L"ans"));
                                if (pIT == NULL)
                                {
                                    // no result ie: assignation
                                    char pcError[256];
                                    sprintf(pcError, _("Wrong breakpoint condition: A result expected.\n"));
                                    bp->setConditionError(pcError);
                                }
                                else if(pIT->isTrue() == false)
                                {
                                    // bool scalar false
                                    pCtx->scope_end();
                                    stopExecution = false;
                                    continue;
                                }

                                // condition is invalid or true
                            }
                            catch (ast::ScilabException& e)
                            {
                                //not work !
                                //invalid breakpoint
                                if(ConfigVariable::isError())
                                {
                                    bp->setConditionError(scilab::UTF8::toUTF8(ConfigVariable::getLastErrorMessage()));
                                    ConfigVariable::clearLastError();
                                    ConfigVariable::resetError();
                                }
                                else
                                {
                                    bp->setConditionError(scilab::UTF8::toUTF8(e.GetErrorMessage()));
                                }
                            }

                            pCtx->scope_end();
                        }

                        //we have a breakpoint !
                        //stop execution and wait signal from debugger to restart
                        iBreakPoint = i;

                        //only one breakpoint can be "call" on same exp
                        break;
                    }
                }
            }

            if(stopExecution)
            {
                manager->stop(exp, iBreakPoint);
                if (manager->isAborted())
                {
                    throw ast::InternalAbort();
                }
            }
        }

        // interrupt me to execute a prioritary command
        while (StaticRunner_isRunnerAvailable() == 1 && StaticRunner_isInterruptibleCommand() == 1)
        {
            StaticRunner_launch();
        }

        //copy from runvisitor::seqexp
        try
        {
            //reset default values
            setResult(NULL);
            int iExpectedSize = getExpectedSize();
            setExpectedSize(-1);
            exp->accept(*this);
            setExpectedSize(iExpectedSize);
            types::InternalType * pIT = getResult();

            // In case of exec file, set the file name in the Macro to store where it is defined.
            int iFileID = ConfigVariable::getExecutedFileID();
            if (iFileID && exp->isFunctionDec())
            {
                types::InternalType* pITMacro = symbol::Context::getInstance()->get(exp->getAs<ast::FunctionDec>()->getSymbol());
                if (pITMacro)
                {
                    types::Macro* pMacro = pITMacro->getAs<types::Macro>();
                    const wchar_t* filename = getfile_filename(iFileID);
                    // scilab.quit is not open with mopen
                    // in this case filename is NULL because FileManager have not been filled.
                    if (filename)
                    {
                        pMacro->setFileName(filename);
                    }
                }
            }

            if (pIT != NULL)
            {
                bool bImplicitCall = false;
                if (pIT->isCallable()) //to manage call without ()
                {
                    types::Callable *pCall = pIT->getAs<types::Callable>();
                    types::typed_list out;
                    types::typed_list in;
                    types::optional_list opt;

                    try
                    {
                        //in this case of calling, we can return only one value
                        int iSaveExpectedSize = getExpectedSize();
                        setExpectedSize(1);

                        pCall->invoke(in, opt, getExpectedSize(), out, *exp);
                        setExpectedSize(iSaveExpectedSize);

                        if (out.size() == 0)
                        {
                            setResult(NULL);
                        }
                        else
                        {
                            setResult(out[0]);
                        }

                        bImplicitCall = true;
                    }
                    catch (const InternalError& ie)
                    {
                        if (ConfigVariable::getLastErrorFunction() == L"")
                        {
                            ConfigVariable::setLastErrorFunction(pCall->getName());
                            ConfigVariable::setLastErrorLine(e.getLocation().first_line);
                        }

                        throw ie;
                    }
                }

                //don't output Simplevar and empty result
                if (getResult() != NULL && (!exp->isSimpleVar() || bImplicitCall))
                {
                    //symbol::Context::getInstance()->put(symbol::Symbol(L"ans"), *execMe.getResult());
                    types::InternalType* pITAns = getResult();
                    symbol::Context::getInstance()->put(m_pAns, pITAns);
                    if (exp->isVerbose() && ConfigVariable::isPrintOutput())
                    {
                        //TODO manage multiple returns
                        scilabWriteW(L" ans  =\n\n");
                        std::wostringstream ostrName;
                        ostrName << L"ans";
                        types::VariableToString(pITAns, ostrName.str().c_str());
                    }
                }

                pIT->killMe();
            }

            if ((&e)->isBreakable() && exp->isBreak())
            {
                const_cast<SeqExp *>(&e)->setBreak();
                exp->resetBreak();
                break;
            }

            if ((&e)->isContinuable() && exp->isContinue())
            {
                const_cast<SeqExp *>(&e)->setContinue();
                exp->resetContinue();
                break;
            }

            if ((&e)->isReturnable() && exp->isReturn())
            {
                const_cast<SeqExp *>(&e)->setReturn();
                exp->resetReturn();
                break;
            }

            // Stop execution at the end of the seqexp of the caller
            // Do it at the end of the seqexp will make the debugger stop
            // even if the caller is at the last line
            // ie: the caller is followed by endfunction
            if(manager->isStepOut())
            {
                manager->resetStepOut();
                if(getMacroSourceFile() == false)
                {
                    // no sources
                    manager->setStepOut();
                }
                else
                {
                    manager->stop(exp, iBreakPoint);
                }

                if (manager->isAborted())
                {
                    throw ast::InternalAbort();
                }
            }

        }
        catch (const InternalError& ie)
        {
            ConfigVariable::fillWhereError(ie.GetErrorLocation().first_line);

            const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();

            //where can be empty on last stepout, on first calling expression
            if (lWhereAmI.size())
            {
                const std::wstring* filename = lWhereAmI.back().m_file_name;

                if (filename == nullptr)
                {
                    //error in a console script
                    std::wstring functionName = lWhereAmI.back().call->getName();
                    manager->errorInScript(functionName, exp);
                }
                else
                {
                    manager->errorInFile(*filename, exp);
                }

                // Debugger just restart after been stopped on an error.
                if (manager->isAborted())
                {
                    throw ast::InternalAbort();
                }
            }

            throw ie;
        }

        // If something other than NULL is given to setResult, then that would imply
        // to make a cleanup in visit(ForExp) for example (e.getBody().accept(*this);)
        setResult(NULL);

    }

    if (e.getParent() == NULL && e.getExecFrom() == SeqExp::SCRIPT && (manager->isStepNext() || manager->isStepIn()))
    {
        const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
        if (lWhereAmI.size())
        {
            std::wstring functionName = lWhereAmI.back().call->getName();
            types::InternalType* pIT = symbol::Context::getInstance()->get(symbol::Symbol(functionName));
            if (pIT && (pIT->isMacro() || pIT->isMacroFile()))
            {
                types::Macro* m = nullptr;
                if (pIT->isMacroFile())
                {
                    types::MacroFile* mf = pIT->getAs<types::MacroFile>();
                    m = mf->getMacro();
                }
                else
                {
                    m = pIT->getAs<types::Macro>();
                }

                //create a fake exp to represente end/enfunction

                //will be deleted by CommentExp
                std::wstring* comment = new std::wstring(L"end of function");
                Location loc(m->getLastLine(), m->getLastLine(), 0, 0);
                CommentExp fakeExp(loc, comment);
                manager->stop(&fakeExp, -1);

                if (manager->isAborted())
                {
                    throw ast::InternalAbort();
                }

                //transform stepnext after endfunction as a stepout to show line marker on current statement
                if (manager->isStepNext())
                {
                    manager->resetStepNext();
                    manager->setStepOut();
                }
                else if (manager->isStepIn())
                {
                    manager->resetStepIn();
                    manager->setStepOut();
                }
            }
        }
    }
}
}

// return false if a file .sci of a file .bin doesn't exists
// return true for others files or existing .sci
bool getMacroSourceFile(std::string* filename)
{
    const std::vector<ConfigVariable::WhereEntry>& lWhereAmI = ConfigVariable::getWhere();
    // "Where" can be empty at the end of script execution
    // this function is called when the script ends after a step out
    if(lWhereAmI.empty())
    {
        return false;
    }

    std::string file = scilab::UTF8::toUTF8(*lWhereAmI.back().m_file_name);
    if (file.rfind(".bin") != std::string::npos)
    {
        file.replace(file.size() - 4, 4, ".sci");
        // stop on bp only if the file exist
        if (!FileExist(file.data()))
        {
            return false;
        }
    }

    if(filename != nullptr)
    {
        filename->assign(file);
    }

    return true;
}