Skip to contentSkip to conversation

perf annotate: Fallback to objdump when capstone fails

PATCH Applied Namhyung Kim 3 participants 8 messages Apr 25, 2024 – Apr 27, 2024 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf annotate: Fallback to objdump when capstone fails

I found some cases that capstone failed to disassemble. Probably my capstone is an old version but anyway there's a chance it can fail. And then it silently stopped in the middle. In my case, it didn't understand "RDPKRU" instruction.

Let's check if the capstone disassemble reached to the end of the function. And fallback to objdump if not.

---
 tools/perf/util/disasm.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/perf/util/disasm.c +14 -0
index 92937809be85..412101f2cf2a 100644
@@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 		offset += insn[i].size;
 	}
 
+	/* It failed in the middle: probably due to unknown instructions */
+	if (offset != len) {
+		struct list_head *list = &notes->src->source;
+
+		/* Discard all lines and fallback to objdump */
+		while (!list_empty(list)) {
+			dl = list_first_entry(list, struct disasm_line, al.node);
+
+			list_del_init(&dl->al.node);
+			disasm_line__free(dl);
+		}
+		count = -1;
+	}
+
 out:
 	if (needs_cs_close)
 		cs_close(&handle);
Signature · 1 line
2.44.0.769.g3c40516874-goog
author maintainer
#1 lore raw

PATCH 2/2 perf annotate: Update dso binary type when try build-id

dso__disassemble_filename() tries to get the filename for objdump (or capstone) using build-id. But I found sometimes it didn't disassemble some functions. It turned out that those functions belong to a dso which has no binary type set. It seems it sets the binary type for some special files only - like kernel (kallsyms or kcore) or BPF images. And there's a logic to skip dso with DSO_BINARY_TYPE__NOT_FOUND.

As it's checked the build-id cache linke, it should set the binary type as DSO_BINARY_TYPE__BUILD_ID_CACHE.

Fixes 873a83731f1c ("perf annotate: Skip DSOs not found")
---
 tools/perf/util/disasm.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/disasm.c +2 -0
index 412101f2cf2a..6d1125e687b7 100644
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(&dso->lock);
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}
 
 	free(build_id_path);
Signature · 1 line
2.44.0.769.g3c40516874-goog
#2 lore raw

Re: [PATCH 2/2] perf annotate: Update dso binary type when try build-id

Quoted 21 lines · Namhyung Kim

dso__disassemble_filename() tries to get the filename for objdump (or capstone) using build-id. But I found sometimes it didn't disassemble some functions. It turned out that those functions belong to a dso which has no binary type set. It seems it sets the binary type for some special files only - like kernel (kallsyms or kcore) or BPF images. And there's a logic to skip dso with DSO_BINARY_TYPE__NOT_FOUND.

As it's checked the build-id cache linke, it should set the binary type as DSO_BINARY_TYPE__BUILD_ID_CACHE.

Fixes 873a83731f1c ("perf annotate: Skip DSOs not found")
---
 tools/perf/util/disasm.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/disasm.c +0 -0
index 412101f2cf2a..6d1125e687b7 100644

tools/perf/util/disasm.c static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1163

Quoted 8 lines
tools/perf/util/disasm.c +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(&dso->lock);
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}
 
 	free(build_id_path);

Fixed up to take into account a recent patch by Ian that turned that &dso->lock into dso__lock(dso):

tools/perf/util/disasm.c +2 -0
index 70650808e2e7bf88..2921b32357705a02 100644
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(dso__lock(dso));
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}
 
 	free(build_id_path);
#3 lore raw

Re: [PATCH 2/2] perf annotate: Update dso binary type when try build-id

Quoted 13 lines · Arnaldo Carvalho de Melo

Review static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1159

Quoted 6 lines · Namhyung Kim

+++ b/tools/perf/util/disasm.c

(quoted diff) +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 		mutex_unlock(&dso->lock);
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}

Fixed up to take into account a recent patch by Ian that turned that &dso->lock into dso__lock(dso):

+++ b/tools/perf/util/disasm.c

Review static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1161

Quoted 6 lines
(quoted diff) +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(dso__lock(dso));
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}

Nah, I forgot some more stuff, this is what I have now:

tools/perf/util/disasm.c +2 -2
index 2921b32357705a02..72aec8f61b944a7a 100644
@@ -1156,8 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(dso__lock(dso));
-	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
-		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
+	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
 	}
 
 	free(build_id_path);
#4 lore raw

Re: [PATCH 2/2] perf annotate: Update dso binary type when try build-id

Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Arnaldo Carvalho de Melo

Review static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1159

Quoted 6 lines · Namhyung Kim

+++ b/tools/perf/util/disasm.c

(quoted diff) +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 		mutex_unlock(&dso->lock);
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}

Fixed up to take into account a recent patch by Ian that turned that &dso->lock into dso__lock(dso):

Quoted 10 lines

tools/perf/util/disasm.c static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1161

Quoted 8 lines

+++ b/tools/perf/util/disasm.c

tools/perf/util/disasm.c +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 			}
 		}
 		mutex_unlock(dso__lock(dso));
+	} else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+		dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
 	}

Nah, I forgot some more stuff, this is what I have now:

Nah², I had to remove all these:

pick a58b4da77b40920f perf dsos: Switch backing storage to array from rbtree/list pick 7d91cefd1fb63068 perf dsos: Remove __dsos__addnew()
pick 80c3ccf05199dbb6 perf dsos: Remove __dsos__findnew_link_by_longname_id() pick af3f8dea24f47802 perf dsos: Switch hand code to bsearch()
pick 7537b92b48318834 perf dso: Add reference count checking and accessor functions pick 9bd7c6fe8de22b37 perf dso: Reference counting related fixes
pick 4de57b46a0cb2027 perf dso: Use container_of() to avoid a pointer in 'struct dso_data'

Due to a bisect that pointed "perf dsos: Switch backing storage to array from rbtree/list" as the one where:

root@x1:~# perf test "kernel lock contention analysis test"
 87: kernel lock contention analysis test                            : FAILED!

- Arnaldo

reviewer
#5 lore raw

Re: [PATCH 2/2] perf annotate: Update dso binary type when try build-id

In reply to Arnaldo Carvalho de Melo
Quoted 40 lines · Arnaldo Carvalho de Melo
Quoted 21 lines · Namhyung Kim

dso__disassemble_filename() tries to get the filename for objdump (or capstone) using build-id. But I found sometimes it didn't disassemble some functions. It turned out that those functions belong to a dso which has no binary type set. It seems it sets the binary type for some special files only - like kernel (kallsyms or kcore) or BPF images. And there's a logic to skip dso with DSO_BINARY_TYPE__NOT_FOUND.

As it's checked the build-id cache linke, it should set the binary type as DSO_BINARY_TYPE__BUILD_ID_CACHE.

Fixes 873a83731f1c ("perf annotate: Skip DSOs not found")
---
 tools/perf/util/disasm.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/disasm.c +0 -0
index 412101f2cf2a..6d1125e687b7 100644

tools/perf/util/disasm.c static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1163

Quoted 8 lines
tools/perf/util/disasm.c +2 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
                      }
              }
              mutex_unlock(&dso->lock);
+     } else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {
+             dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
      }
 
      free(build_id_path);

Fixed up to take into account a recent patch by Ian that turned that &dso->lock into dso__lock(dso):

tools/perf/util/disasm.c +0 -0
index 70650808e2e7bf88..2921b32357705a02 100644

tools/perf/util/disasm.c static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil · line 1159

Quoted 4 lines
tools/perf/util/disasm.c +1 -0
@@ -1156,6 +1156,8 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
                        }
                }
                mutex_unlock(dso__lock(dso));
+       } else if (dso->binary_type == DSO_BINARY_TYPE__NOT_FOUND) {

With reference count checking on dsos, this will need to be:

dso__binary_type(dso)

too.

Thanks,
Ian

Quoted 4 lines
tools/perf/util/disasm.c +1 -0
+               dso->binary_type = DSO_BINARY_TYPE__BUILD_ID_CACHE;
        }
 
        free(build_id_path);
maintainer Applied
#6 lore raw
In reply to Namhyung Kim
Quoted 7 lines · Namhyung Kim

I found some cases that capstone failed to disassemble. Probably my capstone is an old version but anyway there's a chance it can fail. And then it silently stopped in the middle. In my case, it didn't understand "RDPKRU" instruction.

Let's check if the capstone disassemble reached to the end of the function. And fallback to objdump if not.

Thanks, applied to perf-tools-next,

- Arnaldo

Quoted 32 lines
---
 tools/perf/util/disasm.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/perf/util/disasm.c +14 -0
index 92937809be85..412101f2cf2a 100644
@@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 		offset += insn[i].size;
 	}
 
+	/* It failed in the middle: probably due to unknown instructions */
+	if (offset != len) {
+		struct list_head *list = &notes->src->source;
+
+		/* Discard all lines and fallback to objdump */
+		while (!list_empty(list)) {
+			dl = list_first_entry(list, struct disasm_line, al.node);
+
+			list_del_init(&dl->al.node);
+			disasm_line__free(dl);
+		}
+		count = -1;
+	}
+
 out:
 	if (needs_cs_close)
 		cs_close(&handle);
Signature · 1 line
2.44.0.769.g3c40516874-goog
#7 lore raw
In reply to Namhyung Kim
Quoted 7 lines · Namhyung Kim

I found some cases that capstone failed to disassemble. Probably my capstone is an old version but anyway there's a chance it can fail. And then it silently stopped in the middle. In my case, it didn't understand "RDPKRU" instruction.

Let's check if the capstone disassemble reached to the end of the function. And fallback to objdump if not.

Humm:

⬢[acme@toolbox perf-tools-next]$ git am ./20240424_namhyung_perf_annotate_fallback_to_objdump_when_capstone_fails.mbx
Applying: perf annotate: Fallback to objdump when capstone fails
Applying: perf annotate: Update dso binary type when try build-id
error: patch failed: tools/perf/util/disasm.c:1156
error: tools/perf/util/disasm.c: patch does not apply
Patch failed at 0002 perf annotate: Update dso binary type when try build-id
hint: Use 'git am --show-current-patch=diff' to see the failed patch When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". ⬢[acme@toolbox perf-tools-next]$ git am --abort
⬢[acme@toolbox perf-tools-next]$

checking...

- Arnado

Quoted 32 lines
---
 tools/perf/util/disasm.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/perf/util/disasm.c +14 -0
index 92937809be85..412101f2cf2a 100644
@@ -1542,6 +1542,20 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 		offset += insn[i].size;
 	}
 
+	/* It failed in the middle: probably due to unknown instructions */
+	if (offset != len) {
+		struct list_head *list = &notes->src->source;
+
+		/* Discard all lines and fallback to objdump */
+		while (!list_empty(list)) {
+			dl = list_first_entry(list, struct disasm_line, al.node);
+
+			list_del_init(&dl->al.node);
+			disasm_line__free(dl);
+		}
+		count = -1;
+	}
+
 out:
 	if (needs_cs_close)
 		cs_close(&handle);
Signature · 1 line
2.44.0.769.g3c40516874-goog