How to modify a variable's value via address? - lldb

I'm a newbie to LLDB. I know it's pretty easy to set a variable's value if we know it's name, e.g.:
p $foo = 5
But how can I do that if I only know it's address? I have read through the LLDB official page but found nothing help. Maybe I have miss something.
any help is welcomed.

The expression command (which p is an alias for) can evaluate any C/C++ expression - so just use C syntax to do what you want.
(lldb) p c
(int) $0 = 10
(lldb) p &c
(int *) $1 = 0x00007fff5fbff9ac
(lldb) p *(int*)0x00007fff5fbff9ac = 5
(int) $3 = 5
(lldb) p c
(int) $4 = 5
(lldb)

Related

Reading multiple lines of multiple numbers(the amount of numbers are not specified) in C

I need to make a program that takes two lines of input, each one composed of an unspecified amount of numbers, and store each one in a separate vector(int*).
It's not allowed to use a string to store the input and then convert it to int.
It has to work in a linux enviroment, actually I've solved it for Windows with the following code:
int t,s1=0,s2=0;
char tx;
int* a=(int*)malloc(0);
int* b=(int*)malloc(0);
scanf("%i%c",&t,&tx);
while(tx!='\n')
{
a=(int*)realloc(a,sizeof(int)*(s1+1));
a[s1++]=t;
scanf("%i%c",&t,&tx);
}
a=(int*)realloc(a,sizeof(int)*(s1+1));
a[s1++]=t;
scanf("%i%c",&t,&tx);
while(tx!='\n')
{
b=(int*)realloc(b,sizeof(int)*(s2+1));
b[s2++]=t;
scanf("%i%c",&t,&tx);
}
b=(int*)realloc(b,sizeof(int)*(s2+1));
b[s2++]=t;
I've tested it on linux and, at least for me, It doesn't work.
I would really appreciate your help, thank you very much!
I tried your program on linux and for me, it worked. I used the following input file:
1 2 3
4 5
I ran it in gdb and after the final statement, the debugger gave the following values:
(gdb) print s1
$4 = 3
(gdb) print s2
$5 = 2
(gdb) print a[0]
$6 = 1
(gdb) print a[1]
$7 = 2
(gdb) print a[2]
$8 = 3
(gdb) print b[0]
$9 = 4
(gdb) print b[1]
$10 = 5
(gdb)
But the code probably will fail, if there are trailing blanks in the input data.
You also do not check the return value of scanf(), so it might enter an endless loop, if scanf() fails, e.g. due to early EOF.
You might also encounter problems, if you feed your program input data from a file using windows line endings (\r\n).
I also would advice to use a do {} while (tx!='\n') loop, as it eliminates duplicate code.

Statement with no effect in C

Why does it writte me "statement with no effect use" in the line where there is the "for".
r=7;
for (r=7;r<=n1;r+10)
printf("%d\n",r);
Because r+7 is no assignment.For assignment to take place,you have to write
something like
r = r+7 or r += 7
r+10 does not modify r so if n1 happens to be greater than on equal to 7 at the start of the loop, the loop will be infinite, which is probably not what you were trying to achieve. Did you mean r += 10, or in other words, increment r by 10?

Cannot access memory at address?

I observed a strange behavior from gdb. Please assist me to identify this.
Below is affected snippet.
Note: Below for loop should not execute more than two times as max value of totNumEntries is 2.
totNumEntries = callLegPtr->activeSpContextPtr->directMediaPtr->numEntries;
for (index = 0; index < totNumEntries; index++)
{
.......
}
Printing values via gdb:
(gdb) p index
$79 = 35933
(gdb) p totNumEntries
$80 = 65535
(gdb) p callLegPtr->activeSpContextPtr->directMediaPtr->numEntries
Cannot access memory at address 0x53369955
As per gdb print, 0x53369955 is out of bond and cannot be accessible but still i can see "totNumEntries" having value 65535 because of that reason above for loop went into to many iterations (35933) and our process declared time out.
Type of totNumEntries is unsigned short (Max value is 65535).
My concern is what exactly the meaning of "Cannot access memory at address 0x53369955". When 0x53369955 is not accessible how come "totNumEntries" taking large value which maximum range for unsigned int type. It is very important for me to understand, please let me know if you need any further information.
Regards,
Shahid Hussain

dereferencing a register gdb

So i am trying to debug some code and all i have is the executable. I know that a register contains the address of what need to know. Is there a way I could print out the hex values from the start of that address to a given length?
things I have tried:
x/s $ebp
p (char) ($ebp)
p (char) (*ebp >> 4 )
p (char)*(%ebp 4 )
p $($ebp)
p $(%ebp + $0x1)
Does normal pointer arithmetic not work with registers? What does it mean when it says "the history is empty
The x command will be the easiest way to display values in memory. Try, for instance:
(gdb) x/32b $ebp
If this doesn't work (especially if you get an error that "value can't be converted to integer"), you're probably debugging a 64-bit executable. x86-64 registers have different names; use $rbp instead.
The characters after the slash in the x command control how many values are displayed, and what format is used. x/s will attempt to read a string from that address, for instance. If you don't use anything, gdb will use whatever you last used.
While it isn't strictly necessary to answer your question, I've fixed up some of the other commands you were trying to run:
p *((char *) $ebp) <- treat $ebp as a character pointer and display what it points to
p *((char *) $ebp + 4) <- with an offset
p ((char *) $ebp)[4] <- same thing as above, except using array syntax
The $ character is only needed when referring to registers, or to gdb variables. You don't need it for anything else.

Weird C Syntax 1["ABCDE"]?

Can anyone please help me understand the following code:
int d = 4;
printf(&d["Hay!Hello %c\n"], &4["BuddyWorld"]); // Hello (some address in char)
printf(&d["Hay!Hello %c\n"], 4["BuddyWorld"]); // Hello y
printf(&d["Hay!Hello %s\n"], 4["BuddyWorld"]); // Segmentation fault
printf(&d["Hay!Hello %s\n"], &4["BuddyWorld"]); // Hello yWorld
printf("d[Hay!Hello %s\n"], &4["BuddyWorld"]);
/* warning: passing argument 1 of 'printf' makes pointer
from integer without a cast */
What is exactly the d[] or &d[] operator? (d is an integer)
Why does &4["BuddyWorld"] and 4[BuddyWorld] yields to different values? ('W' and 'y' respectively)
When you write something like a[i], it gets expanded into *(a + i). (We say a[i] is syntactic sugar for *(a + i)).
Addition is commutative, so "BuddyWorld"[4] = *("BuddyWorld" + 4) = *(4 + "BuddyWorld") = 4["BuddyWorld"], where "BuddyWorld" implicitly stands for its address in memory.
Subscripting in C is weird. a[b] is turned into *(a + b) (the thing at the address obtained by adding b to the pointer a). However, since a + b == b + a, it works the other way around as well (a[b] == b[a]). That's all that's going on. In particular, "HelloWorld" is really a pointer to a character array that stores the characters of the string.
d["Hay!Hello %c\n"] is the same as "Hay!Hello %c\n"[d].
And the & operator gives the address of the pointed content

Resources