[powerdebug V2: 05/14] encapsulate the mainloop code