import re


def filter_strace_output(lines):
    """
    a function to filter QEMU logs returning only the strace entries

    Parameters
    ----------
    lines : list
        a list of strings representing the lines from a QEMU log/trace.

    Returns
    -------
    list
        a list of strings representing only the strace log entries
        the entries will also be cleaned up if a page dump occurs in the middle of them
    """

    #we only want the strace lines, so remove/ignore lines that start with the following:
    line_starts= ['^[\d,a-f]{16}-', # pylint: disable=anomalous-backslash-in-string
                      '^page',
                      '^start',
                      '^host',
                      '^Locating',
                      '^guest_base',
                      '^end_',
                      '^brk',
                      '^entry',
                      '^argv_',
                      '^env_',
                      '^auxv_',
                      '^Trace',
                      '^--- SIGSEGV',
                      '^qemu'
                      ]
    filter_string = '|'.join(line_starts)

    filtered = []
    prev_line = ""
    for line in lines:
        if re.match(filter_string,line):
            continue
        # workaround for https://gitlab.com/qemu-project/qemu/-/issues/654
        if re.search("page layout changed following target_mmap",line):
            prev_line = line.replace("page layout changed following target_mmap","")
            continue
        if re.match('^ = |^= ', line):
            line = prev_line+line

        filtered.append(line)
    return filtered