Skip to content

Commit

Permalink
split up io to allow console vs network io
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalint committed Mar 19, 2013
1 parent 0f53c3e commit 60f3c04
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 31 deletions.
51 changes: 51 additions & 0 deletions console_io.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- _____ _ _____ ____
-- / ____| | | |_ _/ __ \
-- | | ___ _ __ ___ ___ | | ___ | || | | |
-- | | / _ \| '_ \/ __|/ _ \| |/ _ \ | || | | |
-- | |___| (_) | | | \__ \ (_) | | __/ _| || |__| |
-- \_____\___/|_| |_|___/\___/|_|\___| |_____\____/

-- IO module that reads/writes to console

local console_io = {}
console_io.__index = console_io

function console_io:new(o)
o = o or {}
setmetatable(o, self)
return o
end

function console_io:write(...)
io.stdout:write(...)
io.stdout:flush()
end

function console_io:print(...)
self:write(...)
self:write("\n")
end

function console_io:flush()
io.stdout:flush()
end

function console_io:read(format)
return io.stdin:read(format)
end

function console_io:command_loop()
while true do
self:write("yt> ")
local cmd = self:read("*l")
local chunk = load(cmd)
local success, m2 = pcall(chunk)
if not success then
self:print("Error: " .. m2)
end
end
end

print("console_io.lua - loaded with " .. _VERSION)

return console_io
56 changes: 25 additions & 31 deletions debuglib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ breakpoints = {}
single_step_location = nil
single_step_method_id = nil

-- i/o
dbgio = require("network_io")

-- ============================================================
-- Main command loop
-- ============================================================
function command_loop()
while true do
io.write("yt> ")
io.flush()
local cmd = io.read("*line")
local chunk = load(cmd)
local success, m2 = pcall(chunk)
if not success then
print("Error: " .. m2)
end
end
dbgio:command_loop()
end

-- ============================================================
Expand All @@ -49,7 +43,7 @@ end
-- Help
-- ============================================================
function help()
print("Help is on the way...")
dbgio:print("Help is on the way...")
end

-- ============================================================
Expand All @@ -59,15 +53,15 @@ function where()
local frame_count = lj_get_frame_count()

if frame_count == 0 then
print("No code running")
dbgio:print("No code running")
return nil
end

local stack = {}
for i = 1, frame_count do
local f = lj_get_stack_frame(i)
stack[i] = f
print(stack_frame_to_string(f))
dbgio:print(stack_frame_to_string(f))
end
return stack
end
Expand All @@ -79,8 +73,8 @@ function locals()
local frame = lj_get_stack_frame(depth)
local var_table = frame.method_id.local_variable_table
for k, v in pairs(var_table) do
print(string.format("%10s = %s", k,
lj_get_local_variable(depth, v.slot, v.sig)))
dbgio:print(string.format("%10s = %s", k,
lj_get_local_variable(depth, v.slot, v.sig)))
end
end

Expand All @@ -102,7 +96,7 @@ function next(num)

-- TODO allow stepping up a frame....
if not single_step_location then
print("Cannot step to next line")
dbgio:print("Cannot step to next line")
return
end

Expand Down Expand Up @@ -140,17 +134,17 @@ function frame(num)
num = num or 1
local frame_count = lj_get_frame_count()
if num < 1 then
print("Invalid frame number " .. num)
dbgio:print("Invalid frame number " .. num)
depth = 1
elseif num > frame_count then
print("Invalid frame number " .. num)
dbgio:print("Invalid frame number " .. num)
depth = frame_count
else
depth = num
end

local f = lj_get_stack_frame(depth)
print(stack_frame_to_string(f))
dbgio:print(stack_frame_to_string(f))
return f
end

Expand All @@ -173,7 +167,7 @@ function bp(method_decl, line_num)
-- make sure bp doesn't already exist
for idx, bp in ipairs(breakpoints) do
if bp.method_decl == b.method_decl and bp.line_num == b.line_num then
print("Breakpoint already exists")
dbgio:print("Breakpoint already exists")
return
end
end
Expand All @@ -182,7 +176,7 @@ function bp(method_decl, line_num)
b.method_id = lj_get_method_id(method.class, method.method, method.args, method.ret)
lj_set_breakpoint(b.method_id, b.line_num)
table.insert(breakpoints, b)
print("ok")
dbgio:print("ok")

return b
end
Expand All @@ -195,16 +189,16 @@ function bl(num)
if num then
local bp = breakpoints[num]
if not bp then
print("Invalid breakpoint")
dbgio:print("Invalid breakpoint")
return
end
print(string.format("%4d: %s", num, bp))
dbgio:print(string.format("%4d: %s", num, bp))
return bp
end

-- print all
if #breakpoints == 0 then
print("No breakpoints")
dbgio:print("No breakpoints")
return
end
for idx, bp in ipairs(breakpoints) do
Expand Down Expand Up @@ -240,8 +234,8 @@ function cb_breakpoint(thread, method_id, location)
end
end
assert(bp)
print()
print(stack_frame_to_string(lj_get_stack_frame(1)))
dbgio:print()
dbgio:print(stack_frame_to_string(lj_get_stack_frame(1)))
if bp.handler then
-- allow bp handler if present
return bp:handler()
Expand All @@ -264,7 +258,7 @@ function cb_single_step(thread, method_id, location)
lj_clear_jvmti_callback("single_step")
single_step_location = nil
single_step_method_id = nil
print(stack_frame_to_string(lj_get_stack_frame(depth)))
dbgio:print(stack_frame_to_string(lj_get_stack_frame(depth)))
return true
else
return false
Expand Down Expand Up @@ -389,13 +383,13 @@ function x()
xconcat = lj_get_method_id("java/lang/String", "concat", "Ljava/lang/String;", "Ljava/lang/String;")
test_bp = bp("Test.b(I)V")
test_bp.handler = function(bp)
print("test_bp.handler")
print(string.format("bp=%s", bp))
print(string.format("a=%s", a))
dbgio:print("test_bp.handler")
dbgio:print(string.format("bp=%s", bp))
dbgio:print(string.format("a=%s", a))
return false
end
bl()
print(dump(lj_get_class_methods(lj_find_class("java/lang/String"))))
dbgio:print(dump(lj_get_class_methods(lj_find_class("java/lang/String"))))
end

function run_tests()
Expand Down
79 changes: 79 additions & 0 deletions network_io.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-- _ _ _ _ _____ ____
-- | \ | | | | | | |_ _/ __ \
-- | \| | ___| |___ _____ _ __| | __ | || | | |
-- | . ` |/ _ \ __\ \ /\ / / _ \| '__| |/ / | || | | |
-- | |\ | __/ |_ \ V V / (_) | | | < _| || |__| |
-- |_| \_|\___|\__| \_/\_/ \___/|_| |_|\_\ |_____\____/

-- IO module that works on a TCP connection

local NETWORK_IO_PORT = 8023

local network_io = {}
network_io.__index = network_io

function network_io:finish()
self.client:close()
self.server:close()
end

function network_io:new(o)
o = o or {}
setmetatable(o, self)
return o
end

function network_io:write(...)
return self.client:send(string.format("%s", unpack({...})))
end

function network_io:print(...)
if self:write(...) then
return self:write("\n")
else
return nil
end
end

function network_io:flush()
end

function network_io:read(format)
return self.client:receive(format)
end

function network_io:command_loop()
local socket = require("socket")
self.server = assert(socket.bind("*", NETWORK_IO_PORT))
self.server:setoption('tcp-nodelay', true)
self.server:setoption('reuseaddr', true)
print("network_io.lua - server started on port " .. NETWORK_IO_PORT)
while true do
self.client = self.server:accept()
self.client:send("Welcome to Yellow Tree\n")
print("network_io.lua - accepted connection ")-- from " .. self.client:getpeername())
while true do
if not self:write("yt> ") then
break
end
local cmd = self.client:receive("*l")
print(string.format("cmd is '%s'", cmd))
if not cmd then
break
end
local chunk = load(cmd)
local success, m2 = pcall(chunk)
if not success then
self:print("Error: " .. m2)
end
end
print("network_io.lua - client disconnected")
self.client:close()
end
print("network_io.lua - closing server")
self.server:close()
end

print("network_io.lua - loaded with " .. _VERSION)

return network_io

0 comments on commit 60f3c04

Please sign in to comment.