2018-04-28 22:28:13 -07:00
// SPDX-License-Identifier: GPL-2.0
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
# include <errno.h>
2020-03-21 19:04:23 +09:00
# include <fcntl.h>
2018-04-28 22:28:13 -07:00
# include <poll.h>
# include <unistd.h>
# include <linux/perf_event.h>
# include <sys/mman.h>
# include "trace_helpers.h"
2020-03-21 19:04:23 +09:00
# define DEBUGFS " / sys / kernel / debug / tracing / "
2018-04-28 22:28:13 -07:00
# define MAX_SYMS 300000
static struct ksym syms [ MAX_SYMS ] ;
static int sym_cnt ;
static int ksym_cmp ( const void * p1 , const void * p2 )
{
return ( ( struct ksym * ) p1 ) - > addr - ( ( struct ksym * ) p2 ) - > addr ;
}
int load_kallsyms ( void )
{
FILE * f = fopen ( " /proc/kallsyms " , " r " ) ;
char func [ 256 ] , buf [ 256 ] ;
char symbol ;
void * addr ;
int i = 0 ;
if ( ! f )
return - ENOENT ;
2019-05-26 10:32:11 +00:00
while ( fgets ( buf , sizeof ( buf ) , f ) ) {
2018-04-28 22:28:13 -07:00
if ( sscanf ( buf , " %p %c %s " , & addr , & symbol , func ) ! = 3 )
break ;
if ( ! addr )
continue ;
syms [ i ] . addr = ( long ) addr ;
syms [ i ] . name = strdup ( func ) ;
i + + ;
}
2018-10-18 23:18:36 +08:00
fclose ( f ) ;
2018-04-28 22:28:13 -07:00
sym_cnt = i ;
qsort ( syms , sym_cnt , sizeof ( struct ksym ) , ksym_cmp ) ;
return 0 ;
}
struct ksym * ksym_search ( long key )
{
int start = 0 , end = sym_cnt ;
int result ;
2019-04-04 07:17:55 +09:00
/* kallsyms not loaded. return NULL */
if ( sym_cnt < = 0 )
return NULL ;
2018-04-28 22:28:13 -07:00
while ( start < end ) {
size_t mid = start + ( end - start ) / 2 ;
result = key - syms [ mid ] . addr ;
if ( result < 0 )
end = mid ;
else if ( result > 0 )
start = mid + 1 ;
else
return & syms [ mid ] ;
}
if ( start > = 1 & & syms [ start - 1 ] . addr < key & &
key < syms [ start ] . addr )
/* valid ksym */
return & syms [ start - 1 ] ;
/* out of range. return _stext */
return & syms [ 0 ] ;
}
2018-05-24 11:21:11 -07:00
long ksym_get_addr ( const char * name )
{
int i ;
for ( i = 0 ; i < sym_cnt ; i + + ) {
if ( strcmp ( syms [ i ] . name , name ) = = 0 )
return syms [ i ] . addr ;
}
return 0 ;
}
2020-03-21 19:04:23 +09:00
void read_trace_pipe ( void )
{
int trace_fd ;
trace_fd = open ( DEBUGFS " trace_pipe " , O_RDONLY , 0 ) ;
if ( trace_fd < 0 )
return ;
while ( 1 ) {
static char buf [ 4096 ] ;
ssize_t sz ;
sz = read ( trace_fd , buf , sizeof ( buf ) - 1 ) ;
if ( sz > 0 ) {
buf [ sz ] = 0 ;
puts ( buf ) ;
}
}
}