Re: [V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-09-14 Thread Athira Rajeev



> On 14-Sep-2023, at 11:49 PM, Adrian Hunter  wrote:
> 
> On 7/09/23 19:45, Athira Rajeev wrote:
>> Update "struct dso" to include new member "text_end".
>> This new field will represent the offset for end of text
>> section for a dso. For elf, this value is derived as:
>> sh_size (Size of section in byes) + sh_offset (Section file
>> offst) of the elf header for text.
>> 
>> For bfd, this value is derived as:
>> 1. For PE file,
>> section->size + ( section->vma - dso->text_offset)
>> 2. Other cases:
>> section->filepos (file position) + section->size (size of
>> section)
>> 
>> To resolve the address from a sample, perf looks at the
>> DSO maps. In case of address from a kernel module, there
>> were some address found to be not resolved. This was
>> observed while running perf test for "Object code reading".
>> Though the ip falls beteen the start address of the loaded
>> module (perf map->start ) and end address ( perf map->end),
>> it was unresolved.
>> 
>> Example:
>> 
>>Reading object code for memory address: 0xc00807f0142c
>>File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
>>On file address is: 0x1114cc
>>Objdump command is: objdump -z -d --start-address=0x11142c 
>> --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
>>objdump read too few bytes: 128
>>test child finished with -1
>> 
>> Here, module is loaded at:
>># cat /proc/modules | grep xfs
>>xfs 2228224 3 - Live 0xc00807d0
>> 
>> From objdump for xfs module, text section is:
>>text 0010f7bc    00a0 2**4
>> 
>> Here the offset for 0xc00807f0142c ie  0x112074 falls out
>> .text section which is up to 0x10f7bc.
>> 
>> In this case for module, the address 0xc00807e11fd4 is pointing
>> to stub instructions. This address range represents the module stubs
>> which is allocated on module load and hence is not part of DSO offset.
>> 
>> To identify such  address, which falls out of text
>> section and within module end, added the new field "text_end" to
>> "struct dso".
>> 
>> Reported-by: Disha Goel 
>> Signed-off-by: Athira Rajeev 
> 
> Reviewed-by: Adrian Hunter 

Hi Adrian

Thanks for the review

> 
>> ---
>> Changelog:
>> v1 -> v2:
>> Added text_end for bfd also by updating dso__load_bfd_symbols
>> as suggested by Adrian.
>> 
>> tools/perf/util/dso.h| 1 +
>> tools/perf/util/symbol-elf.c | 4 +++-
>> tools/perf/util/symbol.c | 2 ++
>> 3 files changed, 6 insertions(+), 1 deletion(-)
>> 
>> diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
>> index b41c9782c754..70fe0fe69bef 100644
>> --- a/tools/perf/util/dso.h
>> +++ b/tools/perf/util/dso.h
>> @@ -181,6 +181,7 @@ struct dso {
>> u8  rel;
>> struct build_id  bid;
>> u64  text_offset;
>> + u64  text_end;
>> const char  *short_name;
>> const char  *long_name;
>> u16  long_name_len;
>> diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
>> index 95e99c332d7e..9e7eeaf616b8 100644
>> --- a/tools/perf/util/symbol-elf.c
>> +++ b/tools/perf/util/symbol-elf.c
>> @@ -1514,8 +1514,10 @@ dso__load_sym_internal(struct dso *dso, struct map 
>> *map, struct symsrc *syms_ss,
>> }
>> 
>> if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
>> - ".text", NULL))
>> + ".text", NULL)) {
>> dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
>> + dso->text_end = tshdr.sh_offset + tshdr.sh_size;
>> + }
>> 
>> if (runtime_ss->opdsec)
>> opddata = elf_rawdata(runtime_ss->opdsec, NULL);
>> diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
>> index 3f36675b7c8f..f25e4e62cf25 100644
>> --- a/tools/perf/util/symbol.c
>> +++ b/tools/perf/util/symbol.c
>> @@ -1733,8 +1733,10 @@ int dso__load_bfd_symbols(struct dso *dso, const char 
>> *debugfile)
>> /* PE symbols can only have 4 bytes, so use .text high bits */
>> dso->text_offset = section->vma - (u32)section->vma;
>> dso->text_offset += (u32)bfd_asymbol_value(symbols[i]);
>> + dso->text_end = (section->vma - dso->text_offset) + section->size;
>> } else {
>> dso->text_offset = section->vma - section->filepos;
>> + dso->text_end = section->filepos + section->size;
>> }
>> }




Re: [V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-09-14 Thread Adrian Hunter
On 7/09/23 19:45, Athira Rajeev wrote:
> Update "struct dso" to include new member "text_end".
> This new field will represent the offset for end of text
> section for a dso. For elf, this value is derived as:
> sh_size (Size of section in byes) + sh_offset (Section file
> offst) of the elf header for text.
> 
> For bfd, this value is derived as:
> 1. For PE file,
> section->size + ( section->vma - dso->text_offset)
> 2. Other cases:
> section->filepos (file position) + section->size (size of
> section)
> 
> To resolve the address from a sample, perf looks at the
> DSO maps. In case of address from a kernel module, there
> were some address found to be not resolved. This was
> observed while running perf test for "Object code reading".
> Though the ip falls beteen the start address of the loaded
> module (perf map->start ) and end address ( perf map->end),
> it was unresolved.
> 
> Example:
> 
> Reading object code for memory address: 0xc00807f0142c
> File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
> On file address is: 0x1114cc
> Objdump command is: objdump -z -d --start-address=0x11142c 
> --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
> objdump read too few bytes: 128
> test child finished with -1
> 
> Here, module is loaded at:
> # cat /proc/modules | grep xfs
> xfs 2228224 3 - Live 0xc00807d0
> 
> From objdump for xfs module, text section is:
> text 0010f7bc    00a0 2**4
> 
> Here the offset for 0xc00807f0142c ie  0x112074 falls out
> .text section which is up to 0x10f7bc.
> 
> In this case for module, the address 0xc00807e11fd4 is pointing
> to stub instructions. This address range represents the module stubs
> which is allocated on module load and hence is not part of DSO offset.
> 
> To identify such  address, which falls out of text
> section and within module end, added the new field "text_end" to
> "struct dso".
> 
> Reported-by: Disha Goel 
> Signed-off-by: Athira Rajeev 

Reviewed-by: Adrian Hunter 

> ---
> Changelog:
>  v1 -> v2:
>  Added text_end for bfd also by updating dso__load_bfd_symbols
>  as suggested by Adrian.
> 
>  tools/perf/util/dso.h| 1 +
>  tools/perf/util/symbol-elf.c | 4 +++-
>  tools/perf/util/symbol.c | 2 ++
>  3 files changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
> index b41c9782c754..70fe0fe69bef 100644
> --- a/tools/perf/util/dso.h
> +++ b/tools/perf/util/dso.h
> @@ -181,6 +181,7 @@ struct dso {
>   u8   rel;
>   struct build_id  bid;
>   u64  text_offset;
> + u64  text_end;
>   const char   *short_name;
>   const char   *long_name;
>   u16  long_name_len;
> diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
> index 95e99c332d7e..9e7eeaf616b8 100644
> --- a/tools/perf/util/symbol-elf.c
> +++ b/tools/perf/util/symbol-elf.c
> @@ -1514,8 +1514,10 @@ dso__load_sym_internal(struct dso *dso, struct map 
> *map, struct symsrc *syms_ss,
>   }
>  
>   if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
> - ".text", NULL))
> + ".text", NULL)) {
>   dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
> + dso->text_end = tshdr.sh_offset + tshdr.sh_size;
> + }
>  
>   if (runtime_ss->opdsec)
>   opddata = elf_rawdata(runtime_ss->opdsec, NULL);
> diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> index 3f36675b7c8f..f25e4e62cf25 100644
> --- a/tools/perf/util/symbol.c
> +++ b/tools/perf/util/symbol.c
> @@ -1733,8 +1733,10 @@ int dso__load_bfd_symbols(struct dso *dso, const char 
> *debugfile)
>   /* PE symbols can only have 4 bytes, so use .text high 
> bits */
>   dso->text_offset = section->vma - (u32)section->vma;
>   dso->text_offset += (u32)bfd_asymbol_value(symbols[i]);
> + dso->text_end = (section->vma - dso->text_offset) + 
> section->size;
>   } else {
>   dso->text_offset = section->vma - section->filepos;
> + dso->text_end = section->filepos + section->size;
>   }
>   }
>  



[V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-09-07 Thread Athira Rajeev
Update "struct dso" to include new member "text_end".
This new field will represent the offset for end of text
section for a dso. For elf, this value is derived as:
sh_size (Size of section in byes) + sh_offset (Section file
offst) of the elf header for text.

For bfd, this value is derived as:
1. For PE file,
section->size + ( section->vma - dso->text_offset)
2. Other cases:
section->filepos (file position) + section->size (size of
section)

To resolve the address from a sample, perf looks at the
DSO maps. In case of address from a kernel module, there
were some address found to be not resolved. This was
observed while running perf test for "Object code reading".
Though the ip falls beteen the start address of the loaded
module (perf map->start ) and end address ( perf map->end),
it was unresolved.

Example:

Reading object code for memory address: 0xc00807f0142c
File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
On file address is: 0x1114cc
Objdump command is: objdump -z -d --start-address=0x11142c 
--stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
objdump read too few bytes: 128
test child finished with -1

Here, module is loaded at:
# cat /proc/modules | grep xfs
xfs 2228224 3 - Live 0xc00807d0

>From objdump for xfs module, text section is:
text 0010f7bc    00a0 2**4

Here the offset for 0xc00807f0142c ie  0x112074 falls out
.text section which is up to 0x10f7bc.

In this case for module, the address 0xc00807e11fd4 is pointing
to stub instructions. This address range represents the module stubs
which is allocated on module load and hence is not part of DSO offset.

To identify such  address, which falls out of text
section and within module end, added the new field "text_end" to
"struct dso".

Reported-by: Disha Goel 
Signed-off-by: Athira Rajeev 
---
Changelog:
 v1 -> v2:
 Added text_end for bfd also by updating dso__load_bfd_symbols
 as suggested by Adrian.

 tools/perf/util/dso.h| 1 +
 tools/perf/util/symbol-elf.c | 4 +++-
 tools/perf/util/symbol.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index b41c9782c754..70fe0fe69bef 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -181,6 +181,7 @@ struct dso {
u8   rel;
struct build_id  bid;
u64  text_offset;
+   u64  text_end;
const char   *short_name;
const char   *long_name;
u16  long_name_len;
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 95e99c332d7e..9e7eeaf616b8 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1514,8 +1514,10 @@ dso__load_sym_internal(struct dso *dso, struct map *map, 
struct symsrc *syms_ss,
}
 
if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
-   ".text", NULL))
+   ".text", NULL)) {
dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
+   dso->text_end = tshdr.sh_offset + tshdr.sh_size;
+   }
 
if (runtime_ss->opdsec)
opddata = elf_rawdata(runtime_ss->opdsec, NULL);
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 3f36675b7c8f..f25e4e62cf25 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -1733,8 +1733,10 @@ int dso__load_bfd_symbols(struct dso *dso, const char 
*debugfile)
/* PE symbols can only have 4 bytes, so use .text high 
bits */
dso->text_offset = section->vma - (u32)section->vma;
dso->text_offset += (u32)bfd_asymbol_value(symbols[i]);
+   dso->text_end = (section->vma - dso->text_offset) + 
section->size;
} else {
dso->text_offset = section->vma - section->filepos;
+   dso->text_end = section->filepos + section->size;
}
}
 
-- 
2.31.1



Re: [PATCH V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-09-07 Thread Athira Rajeev



> On 18-Aug-2023, at 12:07 PM, Adrian Hunter  wrote:
> 
> On 17/08/23 20:18, Athira Rajeev wrote:
>> Update "struct dso" to include new member "text_end".
>> This new field will represent the offset for end of text
>> section for a dso. This value is derived as:
>> sh_size (Size of section in byes) + sh_offset (Section file
>> offst) of the elf header for text.
>> 
>> To resolve the address from a sample, perf looks at the
>> DSO maps. In case of address from a kernel module, there
>> were some address found to be not resolved. This was
>> observed while running perf test for "Object code reading".
>> Though the ip falls beteen the start address of the loaded
>> module (perf map->start ) and end address ( perf map->end),
>> it was unresolved.
>> 
>> Example:
>> 
>>Reading object code for memory address: 0xc00807f0142c
>>File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
>>On file address is: 0x1114cc
>>Objdump command is: objdump -z -d --start-address=0x11142c 
>> --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
>>objdump read too few bytes: 128
>>test child finished with -1
>> 
>> Here, module is loaded at:
>># cat /proc/modules | grep xfs
>>xfs 2228224 3 - Live 0xc00807d0
>> 
>> From objdump for xfs module, text section is:
>>text 0010f7bc    00a0 2**4
>> 
>> Here the offset for 0xc00807f0142c ie  0x112074 falls out
>> .text section which is up to 0x10f7bc.
>> 
>> In this case for module, the address 0xc00807e11fd4 is pointing
>> to stub instructions. This address range represents the module stubs
>> which is allocated on module load and hence is not part of DSO offset.
>> 
>> To identify such  address, which falls out of text
>> section and within module end, added the new field "text_end" to
>> "struct dso".
>> 
>> Reported-by: Disha Goel 
>> Signed-off-by: Athira Rajeev 
>> ---
>> Changelog:
>>   v1 -> v2:
>>   Changed commit message to explain need for "text_end"
>> 
>> tools/perf/util/dso.h| 1 +
>> tools/perf/util/symbol-elf.c | 4 +++-
>> 2 files changed, 4 insertions(+), 1 deletion(-)
>> 
>> diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
>> index b41c9782c754..70fe0fe69bef 100644
>> --- a/tools/perf/util/dso.h
>> +++ b/tools/perf/util/dso.h
>> @@ -181,6 +181,7 @@ struct dso {
>> u8  rel;
>> struct build_id  bid;
>> u64  text_offset;
>> + u64  text_end;
>> const char  *short_name;
>> const char  *long_name;
>> u16  long_name_len;
>> diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
>> index 8bd466d1c2bd..252d26a59e64 100644
>> --- a/tools/perf/util/symbol-elf.c
>> +++ b/tools/perf/util/symbol-elf.c
>> @@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map 
>> *map, struct symsrc *syms_ss,
>> }
>> 
>> if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
>> - ".text", NULL))
>> + ".text", NULL)) {
>> dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
>> + dso->text_end = tshdr.sh_offset + tshdr.sh_size;
>> + }
>> 
>> if (runtime_ss->opdsec)
>> opddata = elf_rawdata(runtime_ss->opdsec, NULL);
> 
> Should probably amend dso__load_bfd_symbols() also

Hi Adrian,

Thanks for checking. Will address this in V2 

Thanks
Athira




Re: [PATCH V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-08-18 Thread Adrian Hunter
On 17/08/23 20:18, Athira Rajeev wrote:
> Update "struct dso" to include new member "text_end".
> This new field will represent the offset for end of text
> section for a dso. This value is derived as:
> sh_size (Size of section in byes) + sh_offset (Section file
> offst) of the elf header for text.
> 
> To resolve the address from a sample, perf looks at the
> DSO maps. In case of address from a kernel module, there
> were some address found to be not resolved. This was
> observed while running perf test for "Object code reading".
> Though the ip falls beteen the start address of the loaded
> module (perf map->start ) and end address ( perf map->end),
> it was unresolved.
> 
> Example:
> 
> Reading object code for memory address: 0xc00807f0142c
> File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
> On file address is: 0x1114cc
> Objdump command is: objdump -z -d --start-address=0x11142c 
> --stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
> objdump read too few bytes: 128
> test child finished with -1
> 
> Here, module is loaded at:
> # cat /proc/modules | grep xfs
> xfs 2228224 3 - Live 0xc00807d0
> 
> From objdump for xfs module, text section is:
> text 0010f7bc    00a0 2**4
> 
> Here the offset for 0xc00807f0142c ie  0x112074 falls out
> .text section which is up to 0x10f7bc.
> 
> In this case for module, the address 0xc00807e11fd4 is pointing
> to stub instructions. This address range represents the module stubs
> which is allocated on module load and hence is not part of DSO offset.
> 
> To identify such  address, which falls out of text
> section and within module end, added the new field "text_end" to
> "struct dso".
> 
> Reported-by: Disha Goel 
> Signed-off-by: Athira Rajeev 
> ---
> Changelog:
>v1 -> v2:
>Changed commit message to explain need for "text_end"
> 
>  tools/perf/util/dso.h| 1 +
>  tools/perf/util/symbol-elf.c | 4 +++-
>  2 files changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
> index b41c9782c754..70fe0fe69bef 100644
> --- a/tools/perf/util/dso.h
> +++ b/tools/perf/util/dso.h
> @@ -181,6 +181,7 @@ struct dso {
>   u8   rel;
>   struct build_id  bid;
>   u64  text_offset;
> + u64  text_end;
>   const char   *short_name;
>   const char   *long_name;
>   u16  long_name_len;
> diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
> index 8bd466d1c2bd..252d26a59e64 100644
> --- a/tools/perf/util/symbol-elf.c
> +++ b/tools/perf/util/symbol-elf.c
> @@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map 
> *map, struct symsrc *syms_ss,
>   }
>  
>   if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
> - ".text", NULL))
> + ".text", NULL)) {
>   dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
> + dso->text_end = tshdr.sh_offset + tshdr.sh_size;
> + }
>  
>   if (runtime_ss->opdsec)
>   opddata = elf_rawdata(runtime_ss->opdsec, NULL);

Should probably amend dso__load_bfd_symbols() also




[PATCH V2 1/2] tools/perf: Add text_end to "struct dso" to save .text section size

2023-08-17 Thread Athira Rajeev
Update "struct dso" to include new member "text_end".
This new field will represent the offset for end of text
section for a dso. This value is derived as:
sh_size (Size of section in byes) + sh_offset (Section file
offst) of the elf header for text.

To resolve the address from a sample, perf looks at the
DSO maps. In case of address from a kernel module, there
were some address found to be not resolved. This was
observed while running perf test for "Object code reading".
Though the ip falls beteen the start address of the loaded
module (perf map->start ) and end address ( perf map->end),
it was unresolved.

Example:

Reading object code for memory address: 0xc00807f0142c
File is: /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
On file address is: 0x1114cc
Objdump command is: objdump -z -d --start-address=0x11142c 
--stop-address=0x1114ac /lib/modules/6.5.0-rc3+/kernel/fs/xfs/xfs.ko
objdump read too few bytes: 128
test child finished with -1

Here, module is loaded at:
# cat /proc/modules | grep xfs
xfs 2228224 3 - Live 0xc00807d0

>From objdump for xfs module, text section is:
text 0010f7bc    00a0 2**4

Here the offset for 0xc00807f0142c ie  0x112074 falls out
.text section which is up to 0x10f7bc.

In this case for module, the address 0xc00807e11fd4 is pointing
to stub instructions. This address range represents the module stubs
which is allocated on module load and hence is not part of DSO offset.

To identify such  address, which falls out of text
section and within module end, added the new field "text_end" to
"struct dso".

Reported-by: Disha Goel 
Signed-off-by: Athira Rajeev 
---
Changelog:
   v1 -> v2:
   Changed commit message to explain need for "text_end"

 tools/perf/util/dso.h| 1 +
 tools/perf/util/symbol-elf.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index b41c9782c754..70fe0fe69bef 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -181,6 +181,7 @@ struct dso {
u8   rel;
struct build_id  bid;
u64  text_offset;
+   u64  text_end;
const char   *short_name;
const char   *long_name;
u16  long_name_len;
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 8bd466d1c2bd..252d26a59e64 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1512,8 +1512,10 @@ dso__load_sym_internal(struct dso *dso, struct map *map, 
struct symsrc *syms_ss,
}
 
if (elf_section_by_name(runtime_ss->elf, _ss->ehdr, ,
-   ".text", NULL))
+   ".text", NULL)) {
dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
+   dso->text_end = tshdr.sh_offset + tshdr.sh_size;
+   }
 
if (runtime_ss->opdsec)
opddata = elf_rawdata(runtime_ss->opdsec, NULL);
-- 
2.31.1