I am having a question about disassembling a simple C program, What I want to do is to have a file which shows the C source and compiled assembly. I have figured out how to do this by using:
riscv-gcc -g -c test.c
riscv-objdump -d -S test.o
And I also want to get a feeling about the program counter (PC), so it might be good to know which lines of assembly instruction get executed how frequently in a program. I know we can do that with Spike simulator, but how do we combine all the information (C vs. ASM + PC log) into a single file if possible?
I do not know of a standard way to combine the two files, but you can use the spike-dasm utility to disassemble your log from Spike or rocket-chip based simulations. It is installed with the rest of riscv-tools:
Thank you for your reply! But that is for using C++ simulator, right? The spike-dasm is used to translate the simulated result into readable form.
In this case, I think what I want is to have a C program, translate it to assembly, and know how frequent does each instruction get executed. Is there anyway to do that?
There is no existing tool that I know of that does what you want to do. In order to know how frequently each instruction is executed, you need to run the C program through a simulator. You should be able to write a small script that takes the simulation output and counts the frequency of each instruction.
By simulator, you mean Spike simulator? And also is there a way to get which piece of C code (assembly correspondingly) is executed how frequently? The best solution I can find at the moment is to use spike -g pk (binary), -g is for PC histogram, but I don’t understand what PC histgoram size means.
Could you please explain me a bit further about it? Or if you know anything that can does the jobs above?
Thanks for your quick reply. Yes, in that case we would have the frequency of each instruction (represented by addresses in spike -g output).
But we still don’t know which line of C code this corresponds to. Is there anyway to glue this information with a file which shows C and assembly at the same time?
Yes, you are right, we could do that, is there a better way to link the PC information to the disassembled file? or we have to write, for example, a perl script to manually parse the objdump file and manually combine those two?
Sorry, I don’t know of any way to automatically link the lines of ASM to the lines in the disassembled file. It hopefully isn’t too difficult to write a short script to parse out the mappings, but keep in mind that GCC is free to rearrange the C code when it generates the assembly code, so the mappings may not be perfectly accurate.
Thanks @rxia Richard, not a problem at all. I just want to get a sense of roughly which particular instruction is executed most of time (hence slowing down the program) in a particular C program. If this is the best way to go, I will have a try.
May I ask how did you compile the object file (which command did you use?) I used -g -c option in RISC-V gcc, but it turns out that the addresses in objdump files doesn’t match to those in pc histogram.
When you passed in -c, that told GCC not to link the object file, so your ELF file has the unrelocated addresses. If you omit the -c option, GCC will perform the final step of linking and assigning final addresses to the program, producing an executable ELF.
Sorry, I changed around some of the file names to match the names that you picked, so I never actually created test.o. I disassembled the test.out I created. Let me start over from the beginning.
Starting with a simple C file:
// simple.c
double dmul(double a, double b) {
return a * b;
}
int main(void) {
return dmul(1.0, 2.0);
}
I create the relocated, executable ELF file (simple.out) and use that to disassemble:
Would you please explain this script snippest, is it a full work which can be used? I tried it but it didn’t give any response only it seems waiting some input from the keyboard.
what this line mean ?? "@x = split " “, $_;”
What is ?? shold this replaced with the log file name or writen as it is??
How should I give the log file input to this script?!!
This gives static information about frequency of instruction.Does’t this just tells us how assembly code is stored in memory?
How to find frequency of instruction executed in runtime.I tried spike -l (log of execution) but not sure if its correct way