How to programatically set a breakpoint to pseudo code view?

Im using ida pro 9.1, python 3.13, how to make the breakpoints be also set in the pseudo code view?
For example:

idaapi.add_bpt(0x7ff7fa2f4205, 0, idc.BPT_SOFT)
idaapi.enable_bpt(0x7ff7fa2f4205, True)


The api sets the red background only in the asm view, how i could also make it be visible in the pseudo code view? As seen in the picture line 267 isn’t getting colored.

Try this:

ea = func_entry_address
lnnum = 267
b=bpt_t()
b.set_src_bpt("$%x" % ea, lnnum)
add_bpt(b)

I tried this way but it throw an unhandled c++ exception

                try:
                    bpt.set_src_bpt("$%x" % 0x7ff7fa2f4205, 267)
                    ida_dbg.add_bpt(bpt)
                except Exception as e:
                    print(f"Error setting breakpoint: {e}")
                    return

What apis should i use for bpt_t and add_bpt? idaapi or ida_dbg

I experimented a bit and it turned out that the format string must be $%X (uppercase).

import idaapi


def add_breakpoint(ea, lnnum):
    """
    Adds a breakpoint at the specified address and line number.

    :param ea: The address where the breakpoint should be set.
    :param lnnum: The line number in the source code where the breakpoint should be set.
    """

    fnc: idaapi.func_t = idaapi.get_func(ea)
    if not fnc:
        print("Function not found at address 0x%X" % ea)
        return

    b = idaapi.bpt_t()
    # the line breakpoint is set at the start of the function
    b.set_src_bpt("$%X" % fnc.start_ea, lnnum)
    idaapi.add_bpt(b)


ea = idaapi.get_screen_ea()
lnnum = 8
add_breakpoint(ea, lnnum)
2 Likes

Thank you, very much Milan!