-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for reading symbols from /tmp/perf-pid.map
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
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2016 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "bcc_perf_map.h" | ||
|
||
int bcc_perf_map_nspid(int pid) { | ||
char status_path[64]; | ||
FILE *status; | ||
|
||
snprintf(status_path, sizeof(status_path), "/proc/%d/status", pid); | ||
status = fopen(status_path, "r"); | ||
|
||
if (!status) | ||
return -1; | ||
|
||
// return the original PID if the NSpid line is missing | ||
int nspid = pid; | ||
|
||
size_t size; | ||
char *line = NULL; | ||
while (getline(&line, &size, status) != -1) | ||
if (strstr(line, "NSpid:") != NULL) | ||
// PID namespaces can be nested -- last number is innermost PID | ||
nspid = (int)strtol(strrchr(line, '\t'), NULL, 10); | ||
free(line); | ||
|
||
return nspid; | ||
} | ||
|
||
bool bcc_perf_map_path(char *map_path, size_t map_len, int pid) { | ||
char source[64]; | ||
snprintf(source, sizeof(source), "/proc/%d/root", pid); | ||
|
||
char target[4096]; | ||
ssize_t target_len = readlink(source, target, sizeof(target) - 1); | ||
if (target_len == -1) | ||
return false; | ||
|
||
target[target_len] = '\0'; | ||
if (strcmp(target, "/") == 0) | ||
target[0] = '\0'; | ||
|
||
int nspid = bcc_perf_map_nspid(pid); | ||
|
||
snprintf(map_path, map_len, "%s/tmp/perf-%d.map", target, nspid); | ||
return true; | ||
} | ||
|
||
int bcc_perf_map_foreach_sym(const char *path, bcc_perf_map_symcb callback, | ||
void* payload) { | ||
FILE* file = fopen(path, "r"); | ||
if (!file) | ||
return -1; | ||
|
||
char *line = NULL; | ||
size_t size; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. This needs zero initialization. |
||
long long begin, len; | ||
while (getline(&line, &size, file) != -1) { | ||
char *cursor = line; | ||
char *newline, *sep; | ||
|
||
begin = strtoull(cursor, &sep, 16); | ||
if (*sep != ' ' || (sep == cursor && begin == 0)) | ||
continue; | ||
cursor = sep; | ||
while (*cursor && isspace(*cursor)) cursor++; | ||
|
||
len = strtoull(cursor, &sep, 16); | ||
if (*sep != ' ' || (sep == cursor && begin == 0)) | ||
continue; | ||
cursor = sep; | ||
while (*cursor && isspace(*cursor)) cursor++; | ||
|
||
newline = strchr(cursor, '\n'); | ||
if (newline) | ||
newline[0] = '\0'; | ||
|
||
callback(cursor, begin, len - 1, 0, payload); | ||
} | ||
|
||
free(line); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, leaking memory here. |
||
fclose(file); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2016 Facebook, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef LIBBCC_PERF_MAP_H | ||
#define LIBBCC_PERF_MAP_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <unistd.h> | ||
|
||
typedef int (*bcc_perf_map_symcb)(const char *, uint64_t, uint64_t, int, | ||
void *); | ||
|
||
int bcc_perf_map_nspid(int pid); | ||
bool bcc_perf_map_path(char *map_path, size_t map_len, int pid); | ||
int bcc_perf_map_foreach_sym(const char *path, bcc_perf_map_symcb callback, | ||
void* payload); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 whenline == NULL
andsize == 0
. Sincesize
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 callingfree
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! :)
There was a problem hiding this comment.
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. Mygetline
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 saysgetline
will use the buffer passed andrealloc
it if necessary. It doesn't say it'll allocate every time around and the example code in the man page just has a singlefree
outside the loop. Additionally, my own simple test with valgrind appears to confirm this is safe:https://gist.github.com/markdrayton/1ef00109ff2092898ab3b908d0598c41
There was a problem hiding this comment.
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 ofsize
. Sorry for wasting your time!