@@ -291,30 +291,37 @@ def write(self, addr: int, data: bytes) -> None:
291291 self .ql .log .error (f'addresss write error: { addr :#x} ' )
292292 raise
293293
294- def search (self , needle : bytes , begin : int = None , end : int = None ):
295- """
296- Search for a sequence of bytes in memory. Returns all sequences
297- that match
294+ def search (self , needle : bytes , begin : int = None , end : int = None ) -> Sequence [int ]:
295+ """Search for a sequence of bytes in memory.
296+
297+ Args:
298+ needle: bytes sequence to look for
299+ begin: search starting address (or None to start at lowest avaiable address)
300+ end: search ending address (or None to end at highest avaiable address)
301+
302+ Returns: addresses of all matches
298303 """
299304
300- addrs = []
301- if (begin and end ) and end > begin :
302- haystack = self .read (begin , end - begin )
303- addrs = [x .start (0 ) + begin for x in re .finditer (needle , haystack )]
305+ # if starting point not set, search from the first mapped region
306+ if begin is None :
307+ begin = self .map_info [0 ][0 ]
304308
305- if not begin :
306- begin = self .map_info [0 ][0 ] # search from the first mapped region
307- if not end :
308- end = self .map_info [- 1 ][1 ] # search till the last mapped region
309+ # if ending point not set, search till the last mapped region
310+ if end is None :
311+ end = self .map_info [- 1 ][1 ]
309312
310- mapped_range = [(_begin , _end ) for _begin , _end , _ in self .ql .uc .mem_regions ()
311- if _begin in range (begin , end ) or _end in range (begin , end )]
312-
313- for _begin , _end in mapped_range :
314- haystack = self .read (_begin , _end - _begin )
315- addrs += [x .start (0 ) + _begin for x in re .finditer (needle , haystack )]
313+ assert begin < end , 'search arguments do not make sense'
314+
315+ ranges = [(max (begin , lbound ), min (ubound , end )) for lbound , ubound , _ , _ in self .map_info if (begin <= lbound < end ) or (begin < ubound <= end )]
316+ results = []
317+
318+ for lbound , ubound in ranges :
319+ haystack = self .read (lbound , ubound - lbound )
320+ local_results = (match .start (0 ) + lbound for match in re .finditer (needle , haystack ))
321+
322+ results .extend (local_results )
316323
317- return addrs
324+ return results
318325
319326 def unmap (self , addr : int , size : int ) -> None :
320327 """Reclaim a memory range.
0 commit comments