Skip to content

Commit

Permalink
Bootstrap: network_cmds depends on KeyronexKit
Browse files Browse the repository at this point in the history
  • Loading branch information
netbsduser committed Jun 22, 2023
1 parent 4ed045e commit a3af871
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ ports/
bundled/
*.xbstrap
build/
debug.log
1 change: 1 addition & 0 deletions bootstrap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ packages:
- 'host-gcc'
pkgs_required:
- 'mlibc'
- 'KeyronexKit'
configure:
- args:
- 'meson'
Expand Down
18 changes: 18 additions & 0 deletions tools/memdbg/lexer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%{
#include <stdlib.h>
#include "parser.tab.h"

extern int yylex(void);
extern int yyerror(const char *);
%}

%option noyywrap nounput noinput

%%
"ALLOC" return ALLOC;
"FREE" return FREE;
[0-9]+ { yylval.i = atoi(yytext); return INT; }
0[xX][0-9a-fA-F]+ { yylval.str = yytext; return HEX; }
"str:"[^ \n\t]* { yylval.str = yytext + 4; return STR; }
\n
.
57 changes: 57 additions & 0 deletions tools/memdbg/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "parser.tab.h"

#include "result.hh"
#include <iostream>

std::map<std::string, Allocation> allocs;

extern int yyparse();

int
main(int argc, char **argv)
{
int input;
if (argc != 2) {
printf("%s <input file>\n", argv[0]);
return 1;
}
input = open(argv[1], O_RDONLY);
dup2(input, STDIN_FILENO);
close(input);
// yydebug = true;
yyparse();

for (auto &alloc : allocs) {
std::cout << "By " << alloc.second.pid << " In "
<< alloc.second.zone << ": " << alloc.first << "\n";

for (int i = 5; i < 7; i++) {
std::string invocation =
"addr2line -e ../../../build/system-root/boot/keyronex " +
(*alloc.second.addresses)[i];
std::cout << " ";
system(invocation.c_str());
}
}
}

int
yywrap(void)
{
return 0;
}

int
yyerror(const char *msg)
{
printf("Parse error: %s\n", msg);
exit(1);
}
29 changes: 29 additions & 0 deletions tools/memdbg/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
project('memdbg', 'c', 'cpp')

flex = find_program('flex', required: false)
bison = find_program('bison', required: false)

if not flex.found()
error('flex not found.')
endif

if not bison.found()
error('bison not found.')
endif

lgen = generator(flex,
output : '@PLAINNAME@.yy.cc',
arguments : ['-o', '@OUTPUT@', '@INPUT@'])

lfiles = lgen.process('lexer.l')

pgen = generator(bison,
output : ['@BASENAME@.tab.cc', '@BASENAME@.tab.h'],
arguments : ['@INPUT@', '--defines=@OUTPUT1@', '--output=@OUTPUT0@'])

pfiles = pgen.process('parser.y')

e = executable('pgen', 'main.cc',
lfiles,
pfiles)

90 changes: 90 additions & 0 deletions tools/memdbg/parser.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

%code requires {
#include <list>
#include <string>
#include <vector>
}

%{
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <string>

#include "result.hh"

extern int yylex(void);
extern int yyerror(const char*);
%}


%union {
const char *str;
std::vector<std::string> *strlist;
int i;
}

%debug

%token ALLOC FREE
%token <str> HEX STR
%token <i> INT

%type <str> string hex

%type <strlist> hex_list opt_hex_list

%%
input:
note_list ;

note_list:
note
| note_list note
;

string: STR
{
$$ = strdup($1);
}

hex: HEX
{
$$ = strdup($1);
}


hex_list:
hex {
auto list = new std::vector<std::string>;
list->push_back($1);
$$ = list;
}
| hex_list hex {
$$ = $1;
$$->push_back($2);
}
;

opt_hex_list:
hex_list { $$ = $1; }
| %empty { $$ = NULL; }
;

note:
ALLOC INT string hex opt_hex_list {
if(allocs.find($4) != allocs.end()) {
std::cout << "Allocation of an already-allocated address!\n";
abort();
}
allocs[$4] = Allocation { $2, $3, $5 };
}
| FREE string hex {
auto it = allocs.find($3);
if (it != allocs.end())
allocs.erase(it);
else
std::cout << "Didn't find " << $3 << "\n";
}
;
16 changes: 16 additions & 0 deletions tools/memdbg/result.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef KRX_MEMDBG_RESULT_HH
#define KRX_MEMDBG_RESULT_HH

#include <string>
#include <map>
#include <vector>

struct Allocation {
int pid;
std::string zone;
std::vector<std::string> *addresses;
};

extern std::map<std::string, Allocation> allocs;

#endif /* KRX_MEMDBG_RESULT_HH */

0 comments on commit a3af871

Please sign in to comment.