GithubHelp home page GithubHelp logo

Comments (5)

brendangregg avatar brendangregg commented on June 30, 2024

Note that the one-liner that should work may really be this:

./src/bpftrace -e 'kprobe:do_nanosleep { printf("%x\n", *kaddr("avenrun")); }'

I missed the dereference earlier.

from bpftrace.

williangaspar avatar williangaspar commented on June 30, 2024

This function reads the memory address just like # grep -w avenrun /proc/kallsyms.

My question is where or if I should print the output?

uint64_t BPFtrace::resolve_kname(const char *name)
{
  uint64_t addr = 0;
  std::string file_name = "/proc/kallsyms";

  std::ifstream file(file_name);
  if (file.fail())
  {
    std::cerr << strerror(errno) << ": " << file_name << std::endl;
    return addr;
  }

  std::string line;

  std::string search = "\\b( ";
  search += name;
  std::regex e (search + ")");
  std::smatch match;

  while (std::getline(file, line) && addr == 0)
  {
    auto found = std::regex_search (line, match, e);

    if (found)
    {
      std::string first_word = line.substr(0, line.find(" "));
      addr = std::stoull(first_word, 0, 16);
    }
  }

  return addr;
}```

from bpftrace.

brendangregg avatar brendangregg commented on June 30, 2024

I don't understand your question. I think if you drop that function into the earlier patch, it should all work, right? BPFtrace::resolve_kname() doesn't need to print anything itself, it just needs to return the address.

This should print avenrun's location (as seen in /proc):

# ./src/bpftrace -e 'kprobe:do_nanosleep { printf("%llx\n", kaddr("avenrun")); }'

And this should print the value of avenrun:

# ./src/bpftrace -e 'kprobe:do_nanosleep { printf("%x\n", *kaddr("avenrun")); }'

Shouldn't you call close on file at the end?

from bpftrace.

williangaspar avatar williangaspar commented on June 30, 2024

Ok, # ./src/bpftrace -e 'kprobe:do_nanosleep { printf("%llx\n", kaddr("avenrun")); }' prints what I was expecting.

And yes, I forgot to close the file.

from bpftrace.

brendangregg avatar brendangregg commented on June 30, 2024

I realized the tests/codegen.cpp call_kaddr test may always fail, since it would embed the avenrun location into the test output, which would change between systems. Hm. Unless there's a simple way to fix this, I'd be inclined to delete the call_kaddr test and replace it with a // TODO: test kaddr().

from bpftrace.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.