How to profile your own function calls in C? (instrument your code!)

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024

ความคิดเห็น • 59

  • @belesiu
    @belesiu 3 ปีที่แล้ว +3

    Learned something new even though I’ve been an embedded systems engineer for years. I wish this was available to our team way back when! Very nice!!

  • @benjaminshinar9509
    @benjaminshinar9509 4 ปีที่แล้ว +13

    that's really cool!
    I think complier flags is a great topic for videos, and really under-represented!

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +3

      I know, right?! Another topic for the list of things to make videos about.

  • @query4aman
    @query4aman 4 ปีที่แล้ว +4

    I am really inspired by the way you make videos, Great Job!

  • @alexsmsn
    @alexsmsn 4 ปีที่แล้ว +11

    Love your videos. I've been a C practitioner for years and didn't know this was available and so easy to use (or at least you make it look easy). Thank you! Binge watching the rest of your channel.

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +6

      Thanks. Yeah, I didn't either. It makes me wonder what else is sitting there waiting for me to discover.

  • @andrewnorris5415
    @andrewnorris5415 ปีที่แล้ว

    Loved this, thanks so much! Note it said "no_pie" on your screen - but my clang only accepted "no-pie". Also the warnings you got were not given on my clang when I just added the "-finstrument-functions" flag. Maybe the latest compilers fail to warn us?

  • @andrewnorris5415
    @andrewnorris5415 ปีที่แล้ว

    Thanks for this. I found an example online that uses dladdr to get the function name from the address.

  • @robkaz12345
    @robkaz12345 4 ปีที่แล้ว +1

    Nice video, would you make a video about virtual memory and how to calculate offsest of main function?

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      First, check David's comment about __executable_start. That's a far simpler than what I was doing. But, yes, I'm planning to do some more virtual memory stuff in the future. It's on the list.

  • @ayu12641
    @ayu12641 4 ปีที่แล้ว +1

    Any way to get the function parameter information along with the function call trace? These instrumentation functions just seem to output function addresses. One way that I can think of is to run the whole program using GDB while putting breakpoints on all functions and parsing the stdout using scripts. Do you have any suggestions?

  • @aabdev
    @aabdev 4 ปีที่แล้ว

    That is a good way to measure code coverage of running MCU bare metal firmware. Because there is no opportunity to use tool gcov in MCU firmware.

  • @amoghmund
    @amoghmund 3 ปีที่แล้ว

    How can we define anonymous functions in C like that in JavaScript or PHP

  • @sachetmittal123
    @sachetmittal123 4 ปีที่แล้ว

    Is there a way to inject these in specific functions?

  • @ramyaneekashyap4540
    @ramyaneekashyap4540 3 ปีที่แล้ว

    hey, have you made any video on gcov?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      I haven't. But, it's on my list of future video topics.

    • @ramyaneekashyap4540
      @ramyaneekashyap4540 3 ปีที่แล้ว

      @@JacobSorber that's great! Looking forward to it. In the mean time if you can drop of some decent sources/links to refer, that would be immensely helpful.
      PS: your videos are shark for system and os related c programming 🎉 love them!

  • @mehmetdemir-lf2vm
    @mehmetdemir-lf2vm 3 ปีที่แล้ว

    no words about gprof?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      Apparently not. I do have profilers (gprof included) on my list of topics for future videos. So, at some point, there should be a video on gprof on the channel. Just not sure when.

  • @WhileTrueThenDream
    @WhileTrueThenDream 4 ปีที่แล้ว +11

    Thank you. Today I was using printfs at the beginning of some function callbacks to check when each one is executed and thought "there must be an smarter way to do that, I need to perform a research.."

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +2

      Yes! Glad I could help.

  • @JannisAdmek
    @JannisAdmek 4 ปีที่แล้ว +7

    The new intro is beyond amazing, I watched it a couple of times :D

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +2

      Thanks, Jannis. It's always fun to see what you can do with a half meter of fishing line and a camera. 🙂

  • @ShotgunLlama
    @ShotgunLlama 3 ปีที่แล้ว +2

    One other very useful, probably related, thing you can do is use the --wrap option for the linker, which will wrap calls to, for example, foo() to instead refer to __wrap_foo(), which can in turn call __real_foo(). I imagine this could be useful when you only want to have extra behavior for certain functions, and I believe it can also wrap stdlib functions

  • @mshingote
    @mshingote 4 ปีที่แล้ว +3

    Learnt something new! :)

  • @bitshiftbandit
    @bitshiftbandit ปีที่แล้ว +1

    I know C is not a paradigm focused language, but I think the general technique being asked is related to Aspect-oriented programming. To be fair, I also cannot think of a reasonable place to go this besides at compile time. The example, while limited, gives the basic building blocks to add point cuts. Very powerful feature indeed! I would love to see a library to augment this with e.g. standard lookup tables, pulling stack values/registers, etc.

  • @eddaly4160
    @eddaly4160 11 หลายเดือนก่อน

    Great video, I thought I had a clever way to print the name of the function using a %s and __FUNCTION__ in the printf call....it works but you get __cyg_profile_func_enter and __cyg_profile_func_exit names....who knows maybe there is some other macro that I can use to the function name...

  • @desmondbrown5508
    @desmondbrown5508 3 ปีที่แล้ว

    No offense to you, but you look so much like Lucas Baker from RE7... Maybe I just haven't played the game in awhile. Good videos though. Been enjoying hearing different techniques.

  • @manoliowes
    @manoliowes 4 ปีที่แล้ว +1

    Hey There : )
    Great video. Love your work!
    I find it somewhat limiting though, having the _enter and _exit functions applied after any function call in my program.
    Is there a way to target one specific function instead of all of them?
    Is there a way to define the _enter and _exit functions in a separate file or perhaps in a library, that one can include?
    That would make it easier to use across multiple projects.
    I can't help it to compare this concept to decorators in python. You had a similar idea in your videos. I would like to see more about that.
    I have to say that python is quite more elegant in doing it.
    Thanks for reading this

  • @MyEpitt
    @MyEpitt ปีที่แล้ว

    Is this profiling method available if we use gcc? That is, is it portable?

  • @WilliamRaezer
    @WilliamRaezer 6 หลายเดือนก่อน

    Instrumentation post compilation is more interesting.

  • @simgamedev6423
    @simgamedev6423 3 ปีที่แล้ว +1

    Will you make more videos on this subject?

  • @farazfazli
    @farazfazli 4 ปีที่แล้ว +2

    I love the new intro. It's so awesome!

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +1

      Thanks. My daughter and I had fun making it.

  • @samplesandtests
    @samplesandtests ปีที่แล้ว

    if you use the -no_pie flag and try to run multiple instances of the program (either in the background or in different terminal windows) would the memory collide? i am temped to try this.

    • @JacobSorber
      @JacobSorber  ปีที่แล้ว

      Try it. 🤔 But, no, on any machine/OS using virtual memory it won't collide.

  • @fidalgoverde
    @fidalgoverde ปีที่แล้ว

    absolutely fabulous

  • @vani9831
    @vani9831 ปีที่แล้ว

    This is my new favourite channel now! Thanks a lot for this.
    I would have loved for you to go slightly advanced with the example though. Maybe call something custom, print something from memory and not just pointers, or show how to use this with gcc even.
    I may be wrong here, but there is definitely a large audience still using gcc (and even quite old versions) , because most C programmers are actually working with legacy code on various system architectures in production systems.

  • @andrewnorris5415
    @andrewnorris5415 ปีที่แล้ว

    New comment. I love this more and more! I can just add the "-finstrument-functions -rdynamic" in the Makefile - and it automatically assembles with the instrumentation. Using function dladdr - I can easily grab the name of the function . Now looking to see if I can get the params passed. Which could be useful for debugging code - keeping it all in one place.

  • @supaderrick3502
    @supaderrick3502 4 ปีที่แล้ว +1

    Your videos are gold.

  • @rafalmichalski4893
    @rafalmichalski4893 4 ปีที่แล้ว +1

    Thank you very much !

  • @pathayes2224
    @pathayes2224 3 ปีที่แล้ว

    Great Job!...Can you give a link with the corresponding gcc linker and compiler info? especially the no_pie & finstrument-functions

  • @programmingexplainedsimply3310
    @programmingexplainedsimply3310 4 ปีที่แล้ว

    Very nicely explained.

  • @santoshsco
    @santoshsco 3 ปีที่แล้ว

    Hi Jacob , could you please make a video on gprof ? Nice video btw .

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      Thanks. And, yeah, probably. Profiling is on my list of future video topics.

  • @ciph3r836
    @ciph3r836 4 ปีที่แล้ว

    How to modify a compiled binary or how can i add instructions to a compiled binary ?

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว

      That would be an intense video. Or a lengthy explanation. For now, maybe take a look at this project. github.com/utds3lab/multiverse

    • @ciph3r836
      @ciph3r836 4 ปีที่แล้ว

      @@JacobSorber thanks . I tried reading articles. Couldn't understand after a point

  • @BalaMurugan-bh7if
    @BalaMurugan-bh7if 4 ปีที่แล้ว

    thanks...very useful as like other videos.... having a question about pthread monitoring...
    could you please provide a method like software watchdog mechanism to know liveliness of each thread of my application(my app is a multithread process developed using c. want to know which thread runs into busyloop/deadlock)?

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +1

      Good question. You might want to check out my video about debugging with threads. It's not quite the same thing, but you can often use your debugger to effectively sample your threads and see what they're up to. This is a good topic for a future video, as well. I'll see what I can do. Thanks.

    • @BalaMurugan-bh7if
      @BalaMurugan-bh7if 4 ปีที่แล้ว

      @@JacobSorber thanks!! i run my app in an embedded system and need to report the health of my app where i have to check the critical thread health. Hence the request.

  • @karimkohel3240
    @karimkohel3240 4 ปีที่แล้ว

    wow, nice new intro