Jump to content




Need Help With My Debug Program


  • You cannot reply to this topic
6 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 August 2013 - 08:27 AM

Hello com,
I'm working on this little program now for an day and It won'T work right.
It's is a debugger which displays all variables of an program and allows you to change them.

here is the code:
Spoiler

this is my test program:
local count = 1
while true do
  count = count+1
  term.setBackgroundColor(colors.green)
  term.clear()
  term.setCursorPos(1,1)
  term.write("count = "..count)
  sleep(1)
end

I want that it run the program and when I press F2 it switches between the modes. but It neither run the program nor displays the variables and if I hit the F2 key in the debug mode, it don't switches to the program. What I am doing wrong?

PS:
For now the "edit the variables" is not added

I hope someone can help me,

Freack100

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 29 August 2013 - 10:10 AM

You are not getting the environment of the program in question. To get the environment of a program I suggest doing something similair like this:
local file = fs.open( filename, 'r' )
local content = file.readAll():gsub( 'local', '' )
file.close()

local func, err = loadstring( content, filename )
if not func then
  error( 'Compile error: ' .. err, 0 )
end

local ok, err_ = pcall( func ) -- you have to execute the function before you can get the environment
if not ok then
  error( 'Runtime error: ' .. err_, 0 )
end

local fileEnv = getfenv( func )
-- this table contains everything from the _G table and the global vars of the file


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 August 2013 - 10:24 AM

View PostEngineer, on 29 August 2013 - 10:10 AM, said:

You are not getting the environment of the program in question. To get the environment of a program I suggest doing something similair like this:
local file = fs.open( filename, 'r' )
local content = file.readAll():gsub( 'local', '' )
file.close()

local func, err = loadstring( content, filename )
if not func then
  error( 'Compile error: ' .. err, 0 )
end

local ok, err_ = pcall( func ) -- you have to execute the function before you can get the environment
if not ok then
  error( 'Runtime error: ' .. err_, 0 )
end

local fileEnv = getfenv( func )
-- this table contains everything from the _G table and the global vars of the file
okay, now I get everything in the _G table but I only want the variables of my debugged program...

And why isn't the test program running while debugging?

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 29 August 2013 - 10:43 AM

You can filter out the _G table yourself, if you can't take that on, on yourself then I suggest that you start smaller

#5 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 29 August 2013 - 11:18 AM

Engineer is probably right about your needing to start smaller if you can't filter out the table on your own, but since I don't really see the harm in explaining this one thing, I'll try to show you how I might go about it.

You could simply iterate through the table using pairs and remove anything that has a type of "function".
local functionIndexes = {}
for index, value in pairs (env) do
	if type (value) == "function" then
		functionIndexes[index] = true
	end
end

for functionIndex, _ in pairs (functionIndexes) do
	env[functionIndex] = nil
end


#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 29 August 2013 - 11:45 AM

I know how to filter it out :D

now I only have ne question left:
why isn't the test program running while debugging?

#7 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 29 August 2013 - 12:24 PM

My best guess at a first glance is that the program errors out at the first API call, since it's environment contains nothing. Metatables for the win:
local env = setmetatable({}, {__index = _G})

BTW, I hope you know that the debugger will only notice the debug key if the program yields or errors out.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users