[Lldb-commits] [lldb] [lldb] Add bidirectional packetLog to gdbclientutils.py (PR #162176)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 8 03:45:40 PDT 2025


DavidSpickett wrote:

> Yes it is, sorry about that. I used to use phabricator for this back when that was the way to do reviews, I don't currently know of a tool that works with github PR's

I miss this feature too. We mention a couple of tools on https://llvm.org/docs/GitHub.html#using-graphite-for-stacked-pull-requests but I've not tried any of them myself.

> I meant that the mock server is logging both the packets it receives from the lldb client and the packets it sends back to it.

Ok so we're only talking about the mock server here.

> It's mostly calling packetLog.index(...) with specific packets to confirm their presence and testing ranges based on that

Reading the whole PR there are several confusing things (which predate your changes but still) in here. They only get worse when we add another log.

I would prefer you do this by extending the current log to include all packets. So that we have 1 log with 3 views. Sent, received and all. The existing code can all be changed to use the received view. Logging prints them all. Could leave out sent given that nothing will use it right now.

As a starting point (feel free to take this):
```
from collections.abc import Sequence

class PacketLog(object):
  SENT = 0
  RECEIVED = 1

  def __init__(self):
    self._sent = []
    self._received = []
    self._order = []

  def add_sent(self, packet):
    self._sent.append(packet)
    self._order.append(PacketLog.SENT)

  def add_received(self, packet):
    self._received.append(packet)
    self._order.append(PacketLog.RECEIVED)

  def __iter__(self):
    sent = iter(self._sent)
    received = iter(self._received)

    for direction in self._order:
      if direction == PacketLog.SENT:
        yield next(sent)
      else:
        yield next(received)

  class PacketView(Sequence):
    def __init__(self, packets):
      self.packets = packets

    def __getitem__(self, idx):
      return self.packets[idx]

    def __len__(self):
      return len(self.packets)

  @property
  def sent(self):
    return PacketLog.PacketView(self._sent)

  @property
  def received(self):
    return PacketLog.PacketView(self._received)

pl = PacketLog()
pl.add_sent("sent this thing 1")
pl.add_received("received this thing 2")
pl.add_sent("sent this thing 3")
pl.add_received("received this thing 4")

for p in pl:
  print(p)
print()

for p in pl.sent:
  print(p)
print(pl.sent.index("sent this thing 1"))
print(pl.sent[0])
print()

for p in pl.received:
  print(p)
print(pl.received[0])
print(pl.received.index("received this thing 4"))
print()
pl.received[1] = "345345345" # will error, don't let us modify the packets
```
Two lists so we keep the indexing, slicing, iterating we do now, and a third list for insertion order so the log prints them in the correct sequence. Wrapping the lists prevents us modifying them and making the order go out of sync.

There's some super fancy ways to do this but we're not too concerned about being Pythonic here.

https://github.com/llvm/llvm-project/pull/162176


More information about the lldb-commits mailing list