forked from premake/premake-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobdebug.lua
1707 lines (1591 loc) · 67.2 KB
/
mobdebug.lua
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
--
-- MobDebug -- Lua remote debugger
-- Copyright 2011-15 Paul Kulchenko
-- Based on RemDebug 1.0 Copyright Kepler Project 2005
--
-- use loaded modules or load explicitly on those systems that require that
local require = require
local io = io or require "io"
local table = table or require "table"
local string = string or require "string"
local coroutine = coroutine or require "coroutine"
local debug = require "debug"
-- protect require "os" as it may fail on embedded systems without os module
local os = os or (function(module)
local ok, res = pcall(require, module)
return ok and res or nil
end)("os")
local mobdebug = {
_NAME = "mobdebug",
_VERSION = "0.702",
_COPYRIGHT = "Paul Kulchenko",
_DESCRIPTION = "Mobile Remote Debugger for the Lua programming language",
port = os and os.getenv and tonumber((os.getenv("MOBDEBUG_PORT"))) or 8172,
checkcount = 200,
yieldtimeout = 0.02, -- yield timeout (s)
connecttimeout = 2, -- connect timeout (s)
}
local HOOKMASK = "lcr"
local error = error
local getfenv = getfenv
local setfenv = setfenv
local loadstring = loadstring or load -- "load" replaced "loadstring" in Lua 5.2
local pairs = pairs
local setmetatable = setmetatable
local tonumber = tonumber
local unpack = table.unpack or unpack
local rawget = rawget
local gsub, sub, find = string.gsub, string.sub, string.find
-- if strict.lua is used, then need to avoid referencing some global
-- variables, as they can be undefined;
-- use rawget to avoid complaints from strict.lua at run-time.
-- it's safe to do the initialization here as all these variables
-- should get defined values (if any) before the debugging starts.
-- there is also global 'wx' variable, which is checked as part of
-- the debug loop as 'wx' can be loaded at any time during debugging.
local genv = _G or _ENV
local jit = rawget(genv, "jit")
local MOAICoroutine = rawget(genv, "MOAICoroutine")
-- ngx_lua debugging requires a special handling as its coroutine.*
-- methods use a different mechanism that doesn't allow resume calls
-- from debug hook handlers.
-- Instead, the "original" coroutine.* methods are used.
-- `rawget` needs to be used to protect against `strict` checks, but
-- ngx_lua hides those in a metatable, so need to use that.
local metagindex = getmetatable(genv) and getmetatable(genv).__index
local ngx = type(metagindex) == "table" and metagindex.rawget and metagindex:rawget("ngx") or nil
local corocreate = ngx and coroutine._create or coroutine.create
local cororesume = ngx and coroutine._resume or coroutine.resume
local coroyield = ngx and coroutine._yield or coroutine.yield
local corostatus = ngx and coroutine._status or coroutine.status
local corowrap = coroutine.wrap
if not setfenv then -- Lua 5.2+
-- based on http://lua-users.org/lists/lua-l/2010-06/msg00314.html
-- this assumes f is a function
local function findenv(f)
local level = 1
repeat
local name, value = debug.getupvalue(f, level)
if name == '_ENV' then return level, value end
level = level + 1
until name == nil
return nil end
getfenv = function (f) return(select(2, findenv(f)) or _G) end
setfenv = function (f, t)
local level = findenv(f)
if level then debug.setupvalue(f, level, t) end
return f end
end
-- check for OS and convert file names to lower case on windows
-- (its file system is case insensitive, but case preserving), as setting a
-- breakpoint on x:\Foo.lua will not work if the file was loaded as X:\foo.lua.
-- OSX and Windows behave the same way (case insensitive, but case preserving).
-- OSX can be configured to be case-sensitive, so check for that. This doesn't
-- handle the case of different partitions having different case-sensitivity.
local win = os and os.getenv and (os.getenv('WINDIR') or (os.getenv('OS') or ''):match('[Ww]indows')) and true or false
local mac = not win and (os and os.getenv and os.getenv('DYLD_LIBRARY_PATH') or not io.open("/proc")) and true or false
local iscasepreserving = win or (mac and io.open('/library') ~= nil)
-- turn jit off based on Mike Pall's comment in this discussion:
-- http://www.freelists.org/post/luajit/Debug-hooks-and-JIT,2
-- "You need to turn it off at the start if you plan to receive
-- reliable hook calls at any later point in time."
if jit and jit.off then jit.off() end
local socket = require "socket"
local coro_debugger
local coro_debugee
local coroutines = {}; setmetatable(coroutines, {__mode = "k"}) -- "weak" keys
local events = { BREAK = 1, WATCH = 2, RESTART = 3, STACK = 4 }
local breakpoints = {}
local watches = {}
local lastsource
local lastfile
local watchescnt = 0
local abort -- default value is nil; this is used in start/loop distinction
local seen_hook = false
local checkcount = 0
local step_into = false
local step_over = false
local step_level = 0
local stack_level = 0
local server
local buf
local outputs = {}
local iobase = {print = print}
local basedir = ""
local deferror = "execution aborted at default debugee"
local debugee = function ()
local a = 1
for _ = 1, 10 do a = a + 1 end
error(deferror)
end
local function q(s) return string.gsub(s, '([%(%)%.%%%+%-%*%?%[%^%$%]])','%%%1') end
local serpent = (function() ---- include Serpent module for serialization
local n, v = "serpent", "0.30" -- (C) 2012-17 Paul Kulchenko; MIT License
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
local badtype = {thread = true, userdata = true, cdata = true}
local getmetatable = debug and debug.getmetatable or getmetatable
local pairs = function(t) return next, t end -- avoid using __pairs in Lua 5.2+
local keyword, globals, G = {}, {}, (_G or _ENV)
for _,k in ipairs({'and', 'break', 'do', 'else', 'elseif', 'end', 'false',
'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat',
'return', 'then', 'true', 'until', 'while'}) do keyword[k] = true end
for k,v in pairs(G) do globals[v] = k end -- build func to name mapping
for _,g in ipairs({'coroutine', 'debug', 'io', 'math', 'string', 'table', 'os'}) do
for k,v in pairs(type(G[g]) == 'table' and G[g] or {}) do globals[v] = g..'.'..k end end
local function s(t, opts)
local name, indent, fatal, maxnum = opts.name, opts.indent, opts.fatal, opts.maxnum
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
local maxlen, metatostring = tonumber(opts.maxlength), opts.metatostring
local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge)
local numformat = opts.numformat or "%.17g"
local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0
local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)",
-- tostring(val) is needed because __tostring may return a non-string value
function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return tostring(syms[s]) end)) end
local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or numformat:format(s))
or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end
local function globerr(s,l) return globals[s] and globals[s]..comment(s,l) or not fatal
and safestr(select(2, pcall(tostring, s))) or error("Can't serialize "..tostring(s)) end
local function safename(path, name) -- generates foo.bar, foo[3], or foo['b a r']
local n = name == nil and '' or name
local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
local safe = plain and n or '['..safestr(n)..']'
return (path or '')..(plain and path and '.' or '')..safe, safe end
local alphanumsort = type(opts.sortkeys) == 'function' and opts.sortkeys or function(k, o, n) -- k=keys, o=originaltable, n=padding
local maxn, to = tonumber(n) or 12, {number = 'a', string = 'b'}
local function padnum(d) return ("%0"..tostring(maxn).."d"):format(tonumber(d)) end
table.sort(k, function(a,b)
-- sort numeric keys first: k[key] is not nil for numerical keys
return (k[a] ~= nil and 0 or to[type(a)] or 'z')..(tostring(a):gsub("%d+",padnum))
< (k[b] ~= nil and 0 or to[type(b)] or 'z')..(tostring(b):gsub("%d+",padnum)) end) end
local function val2str(t, name, indent, insref, path, plainindex, level)
local ttype, level, mt = type(t), (level or 0), getmetatable(t)
local spath, sname = safename(path, name)
local tag = plainindex and
((type(name) == "number") and '' or name..space..'='..space) or
(name ~= nil and sname..space..'='..space or '')
if seen[t] then -- already seen this element
sref[#sref+1] = spath..space..'='..space..seen[t]
return tag..'nil'..comment('ref', level) end
-- protect from those cases where __tostring may fail
if type(mt) == 'table' then
local to, tr = pcall(function() return mt.__tostring(t) end)
local so, sr = pcall(function() return mt.__serialize(t) end)
if (opts.metatostring ~= false and to or so) then -- knows how to serialize itself
seen[t] = insref or spath
t = so and sr or tr
ttype = type(t)
end -- new value falls through to be serialized
end
if ttype == "table" then
if level >= maxl then return tag..'{}'..comment('maxlvl', level) end
seen[t] = insref or spath
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
if maxlen and maxlen < 0 then return tag..'{}'..comment('maxlen', level) end
local maxn, o, out = math.min(#t, maxnum or #t), {}, {}
for key = 1, maxn do o[key] = key end
if not maxnum or #o < maxnum then
local n = #o -- n = n + 1; o[n] is much faster than o[#o+1] on large tables
for key in pairs(t) do if o[key] ~= key then n = n + 1; o[n] = key end end end
if maxnum and #o > maxnum then o[maxnum+1] = nil end
if opts.sortkeys and #o > maxn then alphanumsort(o, t, opts.sortkeys) end
local sparse = sparse and #o > maxn -- disable sparsness if only numeric keys (shorter output)
for n, key in ipairs(o) do
local value, ktype, plainindex = t[key], type(key), n <= maxn and not sparse
if opts.valignore and opts.valignore[value] -- skip ignored values; do nothing
or opts.keyallow and not opts.keyallow[key]
or opts.keyignore and opts.keyignore[key]
or opts.valtypeignore and opts.valtypeignore[type(value)] -- skipping ignored value types
or sparse and value == nil then -- skipping nils; do nothing
elseif ktype == 'table' or ktype == 'function' or badtype[ktype] then
if not seen[key] and not globals[key] then
sref[#sref+1] = 'placeholder'
local sname = safename(iname, gensym(key)) -- iname is table for local variables
sref[#sref] = val2str(key,sname,indent,sname,iname,true) end
sref[#sref+1] = 'placeholder'
local path = seen[t]..'['..tostring(seen[key] or globals[key] or gensym(key))..']'
sref[#sref] = path..space..'='..space..tostring(seen[value] or val2str(value,nil,indent,path))
else
out[#out+1] = val2str(value,key,indent,insref,seen[t],plainindex,level+1)
if maxlen then
maxlen = maxlen - #out[#out]
if maxlen < 0 then break end
end
end
end
local prefix = string.rep(indent or '', level)
local head = indent and '{\n'..prefix..indent or '{'
local body = table.concat(out, ','..(indent and '\n'..prefix..indent or space))
local tail = indent and "\n"..prefix..'}' or '}'
return (custom and custom(tag,head,body,tail,level) or tag..head..body..tail)..comment(t, level)
elseif badtype[ttype] then
seen[t] = insref or spath
return tag..globerr(t, level)
elseif ttype == 'function' then
seen[t] = insref or spath
if opts.nocode then return tag.."function() --[[..skipped..]] end"..comment(t, level) end
local ok, res = pcall(string.dump, t)
local func = ok and "((loadstring or load)("..safestr(res)..",'@serialized'))"..comment(t, level)
return tag..(func or globerr(t, level))
else return tag..safestr(t) end -- handle all other types
end
local sepr = indent and "\n" or ";"..space
local body = val2str(t, name, indent) -- this call also populates sref
local tail = #sref>1 and table.concat(sref, sepr)..sepr or ''
local warn = opts.comment and #sref>1 and space.."--[[incomplete output with shared/self-references skipped]]" or ''
return not name and body..warn or "do local "..body..sepr..tail.."return "..name..sepr.."end"
end
local function deserialize(data, opts)
local env = (opts and opts.safe == false) and G
or setmetatable({}, {
__index = function(t,k) return t end,
__call = function(t,...) error("cannot call functions") end
})
local f, res = (loadstring or load)('return '..data, nil, nil, env)
if not f then f, res = (loadstring or load)(data, nil, nil, env) end
if not f then return f, res end
if setfenv then setfenv(f, env) end
return pcall(f)
end
local function merge(a, b) if b then for k,v in pairs(b) do a[k] = v end end; return a; end
return { _NAME = n, _COPYRIGHT = c, _DESCRIPTION = d, _VERSION = v, serialize = s,
load = deserialize,
dump = function(a, opts) return s(a, merge({name = '_', compact = true, sparse = true}, opts)) end,
line = function(a, opts) return s(a, merge({sortkeys = true, comment = true}, opts)) end,
block = function(a, opts) return s(a, merge({indent = ' ', sortkeys = true, comment = true}, opts)) end }
end)() ---- end of Serpent module
mobdebug.line = serpent.line
mobdebug.dump = serpent.dump
mobdebug.linemap = nil
mobdebug.loadstring = loadstring
local function removebasedir(path, basedir)
if iscasepreserving then
-- check if the lowercased path matches the basedir
-- if so, return substring of the original path (to not lowercase it)
return path:lower():find('^'..q(basedir:lower()))
and path:sub(#basedir+1) or path
else
return string.gsub(path, '^'..q(basedir), '')
end
end
local function stack(start)
local function vars(f)
local func = debug.getinfo(f, "f").func
local i = 1
local locals = {}
-- get locals
while true do
local name, value = debug.getlocal(f, i)
if not name then break end
if string.sub(name, 1, 1) ~= '(' then
locals[name] = {value, select(2,pcall(tostring,value))}
end
i = i + 1
end
-- get varargs (these use negative indices)
i = 1
while true do
local name, value = debug.getlocal(f, -i)
-- `not name` should be enough, but LuaJIT 2.0.0 incorrectly reports `(*temporary)` names here
if not name or name ~= "(*vararg)" then break end
locals[name:gsub("%)$"," "..i..")")] = {value, select(2,pcall(tostring,value))}
i = i + 1
end
-- get upvalues
i = 1
local ups = {}
while func do -- check for func as it may be nil for tail calls
local name, value = debug.getupvalue(func, i)
if not name then break end
ups[name] = {value, select(2,pcall(tostring,value))}
i = i + 1
end
return locals, ups
end
local stack = {}
local linemap = mobdebug.linemap
for i = (start or 0), 100 do
local source = debug.getinfo(i, "Snl")
if not source then break end
local src = source.source
if src:find("@") == 1 then
src = src:sub(2):gsub("\\", "/")
if src:find("%./") == 1 then src = src:sub(3) end
end
table.insert(stack, { -- remove basedir from source
{source.name, removebasedir(src, basedir),
linemap and linemap(source.linedefined, source.source) or source.linedefined,
linemap and linemap(source.currentline, source.source) or source.currentline,
source.what, source.namewhat, source.short_src},
vars(i+1)})
if source.what == 'main' then break end
end
return stack
end
local function set_breakpoint(file, line)
if file == '-' and lastfile then file = lastfile
elseif iscasepreserving then file = string.lower(file) end
if not breakpoints[line] then breakpoints[line] = {} end
breakpoints[line][file] = true
end
local function remove_breakpoint(file, line)
if file == '-' and lastfile then file = lastfile
elseif file == '*' and line == 0 then breakpoints = {}
elseif iscasepreserving then file = string.lower(file) end
if breakpoints[line] then breakpoints[line][file] = nil end
end
local function has_breakpoint(file, line)
return breakpoints[line]
and breakpoints[line][iscasepreserving and string.lower(file) or file]
end
local function restore_vars(vars)
if type(vars) ~= 'table' then return end
-- locals need to be processed in the reverse order, starting from
-- the inner block out, to make sure that the localized variables
-- are correctly updated with only the closest variable with
-- the same name being changed
-- first loop find how many local variables there is, while
-- the second loop processes them from i to 1
local i = 1
while true do
local name = debug.getlocal(3, i)
if not name then break end
i = i + 1
end
i = i - 1
local written_vars = {}
while i > 0 do
local name = debug.getlocal(3, i)
if not written_vars[name] then
if string.sub(name, 1, 1) ~= '(' then
debug.setlocal(3, i, rawget(vars, name))
end
written_vars[name] = true
end
i = i - 1
end
i = 1
local func = debug.getinfo(3, "f").func
while true do
local name = debug.getupvalue(func, i)
if not name then break end
if not written_vars[name] then
if string.sub(name, 1, 1) ~= '(' then
debug.setupvalue(func, i, rawget(vars, name))
end
written_vars[name] = true
end
i = i + 1
end
end
local function capture_vars(level, thread)
level = (level or 0)+2 -- add two levels for this and debug calls
local func = (thread and debug.getinfo(thread, level, "f") or debug.getinfo(level, "f") or {}).func
if not func then return {} end
local vars = {['...'] = {}}
local i = 1
while true do
local name, value = debug.getupvalue(func, i)
if not name then break end
if string.sub(name, 1, 1) ~= '(' then vars[name] = value end
i = i + 1
end
i = 1
while true do
local name, value
if thread then
name, value = debug.getlocal(thread, level, i)
else
name, value = debug.getlocal(level, i)
end
if not name then break end
if string.sub(name, 1, 1) ~= '(' then vars[name] = value end
i = i + 1
end
-- get varargs (these use negative indices)
i = 1
while true do
local name, value
if thread then
name, value = debug.getlocal(thread, level, -i)
else
name, value = debug.getlocal(level, -i)
end
-- `not name` should be enough, but LuaJIT 2.0.0 incorrectly reports `(*temporary)` names here
if not name or name ~= "(*vararg)" then break end
vars['...'][i] = value
i = i + 1
end
-- returned 'vars' table plays a dual role: (1) it captures local values
-- and upvalues to be restored later (in case they are modified in "eval"),
-- and (2) it provides an environment for evaluated chunks.
-- getfenv(func) is needed to provide proper environment for functions,
-- including access to globals, but this causes vars[name] to fail in
-- restore_vars on local variables or upvalues with `nil` values when
-- 'strict' is in effect. To avoid this `rawget` is used in restore_vars.
setmetatable(vars, { __index = getfenv(func), __newindex = getfenv(func) })
return vars
end
local function stack_depth(start_depth)
for i = start_depth, 0, -1 do
if debug.getinfo(i, "l") then return i+1 end
end
return start_depth
end
local function is_safe(stack_level)
-- the stack grows up: 0 is getinfo, 1 is is_safe, 2 is debug_hook, 3 is user function
if stack_level == 3 then return true end
for i = 3, stack_level do
-- return if it is not safe to abort
local info = debug.getinfo(i, "S")
if not info then return true end
if info.what == "C" then return false end
end
return true
end
local function in_debugger()
local this = debug.getinfo(1, "S").source
-- only need to check few frames as mobdebug frames should be close
for i = 3, 7 do
local info = debug.getinfo(i, "S")
if not info then return false end
if info.source == this then return true end
end
return false
end
local function is_pending(peer)
-- if there is something already in the buffer, skip check
if not buf and checkcount >= mobdebug.checkcount then
peer:settimeout(0) -- non-blocking
buf = peer:receive(1)
peer:settimeout() -- back to blocking
checkcount = 0
end
return buf
end
local function readnext(peer, num)
peer:settimeout(0) -- non-blocking
local res, err, partial = peer:receive(num)
peer:settimeout() -- back to blocking
return res or partial or '', err
end
local function handle_breakpoint(peer)
-- check if the buffer has the beginning of SETB/DELB command;
-- this is to avoid reading the entire line for commands that
-- don't need to be handled here.
if not buf or not (buf:sub(1,1) == 'S' or buf:sub(1,1) == 'D') then return end
-- check second character to avoid reading STEP or other S* and D* commands
if #buf == 1 then buf = buf .. readnext(peer, 1) end
if buf:sub(2,2) ~= 'E' then return end
-- need to read few more characters
buf = buf .. readnext(peer, 5-#buf)
if buf ~= 'SETB ' and buf ~= 'DELB ' then return end
local res, _, partial = peer:receive() -- get the rest of the line; blocking
if not res then
if partial then buf = buf .. partial end
return
end
local _, _, cmd, file, line = (buf..res):find("^([A-Z]+)%s+(.-)%s+(%d+)%s*$")
if cmd == 'SETB' then set_breakpoint(file, tonumber(line))
elseif cmd == 'DELB' then remove_breakpoint(file, tonumber(line))
else
-- this looks like a breakpoint command, but something went wrong;
-- return here to let the "normal" processing to handle,
-- although this is likely to not go well.
return
end
buf = nil
end
local function normalize_path(file)
local n
repeat
file, n = file:gsub("/+%.?/+","/") -- remove all `//` and `/./` references
until n == 0
-- collapse all up-dir references: this will clobber UNC prefix (\\?\)
-- and disk on Windows when there are too many up-dir references: `D:\foo\..\..\bar`;
-- handle the case of multiple up-dir references: `foo/bar/baz/../../../more`;
-- only remove one at a time as otherwise `../../` could be removed;
repeat
file, n = file:gsub("[^/]+/%.%./", "", 1)
until n == 0
-- there may still be a leading up-dir reference left (as `/../` or `../`); remove it
return (file:gsub("^(/?)%.%./", "%1"))
end
local function debug_hook(event, line)
-- (1) LuaJIT needs special treatment. Because debug_hook is set for
-- *all* coroutines, and not just the one being debugged as in regular Lua
-- (http://lua-users.org/lists/lua-l/2011-06/msg00513.html),
-- need to avoid debugging mobdebug's own code as LuaJIT doesn't
-- always correctly generate call/return hook events (there are more
-- calls than returns, which breaks stack depth calculation and
-- 'step' and 'step over' commands stop working; possibly because
-- 'tail return' events are not generated by LuaJIT).
-- the next line checks if the debugger is run under LuaJIT and if
-- one of debugger methods is present in the stack, it simply returns.
if jit then
-- when luajit is compiled with LUAJIT_ENABLE_LUA52COMPAT,
-- coroutine.running() returns non-nil for the main thread.
local coro, main = coroutine.running()
if not coro or main then coro = 'main' end
local disabled = coroutines[coro] == false
or coroutines[coro] == nil and coro ~= (coro_debugee or 'main')
if coro_debugee and disabled or not coro_debugee and (disabled or in_debugger())
then return end
end
-- (2) check if abort has been requested and it's safe to abort
if abort and is_safe(stack_level) then error(abort) end
-- (3) also check if this debug hook has not been visited for any reason.
-- this check is needed to avoid stepping in too early
-- (for example, when coroutine.resume() is executed inside start()).
if not seen_hook and in_debugger() then return end
if event == "call" then
stack_level = stack_level + 1
elseif event == "return" or event == "tail return" then
stack_level = stack_level - 1
elseif event == "line" then
if mobdebug.linemap then
local ok, mappedline = pcall(mobdebug.linemap, line, debug.getinfo(2, "S").source)
if ok then line = mappedline end
if not line then return end
end
-- may need to fall through because of the following:
-- (1) step_into
-- (2) step_over and stack_level <= step_level (need stack_level)
-- (3) breakpoint; check for line first as it's known; then for file
-- (4) socket call (only do every Xth check)
-- (5) at least one watch is registered
if not (
step_into or step_over or breakpoints[line] or watchescnt > 0
or is_pending(server)
) then checkcount = checkcount + 1; return end
checkcount = mobdebug.checkcount -- force check on the next command
-- this is needed to check if the stack got shorter or longer.
-- unfortunately counting call/return calls is not reliable.
-- the discrepancy may happen when "pcall(load, '')" call is made
-- or when "error()" is called in a function.
-- in either case there are more "call" than "return" events reported.
-- this validation is done for every "line" event, but should be "cheap"
-- as it checks for the stack to get shorter (or longer by one call).
-- start from one level higher just in case we need to grow the stack.
-- this may happen after coroutine.resume call to a function that doesn't
-- have any other instructions to execute. it triggers three returns:
-- "return, tail return, return", which needs to be accounted for.
stack_level = stack_depth(stack_level+1)
local caller = debug.getinfo(2, "S")
-- grab the filename and fix it if needed
local file = lastfile
if (lastsource ~= caller.source) then
file, lastsource = caller.source, caller.source
-- technically, users can supply names that may not use '@',
-- for example when they call loadstring('...', 'filename.lua').
-- Unfortunately, there is no reliable/quick way to figure out
-- what is the filename and what is the source code.
-- If the name doesn't start with `@`, assume it's a file name if it's all on one line.
if find(file, "^@") or not find(file, "[\r\n]") then
file = gsub(gsub(file, "^@", ""), "\\", "/")
-- normalize paths that may include up-dir or same-dir references
-- if the path starts from the up-dir or reference,
-- prepend `basedir` to generate absolute path to keep breakpoints working.
-- ignore qualified relative path (`D:../`) and UNC paths (`\\?\`)
if find(file, "^%.%./") then file = basedir..file end
if find(file, "/%.%.?/") then file = normalize_path(file) end
-- need this conversion to be applied to relative and absolute
-- file names as you may write "require 'Foo'" to
-- load "foo.lua" (on a case insensitive file system) and breakpoints
-- set on foo.lua will not work if not converted to the same case.
if iscasepreserving then file = string.lower(file) end
if find(file, "^%./") then file = sub(file, 3)
else file = gsub(file, "^"..q(basedir), "") end
-- some file systems allow newlines in file names; remove these.
file = gsub(file, "\n", ' ')
else
file = mobdebug.line(file)
end
-- set to true if we got here; this only needs to be done once per
-- session, so do it here to at least avoid setting it for every line.
seen_hook = true
lastfile = file
end
if is_pending(server) then handle_breakpoint(server) end
local vars, status, res
if (watchescnt > 0) then
vars = capture_vars(1)
for index, value in pairs(watches) do
setfenv(value, vars)
local ok, fired = pcall(value)
if ok and fired then
status, res = cororesume(coro_debugger, events.WATCH, vars, file, line, index)
break -- any one watch is enough; don't check multiple times
end
end
end
-- need to get into the "regular" debug handler, but only if there was
-- no watch that was fired. If there was a watch, handle its result.
local getin = (status == nil) and
(step_into
-- when coroutine.running() return `nil` (main thread in Lua 5.1),
-- step_over will equal 'main', so need to check for that explicitly.
or (step_over and step_over == (coroutine.running() or 'main') and stack_level <= step_level)
or has_breakpoint(file, line)
or is_pending(server))
if getin then
vars = vars or capture_vars(1)
step_into = false
step_over = false
status, res = cororesume(coro_debugger, events.BREAK, vars, file, line)
end
-- handle 'stack' command that provides stack() information to the debugger
while status and res == 'stack' do
-- resume with the stack trace and variables
if vars then restore_vars(vars) end -- restore vars so they are reflected in stack values
status, res = cororesume(coro_debugger, events.STACK, stack(3), file, line)
end
-- need to recheck once more as resume after 'stack' command may
-- return something else (for example, 'exit'), which needs to be handled
if status and res and res ~= 'stack' then
if not abort and res == "exit" then mobdebug.onexit(1, true); return end
if not abort and res == "done" then mobdebug.done(); return end
abort = res
-- only abort if safe; if not, there is another (earlier) check inside
-- debug_hook, which will abort execution at the first safe opportunity
if is_safe(stack_level) then error(abort) end
elseif not status and res then
error(res, 2) -- report any other (internal) errors back to the application
end
if vars then restore_vars(vars) end
-- last command requested Step Over/Out; store the current thread
if step_over == true then step_over = coroutine.running() or 'main' end
end
end
local function stringify_results(params, status, ...)
if not status then return status, ... end -- on error report as it
params = params or {}
if params.nocode == nil then params.nocode = true end
if params.comment == nil then params.comment = 1 end
local t = {...}
for i,v in pairs(t) do -- stringify each of the returned values
local ok, res = pcall(mobdebug.line, v, params)
t[i] = ok and res or ("%q"):format(res):gsub("\010","n"):gsub("\026","\\026")
end
-- stringify table with all returned values
-- this is done to allow each returned value to be used (serialized or not)
-- intependently and to preserve "original" comments
return pcall(mobdebug.dump, t, {sparse = false})
end
local function isrunning()
return coro_debugger and (corostatus(coro_debugger) == 'suspended' or corostatus(coro_debugger) == 'running')
end
-- this is a function that removes all hooks and closes the socket to
-- report back to the controller that the debugging is done.
-- the script that called `done` can still continue.
local function done()
if not (isrunning() and server) then return end
if not jit then
for co, debugged in pairs(coroutines) do
if debugged then debug.sethook(co) end
end
end
debug.sethook()
server:close()
coro_debugger = nil -- to make sure isrunning() returns `false`
seen_hook = nil -- to make sure that the next start() call works
abort = nil -- to make sure that callback calls use proper "abort" value
end
local function debugger_loop(sev, svars, sfile, sline)
local command
local app, osname
local eval_env = svars or {}
local function emptyWatch () return false end
local loaded = {}
for k in pairs(package.loaded) do loaded[k] = true end
while true do
local line, err
local wx = rawget(genv, "wx") -- use rawread to make strict.lua happy
if (wx or mobdebug.yield) and server.settimeout then server:settimeout(mobdebug.yieldtimeout) end
while true do
line, err = server:receive()
if not line and err == "timeout" then
-- yield for wx GUI applications if possible to avoid "busyness"
app = app or (wx and wx.wxGetApp and wx.wxGetApp())
if app then
local win = app:GetTopWindow()
local inloop = app:IsMainLoopRunning()
osname = osname or wx.wxPlatformInfo.Get():GetOperatingSystemFamilyName()
if win and not inloop then
-- process messages in a regular way
-- and exit as soon as the event loop is idle
if osname == 'Unix' then wx.wxTimer(app):Start(10, true) end
local exitLoop = function()
win:Disconnect(wx.wxID_ANY, wx.wxID_ANY, wx.wxEVT_IDLE)
win:Disconnect(wx.wxID_ANY, wx.wxID_ANY, wx.wxEVT_TIMER)
app:ExitMainLoop()
end
win:Connect(wx.wxEVT_IDLE, exitLoop)
win:Connect(wx.wxEVT_TIMER, exitLoop)
app:MainLoop()
end
elseif mobdebug.yield then mobdebug.yield()
end
elseif not line and err == "closed" then
error("Debugger connection closed", 0)
else
-- if there is something in the pending buffer, prepend it to the line
if buf then line = buf .. line; buf = nil end
break
end
end
if server.settimeout then server:settimeout() end -- back to blocking
command = string.sub(line, string.find(line, "^[A-Z]+"))
if command == "SETB" then
local _, _, _, file, line = string.find(line, "^([A-Z]+)%s+(.-)%s+(%d+)%s*$")
if file and line then
set_breakpoint(file, tonumber(line))
server:send("200 OK\n")
else
server:send("400 Bad Request\n")
end
elseif command == "DELB" then
local _, _, _, file, line = string.find(line, "^([A-Z]+)%s+(.-)%s+(%d+)%s*$")
if file and line then
remove_breakpoint(file, tonumber(line))
server:send("200 OK\n")
else
server:send("400 Bad Request\n")
end
elseif command == "EXEC" then
-- extract any optional parameters
local params = string.match(line, "--%s*(%b{})%s*$")
local _, _, chunk = string.find(line, "^[A-Z]+%s+(.+)$")
if chunk then
local func, res = mobdebug.loadstring(chunk)
local status
if func then
local pfunc = params and loadstring("return "..params) -- use internal function
params = pfunc and pfunc()
params = (type(params) == "table" and params or {})
local stack = tonumber(params.stack)
-- if the requested stack frame is not the current one, then use a new capture
-- with a specific stack frame: `capture_vars(0, coro_debugee)`
local env = stack and coro_debugee and capture_vars(stack-1, coro_debugee) or eval_env
setfenv(func, env)
status, res = stringify_results(params, pcall(func, unpack(env['...'] or {})))
end
if status then
if mobdebug.onscratch then mobdebug.onscratch(res) end
server:send("200 OK " .. tostring(#res) .. "\n")
server:send(res)
else
-- fix error if not set (for example, when loadstring is not present)
if not res then res = "Unknown error" end
server:send("401 Error in Expression " .. tostring(#res) .. "\n")
server:send(res)
end
else
server:send("400 Bad Request\n")
end
elseif command == "LOAD" then
local _, _, size, name = string.find(line, "^[A-Z]+%s+(%d+)%s+(%S.-)%s*$")
size = tonumber(size)
if abort == nil then -- no LOAD/RELOAD allowed inside start()
if size > 0 then server:receive(size) end
if sfile and sline then
server:send("201 Started " .. sfile .. " " .. tostring(sline) .. "\n")
else
server:send("200 OK 0\n")
end
else
-- reset environment to allow required modules to load again
-- remove those packages that weren't loaded when debugger started
for k in pairs(package.loaded) do
if not loaded[k] then package.loaded[k] = nil end
end
if size == 0 and name == '-' then -- RELOAD the current script being debugged
server:send("200 OK 0\n")
coroyield("load")
else
-- receiving 0 bytes blocks (at least in luasocket 2.0.2), so skip reading
local chunk = size == 0 and "" or server:receive(size)
if chunk then -- LOAD a new script for debugging
local func, res = mobdebug.loadstring(chunk, "@"..name)
if func then
server:send("200 OK 0\n")
debugee = func
coroyield("load")
else
server:send("401 Error in Expression " .. tostring(#res) .. "\n")
server:send(res)
end
else
server:send("400 Bad Request\n")
end
end
end
elseif command == "SETW" then
local _, _, exp = string.find(line, "^[A-Z]+%s+(.+)%s*$")
if exp then
local func, res = mobdebug.loadstring("return(" .. exp .. ")")
if func then
watchescnt = watchescnt + 1
local newidx = #watches + 1
watches[newidx] = func
server:send("200 OK " .. tostring(newidx) .. "\n")
else
server:send("401 Error in Expression " .. tostring(#res) .. "\n")
server:send(res)
end
else
server:send("400 Bad Request\n")
end
elseif command == "DELW" then
local _, _, index = string.find(line, "^[A-Z]+%s+(%d+)%s*$")
index = tonumber(index)
if index > 0 and index <= #watches then
watchescnt = watchescnt - (watches[index] ~= emptyWatch and 1 or 0)
watches[index] = emptyWatch
server:send("200 OK\n")
else
server:send("400 Bad Request\n")
end
elseif command == "RUN" then
server:send("200 OK\n")
local ev, vars, file, line, idx_watch = coroyield()
eval_env = vars
if ev == events.BREAK then
server:send("202 Paused " .. file .. " " .. tostring(line) .. "\n")
elseif ev == events.WATCH then
server:send("203 Paused " .. file .. " " .. tostring(line) .. " " .. tostring(idx_watch) .. "\n")
elseif ev == events.RESTART then
-- nothing to do
else
server:send("401 Error in Execution " .. tostring(#file) .. "\n")
server:send(file)
end
elseif command == "STEP" then
server:send("200 OK\n")
step_into = true
local ev, vars, file, line, idx_watch = coroyield()
eval_env = vars
if ev == events.BREAK then
server:send("202 Paused " .. file .. " " .. tostring(line) .. "\n")
elseif ev == events.WATCH then
server:send("203 Paused " .. file .. " " .. tostring(line) .. " " .. tostring(idx_watch) .. "\n")
elseif ev == events.RESTART then
-- nothing to do
else
server:send("401 Error in Execution " .. tostring(#file) .. "\n")
server:send(file)
end
elseif command == "OVER" or command == "OUT" then
server:send("200 OK\n")
step_over = true
-- OVER and OUT are very similar except for
-- the stack level value at which to stop
if command == "OUT" then step_level = stack_level - 1
else step_level = stack_level end
local ev, vars, file, line, idx_watch = coroyield()
eval_env = vars
if ev == events.BREAK then
server:send("202 Paused " .. file .. " " .. tostring(line) .. "\n")
elseif ev == events.WATCH then
server:send("203 Paused " .. file .. " " .. tostring(line) .. " " .. tostring(idx_watch) .. "\n")
elseif ev == events.RESTART then
-- nothing to do
else
server:send("401 Error in Execution " .. tostring(#file) .. "\n")
server:send(file)
end
elseif command == "BASEDIR" then
local _, _, dir = string.find(line, "^[A-Z]+%s+(.+)%s*$")
if dir then
basedir = iscasepreserving and string.lower(dir) or dir
-- reset cached source as it may change with basedir
lastsource = nil
server:send("200 OK\n")
else
server:send("400 Bad Request\n")
end
elseif command == "SUSPEND" then
-- do nothing; it already fulfilled its role
elseif command == "DONE" then
coroyield("done")
return -- done with all the debugging
elseif command == "STACK" then
-- first check if we can execute the stack command
-- as it requires yielding back to debug_hook it cannot be executed
-- if we have not seen the hook yet as happens after start().
-- in this case we simply return an empty result
local vars, ev = {}
if seen_hook then
ev, vars = coroyield("stack")
end
if ev and ev ~= events.STACK then
server:send("401 Error in Execution " .. tostring(#vars) .. "\n")
server:send(vars)