Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading symbols from /tmp/perf-pid.map #573

Merged
merged 1 commit into from
Jun 20, 2016
Merged

Add support for reading symbols from /tmp/perf-pid.map #573

merged 1 commit into from
Jun 20, 2016

Conversation

markdrayton
Copy link
Contributor

For #548, this adds basic support for reading symbols from /tmp/perf-pid.map (e.g., from a JIT or other code relocation). To cope with processes in containers, it supports:

  • mapping from BCC's PID namespace to the target process's PID namespace
    using /proc/pid/status
  • resolving a target process's root filesystem using /proc/pid/root

Note that mapping to a target's PID in a separate namespace requires a NSpid: line in /proc/pid/status. IIRC this was added sometime in 4.0 but I can't remember exactly when.

This adds basic support for /tmp/perf-pid.map. To cope with processes in
containers, it supports:

* mapping from BCC's PID namespace to the target process's PID namespace
  using /proc/pid/status
* resolving a target process's root filesystem using /proc/pid/root
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2016 GitHub, Inc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy paste? ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, yes. Fixed now.

@4ast
Copy link
Member

4ast commented Jun 16, 2016

LGTM
@vmg pls take a look

@4ast 4ast merged commit 6862875 into iovisor:master Jun 20, 2016

size_t size;
char *line = NULL;
while (getline(&line, &size, status) != -1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite right here. According to POSIX.1-200, getline will only allocate a new buffer to store the line when line == NULL and size == 0. Since size is uninitialized here, this can break in really unexpected ways.

On top of that, getline allocates a buffer for each call (even in failure cases), and you're calling free only once outside the loop, hence leaking significant amounts of memory.

Please fix the initialization and deallocation, and add some curly braces to prevent further bugs like this in the future. Thanks! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!

Fixing size is fine. My getline man page actually says it's ignored if *lineptr == NULL, but http://man7.org/linux/man-pages/man3/getline.3.html says it must be zero. Regardless, there's clearly there's no downside to intializing to zero so I'll do that.

Re: getline allocating on every call: are you sure about that? http://man7.org/linux/man-pages/man3/getline.3.html says getline will use the buffer passed and realloc it if necessary. It doesn't say it'll allocate every time around and the example code in the man page just has a single free outside the loop. Additionally, my own simple test with valgrind appears to confirm this is safe:

https://gist.github.com/markdrayton/1ef00109ff2092898ab3b908d0598c41

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're totally right, I misread the manpage. It will indeed realloc if the original pointer is not NULL... That is a much more friendly API to use! So yes, the only frisky thing is initializing the value of size. Sorry for wasting your time!

@vmg
Copy link
Contributor

vmg commented Jun 21, 2016

Sorry I couldn't get to review this earlier. @markdrayton: I quite dig the tests, but you're not using getline properly. See the attached comments.

return -1;

char *line = NULL;
size_t size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This needs zero initialization.

@vmg
Copy link
Contributor

vmg commented Jun 21, 2016

Everything else looks good. Thanks for adding this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants