Repository navigation
Expand file tree
/
Copy pathbreak_point.cpp
More file actions
1115 lines (1031 loc) · 34.2 KB
/
Copy pathbreak_point.cpp
File metadata and controls
1115 lines (1031 loc) · 34.2 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
// force tests to run
#include "hphp/runtime/debugger/break_point.h"
#include "hphp/runtime/debugger/debugger.h"
#include "hphp/runtime/debugger/debugger_proxy.h"
#include "hphp/runtime/debugger/debugger_thrift_buffer.h"
#include "hphp/runtime/base/preg.h"
#include "hphp/runtime/base/execution-context.h"
#include "hphp/runtime/base/class-info.h"
#include "hphp/runtime/base/stat-cache.h"
#include "hphp/runtime/vm/jit/translator-inline.h"
#include "hphp/runtime/base/comparisons.h"
namespace HPHP { namespace Eval {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(debugger);
int InterruptSite::getFileLen() const {
TRACE(2, "InterruptSite::getFileLen\n");
if (m_file.empty()) {
getFile();
}
return m_file.size();
}
std::string InterruptSite::desc() const {
TRACE(2, "InterruptSite::desc\n");
string ret;
if (m_error.isNull()) {
ret = "Break";
} else if (m_error.isObject()) {
ret = "Exception thrown";
} else {
ret = "Error occurred";
}
const char *cls = getClass();
const char *func = getFunction();
if (func && *func) {
ret += " at ";
if (cls && *cls) {
ret += cls;
ret += "::";
}
ret += func;
ret += "()";
}
string file = getFile();
int line0 = getLine0();
if (line0) {
ret += " on line " + lexical_cast<string>(line0);
if (!file.empty()) {
ret += " of " + file;
}
} else if (!file.empty()) {
ret += " in " + file;
}
return ret;
}
InterruptSite::InterruptSite(bool hardBreakPoint, CVarRef error)
: m_error(error), m_activationRecord(nullptr),
m_callingSite(nullptr), m_class(nullptr),
m_file((StringData*)nullptr),
m_line0(0), m_char0(0), m_line1(0), m_char1(0),
m_offset(InvalidAbsoluteOffset), m_unit(nullptr), m_valid(false),
m_funcEntry(false) {
TRACE(2, "InterruptSite::InterruptSite\n");
#define bail_on(c) if (c) { return; }
VMExecutionContext* context = g_vmContext;
ActRec *fp = context->getFP();
bail_on(!fp);
if (hardBreakPoint && fp->skipFrame()) {
// for hard breakpoint, the fp is for an extension function,
// so we need to construct the site on the caller
fp = context->getPrevVMState(fp, &m_offset);
} else {
const uchar *pc = context->getPC();
bail_on(!fp->m_func);
m_unit = fp->m_func->unit();
bail_on(!m_unit);
m_offset = m_unit->offsetOf(pc);
if (m_offset == fp->m_func->base()) {
m_funcEntry = true;
}
}
#undef bail_on
this->Initialize(fp);
}
// Only used to look for callers by function name. No need to
// to retrieve source line information for this kind of site.
InterruptSite::InterruptSite(ActRec *fp, Offset offset, CVarRef error)
: m_error(error), m_activationRecord(nullptr),
m_callingSite(nullptr), m_class(nullptr),
m_file((StringData*)nullptr),
m_line0(0), m_char0(0), m_line1(0), m_char1(0),
m_offset(offset), m_unit(nullptr), m_valid(false),
m_funcEntry(false) {
TRACE(2, "InterruptSite::InterruptSite(fp)\n");
this->Initialize(fp);
}
void InterruptSite::Initialize(ActRec *fp) {
TRACE(2, "InterruptSite::Initialize\n");
#define bail_on(c) if (c) { return; }
assert(fp);
m_activationRecord = fp;
bail_on(!fp->m_func);
m_unit = fp->m_func->unit();
bail_on(!m_unit);
m_file = m_unit->filepath()->data();
if (m_unit->getSourceLoc(m_offset, m_sourceLoc)) {
m_line0 = m_sourceLoc.line0;
m_char0 = m_sourceLoc.char0;
m_line1 = m_sourceLoc.line1;
m_char1 = m_sourceLoc.char1;
}
m_function = fp->m_func->name()->data();
if (fp->m_func->isGenerator()) {
// Strip off "$continuation" to get the original function name
assert(m_function.compare(m_function.length() - 13,
string::npos, "$continuation") == 0);
m_function.resize(m_function.length() - 13);
}
if (fp->m_func->preClass()) {
m_class = fp->m_func->preClass()->name()->data();
} else {
m_class = "";
}
#undef bail_on
m_valid = true;
}
// Returns an Interrupt site for the function that called the
// function that contains this site. This site retains ownership
// of the returned site and it will be deleted when this site
// is destructed, so do not hold on to the returned object for
// longer than there is a guarantee that this site will be alive.
const InterruptSite *InterruptSite::getCallingSite() const {
if (m_callingSite != nullptr) return m_callingSite.get();
VMExecutionContext* context = g_vmContext;
Offset parentOffset;
auto parentFp = context->getPrevVMState(m_activationRecord, &parentOffset);
if (parentFp == nullptr) return nullptr;
m_callingSite.reset(new InterruptSite(parentFp, parentOffset, m_error));
return m_callingSite.get();
}
///////////////////////////////////////////////////////////////////////////////
const char *BreakPointInfo::ErrorClassName = "@";
const char *BreakPointInfo::GetInterruptName(InterruptType interrupt) {
TRACE(2, "BreakPointInfo::GetInterruptName\n");
switch (interrupt) {
case RequestStarted: return "start of request";
case RequestEnded: return "end of request or start of psp";
case PSPEnded: return "end of psp";
default:
assert(false);
break;
}
return nullptr;
}
BreakPointInfo::BreakPointInfo(bool regex, State state,
const std::string &file, int line)
: m_index(0), m_state(state), m_valid(true),
m_interruptType(BreakPointReached),
m_file(file), m_line1(line), m_line2(line),
m_regex(regex), m_check(false) {
TRACE(2, "BreakPointInfo::BreakPointInfo..const std::string &file, int)\n");
createIndex();
}
BreakPointInfo::BreakPointInfo(bool regex, State state,
InterruptType interrupt,
const std::string &url)
: m_index(0), m_state(state), m_valid(true), m_interruptType(interrupt),
m_line1(0), m_line2(0), m_url(url),
m_regex(regex), m_check(false) {
TRACE(2, "BreakPointInfo::BreakPointInfo..const std::string &url)\n");
createIndex();
}
BreakPointInfo::BreakPointInfo(bool regex, State state,
InterruptType interrupt,
const std::string &exp,
const std::string &file)
: m_index(0), m_state(state), m_valid(true), m_interruptType(interrupt),
m_line1(0), m_line2(0),
m_regex(regex), m_check(false) {
TRACE(2, "BreakPointInfo::BreakPointInfo..const std::string &file)\n");
assert(m_interruptType != ExceptionHandler); // Server-side only.
if (m_interruptType == ExceptionThrown) {
parseExceptionThrown(exp);
} else {
parseBreakPointReached(exp, file);
}
createIndex();
}
static int s_max_breakpoint_index = 0;
void BreakPointInfo::createIndex() {
TRACE(2, "BreakPointInfo::createIndex\n");
m_index = ++s_max_breakpoint_index;
}
BreakPointInfo::~BreakPointInfo() {
TRACE(2, "BreakPointInfo::~BreakPointInfo\n");
if (m_index && m_index == s_max_breakpoint_index) {
--s_max_breakpoint_index;
}
}
void BreakPointInfo::sendImpl(int version, DebuggerThriftBuffer &thrift) {
TRACE(2, "BreakPointInfo::sendImpl\n");
thrift.write((int8_t)m_state);
if (version >= 1) thrift.write((int8_t)m_bindState);
thrift.write((int8_t)m_interruptType);
thrift.write(m_file);
thrift.write(m_line1);
thrift.write(m_line2);
thrift.write(m_namespace);
thrift.write(m_class);
thrift.write(m_funcs);
thrift.write(m_url);
thrift.write(m_regex);
thrift.write(m_check);
thrift.write(m_clause);
thrift.write(m_output);
thrift.write(m_exceptionClass);
thrift.write(m_exceptionObject);
}
void BreakPointInfo::recvImpl(int version, DebuggerThriftBuffer &thrift) {
TRACE(2, "BreakPointInfo::recvImpl\n");
int8_t tmp;
thrift.read(tmp); m_state = (State)tmp;
if (version >= 1) {
thrift.read(tmp); m_bindState = (BindState)tmp;
}
thrift.read(tmp); m_interruptType = (InterruptType)tmp;
thrift.read(m_file);
thrift.read(m_line1);
thrift.read(m_line2);
thrift.read(m_namespace);
thrift.read(m_class);
thrift.read(m_funcs);
thrift.read(m_url);
thrift.read(m_regex);
thrift.read(m_check);
thrift.read(m_clause);
thrift.read(m_output);
thrift.read(m_exceptionClass);
thrift.read(m_exceptionObject);
}
void BreakPointInfo::setClause(const std::string &clause, bool check) {
TRACE(2, "BreakPointInfo::setClause\n");
m_clause = clause;
m_check = check;
}
void BreakPointInfo::transferStack(BreakPointInfoPtr bpi) {
if (bpi->m_stack.empty()) return;
m_stack.splice(m_stack.begin(), bpi->m_stack);
}
// Disables the breakpoint at the given stack level.
// Following this call, BreakPointInfo::breakable will return false until
// a subsequent call to BreakPointInfo::setBreakable with a lower or equal
// stack level.
void BreakPointInfo::unsetBreakable(int stackDepth, Offset offset) {
TRACE(2, "BreakPointInfo::unsetBreakable\n");
if (m_stack.empty() || m_stack.back().first < stackDepth) {
m_stack.push_back(std::make_pair(stackDepth, offset));
}
}
// Enables the breakpoint at the given stack level.
// Following this call, BreakPointInfo::breakable will return true until
// a subsequent call to BreakPointInfo::unsetBreakable with the same or
// higher stack level.
void BreakPointInfo::setBreakable(int stackDepth) {
TRACE(2, "BreakPointInfo::setBreakable\n");
while (!m_stack.empty() && m_stack.back().first >= stackDepth) {
m_stack.pop_back();
}
}
// Returns true if this breakpoint is enabled at the given stack level.
bool BreakPointInfo::breakable(int stackDepth, Offset offset) const {
TRACE(2, "BreakPointInfo::breakable\n");
if (!m_stack.empty() && m_stack.back().first >= stackDepth) {
if (m_stack.back().first == stackDepth && m_stack.back().second >= offset) {
// We assume that the only way to ask this question for the same
// stack level and offset, is for the execution to have come back
// here after executing the operation at offset, but without
// executing any other operations in the interpreter.
return true;
}
return false;
} else {
return true;
}
}
void BreakPointInfo::toggle() {
TRACE(2, "BreakPointInfo::toggle\n");
switch (m_state) {
case Always: setState(Once); break;
case Once: setState(Disabled); break;
case Disabled: setState(Always); break;
default:
assert(false);
break;
}
}
bool BreakPointInfo::valid() {
TRACE(2, "BreakPointInfo::valid\n");
if (m_valid) {
switch (m_interruptType) {
case BreakPointReached:
if (!getFuncName().empty()) {
if (!m_file.empty() || m_line1 != 0) {
return false;
}
} else {
if (m_file.empty() || m_line1 == 0 || m_line2 != m_line1) {
return false;
}
}
if (m_regex) {
return false;
}
return (m_line1 && m_line2) || !m_file.empty() || !m_funcs.empty();
case ExceptionThrown:
return !m_class.empty();
case RequestStarted:
case RequestEnded:
case PSPEnded:
return true;
default:
break;
}
}
return false;
}
bool BreakPointInfo::same(BreakPointInfoPtr bpi) {
TRACE(2, "BreakPointInfo::same\n");
return desc() == bpi->desc();
}
// Checks if the interrupt type and site matches this breakpoint.
// Does not run any code.
bool BreakPointInfo::match(DebuggerProxy &proxy, InterruptType interrupt,
InterruptSite &site) {
return match(proxy, interrupt, site, false);
}
// Checks if the interrupt type and site matches this breakpoint.
// Evaluates the breakpoint's conditional clause if present.
// This can cause side effects.
bool BreakPointInfo::cmatch(DebuggerProxy &proxy, InterruptType interrupt,
InterruptSite &site) {
return match(proxy, interrupt, site, true);
}
bool BreakPointInfo::match(DebuggerProxy &proxy, InterruptType interrupt,
InterruptSite &site, bool evalClause) {
TRACE(2, "BreakPointInfo::match\n");
if (m_interruptType == interrupt) {
switch (interrupt) {
case RequestStarted:
case RequestEnded:
case PSPEnded:
return
checkUrl(site.url());
case ExceptionThrown:
return
checkExceptionOrError(site.getError()) &&
checkUrl(site.url()) && (!evalClause || checkClause(proxy));
case BreakPointReached:
{
bool match =
Match(site.getFile(), site.getFileLen(), m_file, m_regex, false) &&
checkLines(site.getLine0()) && checkStack(site) &&
checkUrl(site.url()) && (!evalClause || checkClause(proxy));
if (!getFuncName().empty()) {
// function entry breakpoint
match = match && site.funcEntry();
}
return match;
}
default:
break;
}
}
return false;
}
std::string BreakPointInfo::state(bool padding) const {
TRACE(2, "BreakPointInfo::state\n");
switch (m_state) {
case Always: return padding ? "ALWAYS " : "ALWAYS" ;
case Once: return padding ? "ONCE " : "ONCE" ;
case Disabled: return padding ? "DISABLED" : "DISABLED";
default:
assert(false);
break;
}
return "";
}
std::string BreakPointInfo::regex(const std::string &name) const {
TRACE(7, "BreakPointInfo::regex\n");
if (m_regex) {
return "regex{" + name + "}";
}
return name;
}
std::string BreakPointInfo::getNamespace() const {
TRACE(7, "BreakPointInfo::getNamespace\n");
if (!m_funcs.empty()) {
return m_funcs[0]->m_namespace;
}
return "";
}
std::string BreakPointInfo::getClass() const {
TRACE(7, "BreakPointInfo::getClass\n");
if (!m_funcs.empty()) {
return m_funcs[0]->m_class;
}
return "";
}
std::string BreakPointInfo::getFunction() const {
TRACE(7, "BreakPointInfo::getFunction\n");
if (!m_funcs.empty()) {
return m_funcs[0]->m_function;
}
return "";
}
std::string BreakPointInfo::getFuncName() const {
TRACE(7, "BreakPointInfo::getFuncName\n");
if (!m_funcs.empty()) {
return m_funcs[0]->getName();
}
return "";
}
std::string BreakPointInfo::site() const {
TRACE(7, "BreakPointInfo::site\n");
string ret;
string preposition = "at ";
if (!m_funcs.empty()) {
ret = m_funcs[0]->site(preposition);
for (unsigned int i = 1; i < m_funcs.size(); i++) {
ret += " called by ";
string tmp;
ret += m_funcs[i]->site(tmp);
}
}
if (!m_file.empty() || m_line1) {
if (!ret.empty()) {
ret += " ";
} else {
preposition = "";
}
if (m_line1) {
ret += "on line " + lexical_cast<string>(m_line1);
if (!m_file.empty()) {
ret += " of " + m_file;
}
} else {
ret += "in " + m_file;
}
}
return preposition + ret;
}
std::string BreakPointInfo::descBreakPointReached() const {
TRACE(2, "BreakPointInfo::descBreakPointReached\n");
string ret;
for (unsigned int i = 0; i < m_funcs.size(); i++) {
ret += (i == 0 ? "upon entering " : " called by ");
ret += m_funcs[i]->desc(this);
}
if (!m_file.empty() || m_line1 || m_line2) {
if (!ret.empty()) {
ret += " ";
}
if (m_line1 || m_line2) {
if (m_line1 == m_line2) {
ret += "on line " + lexical_cast<string>(m_line1);
} else if (m_line2 == -1) {
ret += "between line " + lexical_cast<string>(m_line1) + " and end";
} else {
ret += "between line " + lexical_cast<string>(m_line1) +
" and line " + lexical_cast<string>(m_line2);
}
if (!m_file.empty()) {
ret += " of " + regex(m_file);
} else {
ret += " of any file";
}
} else {
ret += "on any lines in " + regex(m_file);
}
}
return ret;
}
std::string BreakPointInfo::descExceptionThrown() const {
TRACE(2, "BreakPointInfo::descExceptionThrown\n");
string ret;
if (!m_namespace.empty() || !m_class.empty()) {
if (m_class == ErrorClassName) {
ret = "right after an error";
} else {
ret = "right before throwing ";
if (!m_class.empty()) {
if (!m_namespace.empty()) {
ret += regex(m_namespace) + "::";
}
ret += regex(m_class);
} else {
ret += "any exceptions in namespace " + regex(m_namespace);
}
}
}
return ret;
}
std::string BreakPointInfo::desc() const {
TRACE(2, "BreakPointInfo::desc\n");
string ret;
switch (m_interruptType) {
case BreakPointReached:
ret = descBreakPointReached();
break;
case ExceptionThrown:
ret = descExceptionThrown();
break;
default:
ret = GetInterruptName((InterruptType)m_interruptType);
break;
}
if (!m_url.empty()) {
ret += " when request is " + regex(m_url);
}
if (!m_clause.empty()) {
if (m_check) {
ret += " if " + m_clause;
} else {
ret += " && " + m_clause;
}
}
return ret;
}
void mangleXhpName(const std::string &source, std::string &target) {
auto oldLen = source.length();
size_t newLen = 0;
size_t index = 0;
for (; index < oldLen; index++) {
auto ch = source[index];
if (ch != ':' && ch != '-') continue;
newLen = 4+index;
break;
}
if (newLen == 0) {
target = source;
return;
}
for (; index < oldLen; index++) {
if (source[index] == ':') newLen += 2; else newLen +=1;
}
target.clear();
target.reserve(newLen);
target.append("xhp_");
for (index = 0; index < oldLen; index++) {
auto ch = source[index];
if (ch == '-') {
target.push_back('_');
} else if (ch == ':') {
if (index > 0) {
target.push_back('_');
target.push_back('_');
}
} else {
target.push_back(ch);
}
}
}
int32_t scanName(const std::string &str, int32_t offset) {
auto len = str.length();
assert(0 <= offset && offset < len);
while (offset < len) {
char ch = str[offset];
if (ch == ':' || ch == '\\' || ch == ',' || ch == '(' || ch == '=' ||
ch == '@') {
if (offset+1 >= len) return offset;
char ch1 = str[offset+1];
if (ch == ':') {
if (ch1 == ':' || ('0' <= ch1 && ch1 <= '9')) return offset;
} else if (ch == '(') {
if (ch1 == ')') return offset;
} else if (ch == '=') {
if (ch1 == '>') return offset;
} else {
assert (ch == '\\' || ch == ',' || ch == '@');
return offset;
}
}
offset++;
}
return offset;
}
int32_t scanNumber(const std::string &str, int32_t offset, int32_t& value) {
value = 0;
auto len = str.length();
assert(0 <= offset && offset < len);
while (offset < len) {
char ch = str[offset];
if (ch < '0' || ch > '9') return offset;
value = value*10 + (ch - '0');
offset++;
}
return offset;
}
int32_t BreakPointInfo::parseFileLocation(const std::string &str,
int32_t offset) {
auto len = str.length();
assert(0 <= offset && offset < len);
auto offset1 = scanNumber(str, offset, m_line1);
if (offset1 == offset) return offset; //Did not find a number
m_line2 = m_line1; //so that we always have a range
if (offset1 >= len) return len; //Nothing follows the number
auto ch = str[offset1];
if (ch == '-') {
if (offset1+1 >= len) return offset; //Invalid file location
auto offset2 = scanNumber(str, offset1+1, m_line2);
if (offset1+1 == offset2) return offset; //Invalid file location
return offset2;
}
return offset1;
}
//( letter | underscore ) #( letter | digit | underscore | extended_ascii ),
//extended_ascii::=char_nbr(127)..char_nbr(255),
static bool isValidIdentifier(const std::string &str) {
auto len = str.length();
for (int32_t index = 0; index < len; index++) {
char ch = str[index];
if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '_') {
continue;
}
if (index == 0) return false;
if (('0' <= ch && ch <= '9') || ch >= 127) {
continue;
}
return false;
}
return true;
}
/* The parser accepts the following syntax, which harks back to pre VM days
(all components are optional, as long as there is at least one component):
{file location},{call}=>{call}()@{url}
{call}=>{call}(),{file location}@{url}
file location: {file}:{line1}-{line2}
call: \{namespace}\{cls}::{func}
Currently semantic checks will disallow expressions that specify
both file locations and calls.
*/
void BreakPointInfo::parseBreakPointReached(const std::string &exp,
const std::string &file) {
TRACE(2, "BreakPointInfo::parseBreakPointReached\n");
string name;
auto len = exp.length();
auto offset0 = 0;
//Look for leading number by itself
auto offset1 = scanNumber(exp, offset0, m_line1);
if (offset1 == len) {
m_line2 = m_line1;
m_file = file;
return;
}
// Skip over a leading backslash
if (len > 0 && exp[0] == '\\') offset0++;
offset1 = scanName(exp, offset0);
// check that exp starts with a file or method name
if (offset1 == offset0) goto returnInvalid;
name = exp.substr(offset0, offset1-offset0);
if (offset0 == 0) {
// parse {file location} if appropriate
if (offset1 < len && exp[offset1] == ',') {
m_file = name;
name.clear();
offset1 += 1;
} else if (offset1 < len-1 && exp[offset1] == ':' &&
exp[offset1+1] != ':') {
m_file = name;
name.clear();
offset1 += 1;
auto offset2 = parseFileLocation(exp, offset1);
// check for {file}:{something that is not a number}
if (offset2 == offset1) goto returnInvalid;
offset1 = offset2;
if (offset1 >= len) return; // file location without anything else
if (exp[offset1] == '@') goto parseUrl; // file location followed by url
// check for {file}{ something other than @ or :}
if (exp[offset1] != ',') goto returnInvalid;
offset1 += 1;
}
}
// parse {func}() or {func}=>{func}() or {func}=>{func}=>{func}() and so on
while (true) {
if (name.empty()) {
if (len > offset1 && exp[offset1] == '\\') offset1++;
auto offset2 = scanName(exp, offset1);
// check for {something other than a name}
if (offset2 == offset1) goto returnInvalid;
name = exp.substr(offset1, offset2-offset1);
offset1 = offset2;
}
while (offset1 < len && exp[offset1] == '\\') {
if (!m_namespace.empty()) m_namespace += "\\";
if (!isValidIdentifier(name)) goto returnInvalid;
m_namespace += name;
offset1 += 1;
auto offset2 = scanName(exp, offset1);
// check for {namespace}\{something that is not a name}
if (offset2 == offset1) goto returnInvalid;
name = exp.substr(offset1, offset2-offset1);
offset1 = offset2;
}
if (offset1 < len-1 && exp[offset1] == ':' && exp[offset1+1] == ':') {
m_class = name;
offset1 += 2;
auto offset2 = scanName(exp, offset1);
// check for {namespace}\{class}::{something that is not a name}
if (offset2 == offset1) goto returnInvalid;
name = exp.substr(offset1, offset2-offset1);
offset1 = offset2;
}
// Now we have a namespace, class and func name.
// The namespace only or the namespace and class might be empty.
DFunctionInfoPtr pfunc(new DFunctionInfo());
if (m_class.empty()) {
if (!isValidIdentifier(name)) goto returnInvalid;
if (m_namespace.empty()) {
pfunc->m_function = name;
} else {
// Yes this does seem beyond strange, but that is what the PHP parser
// does when it sees a function declared inside a namespace, so we
// too have to pretend there is no namespace here. At some point
// the parser hack may have to go away. At that stage, this code
// will have to change, as well as other parts of the debugger.
pfunc->m_function = m_namespace + "\\" + name;
}
} else {
mangleXhpName(m_class, pfunc->m_class);
if (!isValidIdentifier(pfunc->m_class)) goto returnInvalid;
if (!m_namespace.empty()) {
// Emulate parser hack. See longer comment above.
pfunc->m_class = m_namespace + "\\" + pfunc->m_class;
}
if (!isValidIdentifier(name)) goto returnInvalid;
pfunc->m_function = name;
}
m_funcs.insert(m_funcs.begin(), pfunc);
m_namespace.clear();
m_class.clear();
name.clear();
// If we are now at () we skip over it and terminate the loop
if (offset1 < len && exp[offset1] == '(') {
// check for {func}{(}{not )}
if (offset1+1 >= len || exp[offset1+1] != ')') goto returnInvalid;
offset1 += 2;
break; // parsed the last (perhaps only) call in a function call chain
}
// If we are now at => we need to carry on with the loop
if (offset1 < len-1 && exp[offset1] == '=' && exp[offset1+1] == '>') {
offset1 += 2;
continue;
}
goto returnInvalid; // {func calls}{not () or =>}
}
if (m_file.empty()) {
if (offset1 < len && exp[offset1] == ',') {
auto offset2 = scanName(exp, ++offset1);
// check for {func calls}:{not a filename}
if (offset2 == offset1) goto returnInvalid;
m_file = exp.substr(offset1, offset2-offset1);
offset1 = offset2;
if (offset1 < len && exp[offset1] == ':') {
offset2 = parseFileLocation(exp, offset1+1);
// check for {file}:{something that is not a number}
if (offset2 == offset2+1) goto returnInvalid;
offset1 = offset2;
}
}
}
parseUrl:
if (offset1 < len-2 && exp[offset1] == '@') {
offset1++;
m_url = exp.substr(offset1, len-offset1);
} else {
// check for unparsed characters at end of exp
if (offset1 != len) goto returnInvalid;
}
return;
returnInvalid:
m_valid = false;
}
void BreakPointInfo::parseExceptionThrown(const std::string &exp) {
TRACE(2, "BreakPointInfo::parseExceptionThrown\n");
string name;
auto len = exp.length();
auto offset0 = 0;
// Skip over a leading backslash
if (len > 0 && exp[0] == '\\') offset0++;
auto offset1 = scanName(exp, offset0);
// check that exp starts with a name
if (offset1 == offset0) goto returnInvalid;
name = exp.substr(offset0, offset1-offset0);
if (name.empty()) {
if (len > offset1 && exp[offset1] == '\\') offset1++;
auto offset2 = scanName(exp, offset1);
// check for {something other than a name}
if (offset2 == offset1) goto returnInvalid;
name = exp.substr(offset1, offset2-offset1);
}
while (offset1 < len && exp[offset1] == '\\') {
if (!m_namespace.empty()) m_namespace += "\\";
m_namespace += name;
offset1 += 1;
auto offset2 = scanName(exp, offset1);
// check for {namespace}\{something that is not a name}
if (offset2 == offset1) goto returnInvalid;
name = exp.substr(offset1, offset2-offset1);
offset1 = offset2;
}
m_class = name;
// Now we have a namespace and class name.
// The namespace might be empty.
mangleXhpName(m_class, m_class);
if (m_class == "error") m_class = ErrorClassName;
if (!m_namespace.empty()) {
m_class = m_namespace + "\\" + m_class;
m_namespace.clear();
}
if (offset1 < len-2 && exp[offset1] == '@') {
offset1++;
m_url = exp.substr(offset1, len-offset1);
} else {
// check for unparsed characters at end of exp
if (offset1 != len) goto returnInvalid;
}
return;
returnInvalid:
m_valid = false;
}
bool BreakPointInfo::MatchFile(const char *haystack, int haystack_len,
const std::string &needle) {
TRACE(2, "BreakPointInfo::MatchFile(const char *haystack\n");
int pos = haystack_len - needle.size();
if ((pos == 0 || haystack[pos - 1] == '/') &&
strcasecmp(haystack + pos, needle.c_str()) == 0) {
return true;
}
if (strcasecmp(StatCache::realpath(needle.c_str()).c_str(), haystack)
== 0) {
return true;
}
return false;
}
// Returns true if file is a suffix path of fullPath
bool BreakPointInfo::MatchFile(const std::string& file,
const std::string& fullPath) {
TRACE(7, "BreakPointInfo::MatchFile(const std::string&\n");
if (file == fullPath) {
return true;
}
if (file.size() > 0 && file[0] != '/') {
auto pos = fullPath.rfind(file);
// check for match
if (pos == std::string::npos) return false;
// check if match is a suffix
if (pos + file.size() > fullPath.size()) return false;
// check if suffix is a sub path
if (pos == 0 || fullPath[pos-1] != '/') return false;
return true;
}
// Perhaps file or fullPath is a symlink.
auto realFile = StatCache::realpath(file.c_str());
auto realFullPath = StatCache::realpath(fullPath.c_str());
if (realFile != file || realFullPath != fullPath) {
return MatchFile(realFile, realFullPath);
}
return false;
}
bool BreakPointInfo::MatchClass(const char *fcls, const std::string &bcls,
bool regex, const char *func) {
TRACE(2, "BreakPointInfo::MatchClass\n");
if (bcls.empty()) return true;
if (!fcls || !*fcls) return false;
if (regex || !func || !*func) {
return Match(fcls, 0, bcls, true, true);
}
String sdBClsName(bcls);
Class* clsB = Unit::lookupClass(sdBClsName.get());
if (!clsB) return false;
String sdFClsName(fcls, CopyString);
Class* clsF = Unit::lookupClass(sdFClsName.get());
if (clsB == clsF) return true;
String sdFuncName(func, CopyString);
Func* f = clsB->lookupMethod(sdFuncName.get());
return f && f->baseCls() == clsF;
}
bool BreakPointInfo::Match(const char *haystack, int haystack_len,
const std::string &needle, bool regex, bool exact) {
TRACE(2, "BreakPointInfo::Match\n");
if (needle.empty()) {
return true;
}
if (!haystack || !*haystack) {
return false;
}
if (!regex) {
if (exact) {
return strcasecmp(haystack, needle.c_str()) == 0;
}
return MatchFile(haystack, haystack_len, needle);
}
Variant matches;
Variant r = preg_match(String(needle.c_str(), needle.size(),
CopyString),
String(haystack, haystack_len, CopyString),
matches);
return HPHP::same(r, 1);
}
bool BreakPointInfo::checkExceptionOrError(CVarRef e) {