Thursday, February 11, 2010

Tracing System and Library Calls w/ strace and ltrace

For whatever reason, sometimes debugging a program on a unix system requires more than just gdb. I came across two tools recently that can be used for more advanced debugging. strace and it's brother ltrace are applications that will trace system calls during runtime. This means that they will trace the interaction between the process and the system. ltrace is the more featured of the two because it has the ability to trace dynamic library calls. Common usage can be seen in this example, below:

longhorn$ vglrun ltrace -f -S testFont 2>&1 | grep -i font
SYS_open("/usr/lib64/libfontconfig.so.1", 0, 04342330000) = 3
SYS_read(3, "Name:\ttestFont\nState:\tR (running"..., 4096) = 833
SYS_read(3, "Name:\ttestFont\nState:\tR (running"..., 4096) = 833
SYS_read(3, "Name:\ttestFont\nState:\tR (running"..., 4096) = 833
SYS_open("testFont", 0, 00)                      = 3
SYS_open("/usr/lib64/libfontconfig.so.1", 0, 00) = 3
SYS_read(10, "Name:\ttestFont\nState:\tR (running"..., 4096) = 833
SYS_read(5, "Name:\ttestFont\nState:\tR (running"..., 4096) = 833

You can see both system calls and library calls in this example. You'll notice I piped the output to grep to search for a specific call. The "-S" option allow you to trace system calls, making ltrace's functionality a superset of strace's. In addition, the flag "-f" causes ltrace to also follow any child processes the main process may create.

One big difference between the two is that ltrace does not currently support multi-threaded applications because the kernel will send a SIGTRAP command to a traced process, causing premature termination. strace does work with multi-threaded applications, however. See this link for more info.

These trace applications can be very useful for finding out exactly what the code is doing at the system level, and can you you better understand how the code really works. By searching the net it seems that it is very useful for hackers and crackers and reverse engineering as well!

Monday, February 8, 2010

Dynamic Real-time Data Plotting with VTK

It is possible to interact with a dataset and generate a plot dynamically by probing the dataset. In VTK, you can do this with a few useful classes: vtkLineWidget, vtkSplineWidget, vtkProbeFilter, and vtkXYPlotActor.

Here is video of the dynamic plot in action:



It's impressive but simple. You can expand this capability by adding additional probes and using a spline instead of a line for probing, but to get the functionality you see in the video here's a python script.

The class interactionEvent will be used as a callback function for the lineWidget (or splineWidget):

#!/bin/python

from vtk import *;

#callbacks
class interactionEvent():
        def execute(self, obj, event):
                self.spline.GetPolyData(self.poly)
                self.probe.Update()

This just sets up the pipeline for reading in the dataset and all of the rendering window classes, etc...:

#Read in and set up dataset
reader = vtkDataSetReader()
reader.SetFileName("/Users/brandt/Work/pythonVTK/mummy.128.vtk")
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()

#set up rendering pipeline
renderer = vtkRenderer()
renWin = vtkRenderWindow()
iren = vtkRenderWindowInteractor()
renWin.SetSize(800,480)
renWin.AddRenderer(renderer)
iren.SetRenderWindow(renWin)

Here we set up out vtkXYPlotActor, which just controls how the plot will look:

plotActor = vtkXYPlotActor()
plotActor.SetPosition(0.6,.0)
plotActor.SetWidth(.40)
plotActor.SetHeight(.3)
plotActor.GetXAxisActor2D().SizeFontRelativeToAxisOn()
plotActor.GetYAxisActor2D().SizeFontRelativeToAxisOn()
plotActor.SetNumberOfYLabels(5)
plotActor.SetNumberOfXLabels(6)
plotActor.SetAdjustYLabels(0)
plotActor.SetNumberOfYMinorTicks(2)
plotActor.SetNumberOfXMinorTicks(2)
plotActor.AddInput(probe.GetOutput())
plotActor.SetXValuesToNormalizedArcLength()
plotActor.SetTitle( "Scalar Data" )
plotActor.SetXRange(0,1)

We set up our splineWidget here to probe the data, and add the interactionEvent class as the callback for any interaction event:

poly = vtkPolyData()
probe = vtkProbeFilter()
spline = vtkSplineWidget()
spline.GetPolyData(poly)
probe.SetInput(poly)
probe.SetSource(reader.GetOutput()) 
spline.SetInteractor(iren)
spline.SetInput(reader.GetOutput())
spline.PlaceWidget()
spline.On()
spline.SetNumberOfHandles(2)
probe.Update()
renderer.AddActor(plotActor)

Next we assign our probe, widget, and polydata to the interactionEvent class and add the class as the callback function:

#set up callback
cb = interactionEvent()
cb.spline = spline
cb.probe = probe
cb.poly = poly
spline.AddObserver("InteractionEvent", cb.execute)

Finally we create a contour of the mummy skull so we have something pretty to look at!

#draw contour
isoMapper = vtkPolyDataMapper()
isoActor = vtkActor()
iso = vtkContourFilter()
isoMapper.SetInput(iso.GetOutput())
iso.SetInput(reader.GetOutput())
iso.SetValue(0, 125)
isoActor.SetMapper(isoMapper)
renderer.AddActor(isoActor)

iren.Initialize()
iren.Start()