44import be .ugent .topl .mio .sourcemap .SourceMap ;
55import be .ugent .topl .mio .woodstate .Frame ;
66import be .ugent .topl .mio .woodstate .WOODDumpResponse ;
7+ import org .slf4j .Logger ;
8+ import org .slf4j .LoggerFactory ;
79
810import java .io .*;
911import java .net .ServerSocket ;
@@ -19,12 +21,13 @@ public class GdbStub {
1921 private final Debugger debugger ;
2022 private final String binaryLocation ;
2123 private OutputStream out ;
24+ private final Logger logger = LoggerFactory .getLogger (GdbStub .class );
2225 private final SourceMap debugSourceMap ; // Remove later, lldb doesn't need this.
2326
2427 public GdbStub (Debugger debugger , String binaryLocation ) {
2528 this .debugger = debugger ;
2629 this .binaryLocation = binaryLocation ;
27- debugSourceMap = getDwarfSourcemap (binaryLocation );
30+ this . debugSourceMap = getDwarfSourcemap (binaryLocation );
2831
2932 debugger .getBreakpointsListeners ().add ((pc ) -> {
3033 try {
@@ -82,8 +85,6 @@ private String getTriple(String s) {
8285 String hex = s .chars ()
8386 .mapToObj (c -> String .format ("%02x" , c ))
8487 .reduce ("" , String ::concat );
85-
86- System .out .println (hex );
8788 return hex ;
8889 }
8990
@@ -97,9 +98,9 @@ public void start(int port) throws IOException {
9798 byte [] wasmData = Files .readAllBytes (Path .of (binaryLocation ));
9899
99100 ServerSocket server = new ServerSocket (port );
100- System . out . printf ("Waiting for GDB on port %d ..." , port );
101+ logger . info ("Waiting for LLDB connection on port {} ..." , port );
101102 Socket sock = server .accept ();
102- System . out . println ( "GDB connected!" );
103+ logger . info ( "LLDB connected!" );
103104
104105 InputStream in = sock .getInputStream ();
105106 out = sock .getOutputStream ();
@@ -109,10 +110,10 @@ public void start(int port) throws IOException {
109110 while (attached ) {
110111 String pkt = recvPacket (in , out );
111112 if (pkt == null ) {
112- System . out . println ( "GDB closed" );
113+ logger . info ( "Connection closed" );
113114 break ;
114115 }
115- System . out . println ("<- " + pkt );
116+ logger . trace ("<- {}" , pkt );
116117
117118 if (pkt .startsWith ("m" )) {
118119 pkt = pkt .substring (1 );
@@ -121,7 +122,7 @@ public void start(int port) throws IOException {
121122 long addrType = getAddrType (pos );
122123 pos = stripAddrType (pos );
123124 long len = Long .parseUnsignedLong (memArgs [1 ], 16 );
124- log ("Read memory " + len + " bytes from " + pos + " with addr type = " + addrType );
125+ logger . info ("Read memory {} bytes from {} with addr type = {}" , len , pos , addrType );
125126
126127 // TODO: Hack to temporarily prevent lldb from using breakpoints when stepping
127128 // https://github.com/llvm/llvm-project/issues/189960
@@ -132,7 +133,7 @@ public void start(int port) throws IOException {
132133
133134 byte [] memory = wasmData ;
134135 if (addrType == 1 ) {
135- log ("Reading from wasm linear memory" );
136+ logger . info ("Reading from wasm linear memory" );
136137 memory = getCurrentState ().getMemory ().getBytes ();
137138 }
138139 else {
@@ -141,7 +142,7 @@ public void start(int port) throws IOException {
141142
142143 // The reply may contain fewer addressable memory units than requested if the server was reading from a trace frame memory and was able to read only part of the region of memory.
143144 if (pos >= memory .length || pos < 0 ) {
144- System . out . println ("Pos " + pos + " out of bounds for length " + memory .length );
145+ logger . warn ("Pos {} out of bounds for length {}" , pos , memory .length );
145146 sendPacket (out , "E01" );
146147 continue ;
147148 }
@@ -152,17 +153,13 @@ public void start(int port) throws IOException {
152153 // also think about little vs big endian
153154 int index = (int ) pos + i ;
154155 if (index >= memory .length ) {
155- System . out . println ("Stop reading, out of bounds" );
156+ logger . info ("Stop reading, out of bounds" );
156157 break ;
157158 }
158159 int b = memory [index ];
159160 result .append (String .format ("%02x" , b & 0xFF ));
160- //result.append("00");
161161 }
162162 sendPacket (out , result .toString ());
163-
164- // If memory invalid send E01
165- //sendPacket(out, "E01");
166163 continue ;
167164 }
168165 else if (pkt .startsWith ("qSupported" )) {
@@ -190,11 +187,11 @@ else if (pkt.startsWith("qWasmLocal:")) {
190187 String [] args = pkt .substring ("qWasmLocal:" .length ()).split (";" );
191188 int frameIdx = Integer .parseInt (args [0 ]);
192189 int localIdx = Integer .parseInt (args [1 ]);
193- log ("Reading local " + localIdx + " from frame " + frameIdx );
190+ logger . info ("Reading local {} from frame {}" , localIdx , frameIdx );
194191 Frame frame = getCallStack (getCurrentState ()).get (frameIdx );
195- System . out . println ( getCallStack (getCurrentState ()));
196- System . out . println ( frame );
197- System . out . println ( getCurrentState ().getStack ());
192+ logger . trace ( "{}" , getCallStack (getCurrentState ()));
193+ logger . trace ( "{}" , frame );
194+ logger . trace ( "{}" , getCurrentState ().getStack ());
198195 int fp = frame .getFp ();
199196 long value = getCurrentState ().getStack ().get (fp + localIdx + 1 ).getValue ();
200197 sendPacket (out , toHex (toWasmAddr (value ))); // If a pointer on the stack is an address it will be clear it's a wasm memory pointer.
@@ -213,14 +210,12 @@ else if (pkt.startsWith("Z")) {
213210 int type = Integer .parseInt (args [0 ]);
214211 long addr = Long .parseUnsignedLong (args [1 ], 16 );
215212 int kind = Integer .parseInt (args [2 ]);
216- log ("Add breakpoint on " + addr );
213+ logger . info ("Add breakpoint on {}" , addr );
217214 debugger .addBreakpoint ((int ) addr );
218215
219216 try {
220- for (int i = 0 ; i < 8 ; i ++) {
221- log ("Breakpoint line " + debugSourceMap .getLineForPc ((int ) addr + (i -4 ) * 4 ) + " " + debugSourceMap .getSourceFileName ((int ) addr + (i -4 ) * 4 ));
222- }
223- } catch (Exception e ) {}
217+ logger .info ("Breakpoint line {} {}" , debugSourceMap .getLineForPc ((int ) addr ), debugSourceMap .getSourceFileName ((int ) addr ));
218+ } catch (Exception _) {}
224219
225220 // A remote target shall return an empty string for an unrecognized breakpoint or watchpoint packet type.
226221 sendPacket (out , "OK" );
@@ -232,11 +227,11 @@ else if (pkt.startsWith("z")) {
232227 int type = Integer .parseInt (args [0 ]);
233228 long addr = Long .parseUnsignedLong (args [1 ], 16 );
234229 int kind = Integer .parseInt (args [2 ]);
235- log ("Remove breakpoint on " + addr );
230+ logger . info ("Remove breakpoint on {}" , addr );
236231 debugger .removeBreakpoint ((int ) addr );
237232
238233 try {
239- log ("Breakpoint line " + debugSourceMap .getLineForPc ((int ) addr ) + " " + debugSourceMap .getSourceFileName ((int ) addr ));
234+ logger . info ("Breakpoint line {} {}" , debugSourceMap .getLineForPc ((int ) addr ), debugSourceMap .getSourceFileName ((int ) addr ));
240235 } catch (Exception e ) {}
241236
242237 // A remote target shall return an empty string for an unrecognized breakpoint or watchpoint packet type.
@@ -254,8 +249,6 @@ else if (pkt.startsWith("stackInfo")) {
254249 sendPacket(out, "OK");
255250 break;*/
256251 case "qHostInfo" :
257- //sendPacket(out, "triple:x86_64-pc-linux-gnu;endian:little;ptrsize:8;");
258- //sendPacket(out, "cputype:16777228;cpusubtype:3;ostype:darwin;vendor:apple;endian:little;ptrsize:8;hostname:hello;");
259252 sendPacket (out , "vendor:wamr;ostype:wasi;arch:wasm32;endian:little;ptrsize:4;" );
260253 break ;
261254 case "qProcessInfo" :
@@ -281,7 +274,7 @@ else if (pkt.startsWith("stackInfo")) {
281274 sendPacket (out , result );
282275
283276 try {
284- log ("Current line " + debugSourceMap .getLineForPc (state .getPc ()) + " " + debugSourceMap .getSourceFileName (state .getPc ()));
277+ logger . info ("Current line {} {}" , debugSourceMap .getLineForPc (state .getPc ()), debugSourceMap .getSourceFileName (state .getPc ()));
285278 } catch (Exception e ) {}
286279
287280 break ;
@@ -301,7 +294,7 @@ else if (pkt.startsWith("stackInfo")) {
301294 sendPacket (out , encodeRegs ());
302295 break ;
303296 case "s" :
304- log ("Received step command from lldb" );
297+ logger . info ("Received step command from lldb" );
305298 debugger .stepInto ();
306299 sendStopPacket (out , "05" );
307300 break ;
@@ -318,12 +311,12 @@ else if (pkt.startsWith("stackInfo")) {
318311 break ;
319312 case "D" :
320313 attached = false ;
321- log ("Detach from target" );
314+ logger . info ("Detach from target" );
322315 debugger .close ();
323316 sendPacket (out , "OK" );
324317 break ;
325318 default :
326- System . out . println ("Unknown packet: " + pkt );
319+ logger . warn ("Unknown packet: {}" , pkt );
327320 sendPacket (out , "" );
328321 break ;
329322 }
@@ -334,13 +327,6 @@ private void sendStopPacket(OutputStream out, String signal) throws IOException
334327 sendPacket (out , "T" + signal + "thread:1;name:warduino;thread-pcs:" + toHex (getCurrentState ().getPc ()) + ";00:" + toHex (getCurrentState ().getPc ()) + ";reason:trace" );
335328 }
336329
337- private void log (String s ) {
338- System .out .print ("\u001b [36m" );
339- System .out .print ("[GDBSTUB] " );
340- System .out .println (s );
341- System .out .print ("\u001b [0m" );
342- }
343-
344330 private String recvPacket (InputStream in , OutputStream out ) throws IOException {
345331 int c ;
346332
@@ -389,7 +375,7 @@ private void sendPacket(OutputStream out, String payload) throws IOException {
389375 String pkt = "$" + payload + "#" + String .format ("%02x" , sum );
390376 out .write (pkt .getBytes ());
391377 out .flush ();
392- System . out . println ("-> " + pkt );
378+ logger . trace ("-> {}" , pkt );
393379 }
394380
395381 private int checksum (byte [] data ) {
0 commit comments