Skip to content

Commit d831a73

Browse files
committed
[compiler-rt] Symbolizer refactoring: Move string parsing into separate functions
Reviewed at http://reviews.llvm.org/D7869 llvm-svn: 230529
1 parent e395da7 commit d831a73

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cc

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,69 @@ class SymbolizerProcess : public ExternalSymbolizerInterface {
308308
bool reported_invalid_path_;
309309
};
310310

311+
312+
// Parses one or more two-line strings in the following format:
313+
// <function_name>
314+
// <file_name>:<line_number>[:<column_number>]
315+
// Used by LLVMSymbolizer, Addr2LinePool and InternalSymbolizer, since all of
316+
// them use the same output format.
317+
static void ParseSymbolizePCOutput(const char *str, SymbolizedStack *res) {
318+
bool top_frame = true;
319+
SymbolizedStack *last = res;
320+
while (true) {
321+
char *function_name = 0;
322+
str = ExtractToken(str, "\n", &function_name);
323+
CHECK(function_name);
324+
if (function_name[0] == '\0') {
325+
// There are no more frames.
326+
break;
327+
}
328+
SymbolizedStack *cur;
329+
if (top_frame) {
330+
cur = res;
331+
top_frame = false;
332+
} else {
333+
cur = SymbolizedStack::New(res->info.address);
334+
cur->info.FillAddressAndModuleInfo(res->info.address, res->info.module,
335+
res->info.module_offset);
336+
last->next = cur;
337+
last = cur;
338+
}
339+
340+
AddressInfo *info = &cur->info;
341+
info->function = function_name;
342+
// Parse <file>:<line>:<column> buffer.
343+
char *file_line_info = 0;
344+
str = ExtractToken(str, "\n", &file_line_info);
345+
CHECK(file_line_info);
346+
const char *line_info = ExtractToken(file_line_info, ":", &info->file);
347+
line_info = ExtractInt(line_info, ":", &info->line);
348+
line_info = ExtractInt(line_info, "", &info->column);
349+
InternalFree(file_line_info);
350+
351+
// Functions and filenames can be "??", in which case we write 0
352+
// to address info to mark that names are unknown.
353+
if (0 == internal_strcmp(info->function, "??")) {
354+
InternalFree(info->function);
355+
info->function = 0;
356+
}
357+
if (0 == internal_strcmp(info->file, "??")) {
358+
InternalFree(info->file);
359+
info->file = 0;
360+
}
361+
}
362+
}
363+
364+
// Parses a two-line string in the following format:
365+
// <symbol_name>
366+
// <start_address> <size>
367+
// Used by LLVMSymbolizer and InternalSymbolizer.
368+
static void ParseSymbolizeDataOutput(const char *str, DataInfo *info) {
369+
str = ExtractToken(str, "\n", &info->name);
370+
str = ExtractUptr(str, " ", &info->start);
371+
str = ExtractUptr(str, "\n", &info->size);
372+
}
373+
311374
// For now we assume the following protocol:
312375
// For each request of the form
313376
// <module_name> <module_offset>
@@ -537,49 +600,7 @@ class POSIXSymbolizer : public Symbolizer {
537600
return res;
538601
}
539602

540-
bool top_frame = true;
541-
SymbolizedStack *last = res;
542-
while (true) {
543-
char *function_name = 0;
544-
str = ExtractToken(str, "\n", &function_name);
545-
CHECK(function_name);
546-
if (function_name[0] == '\0') {
547-
// There are no more frames.
548-
break;
549-
}
550-
SymbolizedStack *cur;
551-
if (top_frame) {
552-
cur = res;
553-
top_frame = false;
554-
} else {
555-
cur = SymbolizedStack::New(addr);
556-
cur->info.FillAddressAndModuleInfo(addr, module_name, module_offset);
557-
last->next = cur;
558-
last = cur;
559-
}
560-
561-
AddressInfo *info = &cur->info;
562-
info->function = function_name;
563-
// Parse <file>:<line>:<column> buffer.
564-
char *file_line_info = 0;
565-
str = ExtractToken(str, "\n", &file_line_info);
566-
CHECK(file_line_info);
567-
const char *line_info = ExtractToken(file_line_info, ":", &info->file);
568-
line_info = ExtractInt(line_info, ":", &info->line);
569-
line_info = ExtractInt(line_info, "", &info->column);
570-
InternalFree(file_line_info);
571-
572-
// Functions and filenames can be "??", in which case we write 0
573-
// to address info to mark that names are unknown.
574-
if (0 == internal_strcmp(info->function, "??")) {
575-
InternalFree(info->function);
576-
info->function = 0;
577-
}
578-
if (0 == internal_strcmp(info->file, "??")) {
579-
InternalFree(info->file);
580-
info->file = 0;
581-
}
582-
}
603+
ParseSymbolizePCOutput(str, res);
583604
return res;
584605
}
585606

@@ -602,9 +623,7 @@ class POSIXSymbolizer : public Symbolizer {
602623
const char *str = SendCommand(true, module_name, module_offset);
603624
if (str == 0)
604625
return true;
605-
str = ExtractToken(str, "\n", &info->name);
606-
str = ExtractUptr(str, " ", &info->start);
607-
str = ExtractUptr(str, "\n", &info->size);
626+
ParseSymbolizeDataOutput(str, info);
608627
info->start += module->base_address();
609628
return true;
610629
}

0 commit comments

Comments
 (0)