Re: [lldb-dev] how to set a watchpoint on an "unsigned short" parameter ?

2019-02-15 Thread Ted Woodward via lldb-dev
I don't see anything on this line that would change x:
   fprintf(stderr, "some string %lu %c\n",
r==(void*)0UL)? 255UL : A_MACRO(r),
x? '0' : '1'
  );

I suggest you set a breakpoint on the line and a breakpoint on the next line. 
Verify that x is the wrong value using "frame variable x" when you hit the 
second breakpoint.
Make sure this breakpoint is on a source line that does something, not in the 
middle of the function return.

If the value changes, disassemble the line and set a breakpoint on the call 
instruction for printf. See if the value changes when you hit that breakpoint.
If it does, look at the assembly and see if anything is changing the data at 
the address of x. You can get the address with "frame variable ".
If the call to printf is changing the value of x, you've probably got stack 
corruption going on.

---

Clang is supported on Linux:

>uname
Linux
>which clang
/usr/bin/clang

So you aren't forced to use gcc just because you're targeting Linux.

---

watchpoint set syntax is as follows:

(lldb) help watch set
 Commands for setting a watchpoint.

Syntax: watchpoint set  []


If you're setting it on a variable and you have good DWARF info, you don't need 
to specify a size. From my example:
> (lldb) w s v i
> Watchpoint created: Watchpoint 1: addr = 0x0410eec6 size = 2 state = 
> enabled type = w



-Original Message-
From: Jason Vas Dias  
Sent: Friday, February 15, 2019 10:33 AM
To: Ted Woodward 
Cc: LLDB 
Subject: [EXT] Re: [lldb-dev] how to set a watchpoint on an "unsigned short" 
parameter ?

Good day Ted -

Thanks for responding - but I did try that one:

  (lldb) wa s v x
  error: Watchpoint creation failed (addr=0x, size=0, 
variable
expression='x').
  error: cannot set a watchpoint with watch_size of 0
  (lldb) wa s -s 2 v x
  invalid command 'watchpoint set -s'.
  (lldb) wa s v -s 2 x
  error: Watchpoint creation failed (addr=0x, size=0, 
variable
expression='x').
  error: cannot set a watchpoint with watch_size of 0

  I can't seem to get lldb to recognize the '-s' / '--size' options no matter
  where I put them .  And the documentation, such as it is,
  (on https://lldb.llvm.org/lldb-gdb.html), is very vague and incomplete.

  I guess my problems are  because I am  compiling with GCC , and trying
  to debug with LLDB .
  But since the program I am debugging is targetted mainly for the Linux 
platform,
  (I am just using MacOSX for testing) I wanted to compile with GCC .

  I guess it is not possible to debug GCC compiled programs with LLDB ?

  The problem I am trying to track down is stack corruption caused by
  fprintf() :

void f ( void *r, unsigned short x )
{  ...
   fprintf(stderr, "some string %lu %c\n",
r==(void*)0UL)? 255UL : A_MACRO(r),
x? '0' : '1'
  );
 // after this fprintf, the value of x changes from 12 to 8630 .
 // why ? It would be nice to be able to use LLDB to find out,
//  but this does not work.
}

 I've just had to comment out the fprintf , so the problem does not occur.

 Coming from a background of using GDB for the past 25 years, I find
 this lack of watchpoint support in LLDB very difficult to accept.

Thanks & Best Regards,
Jason






On 15/02/2019, Ted Woodward  wrote:
> "w s v x" would be the command you want.
>
>
> (lldb) b f
> Breakpoint 1: where = watch`f + 12 at watch.c:5:4, address = 
> 0x50ec
> (lldb) r
> hexagon-sim INFO: The rev_id used in the simulation is 0x4060
> (v60a_512)
> hexagon-sim INFO: Setting up debug server on port 57824 Process 1 
> launched: '/usr2/tedwood/lldb_test/watch' (hexagon) Process 1 stopped
> * thread #1, name = 'T1', stop reason = breakpoint 1.1
> frame #0: 0x50ec watch`f(i=2) at watch.c:5:4
>2
>3unsigned short f(unsigned short i)
>4{
> -> 5  i++;
>6  return i;
>7}
>8
> (lldb) w s v i
> Watchpoint created: Watchpoint 1: addr = 0x0410eec6 size = 2 state = 
> enabled type = w
> declare @ '/usr2/tedwood/lldb_test/watch.c:3'
> watchpoint spec = 'i'
> new value: 2
> (lldb) c
> Process 1 resuming
>
> Watchpoint 1 hit:
> old value: 2
> new value: 3
> Process 1 stopped
> * thread #1, name = 'T1', stop reason = watchpoint 1
> frame #0: 0x50f8 watch`f(i=3) at watch.c:6:10
>3unsigned short f(unsigned short i)
>4{
>5  i++;
> -> 6  return i;
>7}
>8
>9int main(int argc, char **argv)
>
>
>
> -Original Message-
> From: lldb-dev  On Behalf Of Jason 
> Vas Dias via lldb-dev
> Sent: Thursday, February 14, 2019 1:28 PM

Re: [lldb-dev] how to set a watchpoint on an "unsigned short" parameter ?

2019-02-15 Thread Ted Woodward via lldb-dev
"w s v x" would be the command you want.


(lldb) b f
Breakpoint 1: where = watch`f + 12 at watch.c:5:4, address = 0x50ec
(lldb) r
hexagon-sim INFO: The rev_id used in the simulation is 0x4060 (v60a_512)
hexagon-sim INFO: Setting up debug server on port 57824
Process 1 launched: '/usr2/tedwood/lldb_test/watch' (hexagon)
Process 1 stopped
* thread #1, name = 'T1', stop reason = breakpoint 1.1
frame #0: 0x50ec watch`f(i=2) at watch.c:5:4
   2   
   3unsigned short f(unsigned short i)
   4{
-> 5  i++;
   6  return i;
   7}
   8   
(lldb) w s v i
Watchpoint created: Watchpoint 1: addr = 0x0410eec6 size = 2 state = enabled 
type = w
declare @ '/usr2/tedwood/lldb_test/watch.c:3'
watchpoint spec = 'i'
new value: 2
(lldb) c
Process 1 resuming

Watchpoint 1 hit:
old value: 2
new value: 3
Process 1 stopped
* thread #1, name = 'T1', stop reason = watchpoint 1
frame #0: 0x50f8 watch`f(i=3) at watch.c:6:10
   3unsigned short f(unsigned short i)
   4{
   5  i++;
-> 6  return i;
   7}
   8   
   9int main(int argc, char **argv)



-Original Message-
From: lldb-dev  On Behalf Of Jason Vas Dias 
via lldb-dev
Sent: Thursday, February 14, 2019 1:28 PM
To: lldb-dev@lists.llvm.org
Subject: [lldb-dev] how to set a watchpoint on an "unsigned short" parameter ?

Good day -

  I'd be most grateful if anyone could enlighten me as to how
  to set a watchpoint on an unsigned short parameter variable
  in lldb .

  I am trying to follow the instructions at :
https://lldb.llvm.org/lldb-gdb.html
  but they do not work to set watchpoints.

  There seems to be no other documentation for LLDB commands -
  or if anyone knows of any , please let me know.

  I have a function like :
void f ( unsigned short x )
{  }

  With the debugger stopped inside f, I have tried:

   (lldb)  p 
   (uint16_t *) $3 = 0x0001001122c0
   (lldb) wa s v -s 2 -w write 0x0001001122c0
   error: no variable named '0x0001001122c0' found in this frame
   (lldb) wa s v -s 2 -w write x
   error: Watchpoint creation failed (addr=0x, size=0, variable
   expression='x').
   error: cannot set a watchpoint with watch_size of 0
   (lldb) wa s e -s 2 -w write 0x0001001122c0
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write 0x0001001122c0
   (lldb) wa s e -s 2 -w write *0x0001001122c0
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write *0x0001001122c0
   (lldb) wa s e -s 2 -w write ((unsigned short*)0x0001001122c0)
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write ((unsigned short*)0x0001001122c0)
   (lldb) wa s v -s 2 -w write 
   error: 'x' doesn't have a valid address
   # ^- this error is really strange, particularly as I can do:
   (lldb) p 
   (uint16_t *) $5 = 0x0001001122c0

  It seems to me lldb's implementation of watch points is fundamentally broken -
  there is no way I've been able to get it to work .

  Unfortunately, I have to use MacOSX, so gdb is not available.

  Please, can anyone suggest how to successfully set a watchpoint on
  a parameter (stack) located variable value with lldb ?
  It does not seem to be possible.

  My next step, if no answers to this mail, would be to analyse the LLDB
  source code to see if I can figure out how watchpoints are meant to
  be set, seeing as there is no reference documentation for LLDB commands,
  either installed as manual pages or online.  This to me makes LLDB unsuitable
  for production use, but unforunately I have to use it (I need to debug under
  MacOSX 10,14.3 ).

   The help output for is of no use either:
   (lldb) help watch set
   "Syntax: watchpoint set  []
The following subcommands are supported:
  expression -- Set a watchpoint on an address by supplying an expression. 
Use the
  '-w' option to specify the type of watchpoint and the '-s'
option to specify the
  byte size to watch for.
"
The above statement is provably false:
 (lldb) wa s v -s 2 x
 error: Watchpoint creation failed (addr=0x, size=0,
   variable expression='x').
 error: cannot set a watchpoint with watch_size of 0
 # maybe the -s option goes after the 'set' ? no:
(lldb) wa s -s 2 v reader_id
invalid command 'watchpoint set -s'.

All attempts to
 "Use the '-w' option to specify the type of watchpoint and the '-s' option 
to
  specify the byte size to watch for.
 "
fail,  so there must be alot missing from the help description.

The help for the variable syntax is also vague, and provably false :
 "variable   -- Set a watchpoint on a variable. Use the '-w'
option to specify the type
 of watchpoint and the '-s' option to specify the byte size 
to watch for.
If no '-w' option is specified, it defaults to 

[lldb-dev] how to set a watchpoint on an "unsigned short" parameter ?

2019-02-14 Thread Jason Vas Dias via lldb-dev
Good day -

  I'd be most grateful if anyone could enlighten me as to how
  to set a watchpoint on an unsigned short parameter variable
  in lldb .

  I am trying to follow the instructions at :
https://lldb.llvm.org/lldb-gdb.html
  but they do not work to set watchpoints.

  There seems to be no other documentation for LLDB commands -
  or if anyone knows of any , please let me know.

  I have a function like :
void f ( unsigned short x )
{  }

  With the debugger stopped inside f, I have tried:

   (lldb)  p 
   (uint16_t *) $3 = 0x0001001122c0
   (lldb) wa s v -s 2 -w write 0x0001001122c0
   error: no variable named '0x0001001122c0' found in this frame
   (lldb) wa s v -s 2 -w write x
   error: Watchpoint creation failed (addr=0x, size=0,
variable
   expression='x').
   error: cannot set a watchpoint with watch_size of 0
   (lldb) wa s e -s 2 -w write 0x0001001122c0
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write 0x0001001122c0
   (lldb) wa s e -s 2 -w write *0x0001001122c0
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write *0x0001001122c0
   (lldb) wa s e -s 2 -w write ((unsigned short*)0x0001001122c0)
   error: expression evaluation of address to watch failed
   expression evaluated: -s 2 -w write ((unsigned short*)0x0001001122c0)
   (lldb) wa s v -s 2 -w write 
   error: 'x' doesn't have a valid address
   # ^- this error is really strange, particularly as I can do:
   (lldb) p 
   (uint16_t *) $5 = 0x0001001122c0

  It seems to me lldb's implementation of watch points is fundamentally broken -
  there is no way I've been able to get it to work .

  Unfortunately, I have to use MacOSX, so gdb is not available.

  Please, can anyone suggest how to successfully set a watchpoint on
  a parameter (stack) located variable value with lldb ?
  It does not seem to be possible.

  My next step, if no answers to this mail, would be to analyse the LLDB
  source code to see if I can figure out how watchpoints are meant to
  be set, seeing as there is no reference documentation for LLDB commands,
  either installed as manual pages or online.  This to me makes LLDB unsuitable
  for production use, but unforunately I have to use it (I need to debug under
  MacOSX 10,14.3 ).

   The help output for is of no use either:
   (lldb) help watch set
   "Syntax: watchpoint set  []
The following subcommands are supported:
  expression -- Set a watchpoint on an address by supplying an
expression. Use the
  '-w' option to specify the type of watchpoint and the '-s'
option to specify the
  byte size to watch for.
"
The above statement is provably false:
 (lldb) wa s v -s 2 x
 error: Watchpoint creation failed (addr=0x, size=0,
   variable expression='x').
 error: cannot set a watchpoint with watch_size of 0
 # maybe the -s option goes after the 'set' ? no:
(lldb) wa s -s 2 v reader_id
invalid command 'watchpoint set -s'.

All attempts to
 "Use the '-w' option to specify the type of watchpoint and the
'-s' option to
  specify the byte size to watch for.
 "
fail,  so there must be alot missing from the help description.

The help for the variable syntax is also vague, and provably false :
 "variable   -- Set a watchpoint on a variable. Use the '-w'
option to specify the type
 of watchpoint and the '-s' option to specify the
byte size to watch for.
If no '-w' option is specified, it defaults to
write. If no '-s' option is
specified, it defaults to the variable's byte
size. Note that there are
limited hardware resources for watchpoints. If
watchpoint setting
fails, consider disable/delete existing ones to
free up resources.

  (lldb) wa s v x
  error: Watchpoint creation failed (addr=0x,
size=0, variable
expression='x').
  error: cannot set a watchpoint with watch_size of 0
  (lldb) wa s -s 2 v x
  invalid command 'watchpoint set -s'.
  (lldb) wa s v -s 2 x
  error: Watchpoint creation failed (addr=0x,
size=0, variable
expression='x').
  error: cannot set a watchpoint with watch_size of 0

   So lldb's watch command can neither get the size from a '-s' option, nor can
   it get the size from sizeof(variable) . How then is one meant to
set the size ?

   Obviously, lldb's watchpoint implementation needs much further work - has
   anyone been able to get it to do anything useful ? If so, please
let me know how,
   and what.

   If there is any reference documentation for lldb, besides the false
and misleading
   'https://lldb.llvm.org/lldb-gdb.html', which actually describes in detail and
   correctly the syntax of every lldb command, please let me know.

   Any ideas