public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Ena Irtnanoelec <ena.irtnanoelec@gmail.com>, gdb@sourceware.org
Subject: Re: TemporaryBreakpoint and FInishingbreakpoint issues
Date: Mon, 20 Feb 2023 21:51:00 -0500	[thread overview]
Message-ID: <25f6f329-8f75-4f05-c269-3000c0a4dde6@simark.ca> (raw)
In-Reply-To: <CAAR+ShiVU4tJdEBvQpCa3TV2mOZV+uH6Jv1vtxvSmzBMqOX9dg@mail.gmail.com>



On 2/15/23 08:22, Ena Irtnanoelec via Gdb wrote:
>   Dear all,
> 
> I've got some troubles with both FinishingBreakpoint and
> TemporaryBreakpoints I'm trying to set using the Python's API.
> 
> My use case is as follows : I've added some breakpoints on particular code
> of the linux kernel, on a syscall definition. This is the code in which I'm
> placing a BP:
> 
> SYSCALL_DEFINE3
> <https://elixir.bootlin.com/linux/latest/C/ident/COMPAT_SYSCALL_DEFINE3>(execve
> <https://elixir.bootlin.com/linux/latest/C/ident/execve>, const char
> __user <https://elixir.bootlin.com/linux/latest/C/ident/__user> *,
> filename <https://elixir.bootlin.com/linux/latest/C/ident/filename>,
> 	const compat_uptr_t
> <https://elixir.bootlin.com/linux/latest/C/ident/compat_uptr_t> __user
> <https://elixir.bootlin.com/linux/latest/C/ident/__user> *, argv
> <https://elixir.bootlin.com/linux/latest/C/ident/argv>,
> 	const compat_uptr_t
> <https://elixir.bootlin.com/linux/latest/C/ident/compat_uptr_t> __user
> <https://elixir.bootlin.com/linux/latest/C/ident/__user> *, envp
> <https://elixir.bootlin.com/linux/latest/C/ident/envp>){
> 	return compat_do_execve
> <https://elixir.bootlin.com/linux/latest/C/ident/compat_do_execve>(getname
> <https://elixir.bootlin.com/linux/latest/C/ident/getname>(filename
> <https://elixir.bootlin.com/linux/latest/C/ident/filename>), argv
> <https://elixir.bootlin.com/linux/latest/C/ident/argv>, envp
> <https://elixir.bootlin.com/linux/latest/C/ident/envp>);}
> 
> So far no problems, thus, once there, I would like to have the exit code of
> this function. This is where I have some difficulties. I first tried with a
> FinishingBreakpoint as follows. Thus, it never each the "normal finish" and
> always ends up in "out of scope", despite the fact it is placed at the
> correct place (in an assembly file - entry-common.S). When I'm manually
> stepping thru, I got there.
> class ExecveBreakpoint(gdb.Breakpoint):
> 
>     def stop(self):
>         MyFinishBreakpoint(gdb.selected_frame())
> 
> 
> class MyFinishBreakpoint (gdb.FinishBreakpoint):
>     def stop (self):
>         print ("normal finish")
> 
>     def out_of_scope ():
>         print ("abnormal finish")
> 
> 
> 
> 
> I was wondering if this was because there were no ret instruction at the
> end of the block ?

Not sure, I would have to play with it and debug it to know.

> Anyway, I've tried to get around by computing the last
> instruction before continuing in assembly
>         pc = gdb.selected_frame().pc()
>         addr = gdb.block_for_pc(pc).end
>         addr = addr - \
>             gdb.selected_frame().architecture().disassemble(addr)[0][
> 'length']
> 
>         TestBp(f"*0x{addr:02x}", temporary=True)
> 
> class TestBp(gdb.Breakpoint):
>   def stop(self):
>   print(f"count = {self.hit_count}")
>         #self.delete()# <- this crashed gdb

I think this is kind of expected (although not nice).  The doc says:

    You should not alter the execution state of the inferior (i.e.,
    step, next, etc.), alter the current frame context (i.e., change the
    current active frame), or alter, add or delete any breakpoint. As a
    general rule, you should not alter any data within GDB or the
    inferior at this time.

>         return False
> 
> It happens that even though I made it temporary, it is never deleted.
> Furthermore, the hit_count is always 0 even though I've got multiple print
> calls on the console.

I think it's because when the stop method returns true, the breakpoint
is considered to not be hit.  It's the same as if you have a conditional
breakpoint whose condition evaluates to false.  The breakpoint is
therefore not deleted, the hit count not incremented.

Unfortunately, I don't know of a way to do tell GDB that you consider
the breakpoint hit, but don't want to stop the inferior.  That would be
useful to do some kind of internal accounting but not cause a stop.

Simon

      reply	other threads:[~2023-02-21  2:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-15 13:22 Ena Irtnanoelec
2023-02-21  2:51 ` Simon Marchi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=25f6f329-8f75-4f05-c269-3000c0a4dde6@simark.ca \
    --to=simark@simark.ca \
    --cc=ena.irtnanoelec@gmail.com \
    --cc=gdb@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).