@@ -221,6 +221,7 @@ export type CSourceLine = {
221221
222222export enum KnownMessageInfoType {
223223 GLOBAL_FUNC_VALID = "GLOBAL_FUNC_VALID" ,
224+ BPF_STATE_EXPRS = "BPF_STATE_EXPRS" ,
224225}
225226
226227export type GlobalFuncValidInfo = {
@@ -229,7 +230,13 @@ export type GlobalFuncValidInfo = {
229230 funcName : string ;
230231} ;
231232
232- export type KnownMessageInfo = GlobalFuncValidInfo ;
233+ export type BpfStateExprsInfo = {
234+ type : KnownMessageInfoType . BPF_STATE_EXPRS ;
235+ pc : number ;
236+ bpfStateExprs : BpfStateExpr [ ] ;
237+ } ;
238+
239+ export type KnownMessageInfo = BpfStateExprsInfo | GlobalFuncValidInfo ;
233240
234241export type KnownMessageLine = {
235242 type : ParsedLineType . KNOWN_MESSAGE ;
@@ -247,11 +254,67 @@ type BpfStateExpr = {
247254 value : string ;
248255 rawKey : string ;
249256 frame ?: number ;
257+ pc ?: number ;
250258} ;
251259
252260export const BPF_SCRATCH_REGS = [ "r1" , "r2" , "r3" , "r4" , "r5" ] ;
253261export const BPF_CALLEE_SAVED_REGS = [ "r6" , "r7" , "r8" , "r9" ] ;
254262
263+ const RE_WHITESPACE = / ^ \s + / ;
264+ const RE_PROGRAM_COUNTER = / ^ ( [ 0 - 9 ] + ) : / ;
265+ const RE_BPF_OPCODE = / ^ \( ( [ 0 - 9 a - f ] [ 0 - 9 a - f ] ) \) / ;
266+ const RE_REGISTER = / ^ ( r 1 0 | r [ 0 - 9 ] | w [ 0 - 9 ] ) / ;
267+ const RE_MEMORY_REF = / ^ \* \( ( u 8 | u 1 6 | u 3 2 | u 6 4 ) \* \) \( ( r 1 0 | r [ 0 - 9 ] ) ( [ + - ] [ 0 - 9 ] + ) \) / ;
268+ const RE_IMM_VALUE = / ^ ( 0 x [ 0 - 9 a - f ] + | [ + - ] ? [ 0 - 9 ] + ) / ;
269+ const RE_CALL_TARGET = / ^ c a l l ( [ 0 - 9 a - z _ # + - ] + ) / ;
270+ const RE_GOTO_OP = / ^ ( (?: m a y _ ) ? g o t o | g o t o _ o r _ n o p ) ( p c [ + - ] [ 0 - 9 ] + ) / ;
271+ const RE_FRAME_ID = / ^ f r a m e ( [ 0 - 9 ] + ) : / ;
272+ const RE_ADDR_SPACE_CAST =
273+ / ^ ( r [ 0 - 9 ] ) = a d d r _ s p a c e _ c a s t \( ( r [ 0 - 9 ] ) , ( [ 0 - 9 ] + , [ 0 - 9 ] + ) \) / ;
274+ const RE_C_SOURCE_LINE = / ^ ; \s * ( .* ) @ ( [ a - z A - Z 0 - 9 _ \- . ] + ) : ( [ 0 - 9 ] + ) / ;
275+
276+ const BPF_ALU_OPERATORS = [
277+ "s>>=" ,
278+ "s<<=" ,
279+ "<<=" ,
280+ ">>=" ,
281+ "+=" ,
282+ "-=" ,
283+ "*=" ,
284+ "/=" ,
285+ "%=" ,
286+ "&=" ,
287+ "|=" ,
288+ "^=" ,
289+ "=" ,
290+ ] ;
291+ const BPF_COND_OPERATORS = [
292+ "s>=" ,
293+ "s<=" ,
294+ "==" ,
295+ "!=" ,
296+ "<=" ,
297+ ">=" ,
298+ "s<" ,
299+ "s>" ,
300+ "<" ,
301+ ">" ,
302+ ] ;
303+
304+ function parseProgramCounter ( str : string ) : { pc : number | null ; rest : string } {
305+ const { match, rest } = consumeRegex ( RE_PROGRAM_COUNTER , str ) ;
306+ if ( match )
307+ return {
308+ pc : parseInt ( match [ 1 ] , 10 ) ,
309+ rest,
310+ } ;
311+ else
312+ return {
313+ pc : null ,
314+ rest : str ,
315+ } ;
316+ }
317+
255318const parseBpfStateExpr = (
256319 str : string ,
257320) : { expr : BpfStateExpr ; rest : string } | undefined => {
@@ -285,10 +348,9 @@ const parseBpfStateExpr = (
285348export const parseBpfStateExprs = (
286349 str : string ,
287350) : { exprs : BpfStateExpr [ ] ; rest : string } => {
288- let { match, rest } = consumeString ( "; " , str ) ;
289- if ( ! match ) return { exprs : [ ] , rest : str } ;
351+ let { pc, rest } = parseProgramCounter ( str ) ;
290352
291- let frame = consumeRegex ( RE_FRAME_ID , rest ) ;
353+ let frame = consumeRegex ( RE_FRAME_ID , consumeSpaces ( rest ) ) ;
292354 let frameId = 0 ;
293355 if ( frame . match ) {
294356 frameId = parseInt ( frame . match [ 1 ] , 10 ) ;
@@ -301,52 +363,12 @@ export const parseBpfStateExprs = (
301363 if ( ! parsed ) break ;
302364 rest = consumeSpaces ( parsed . rest ) ;
303365 parsed . expr . frame = frameId ;
366+ if ( pc ) parsed . expr . pc = pc ;
304367 exprs . push ( parsed . expr ) ;
305368 }
306369 return { exprs, rest } ;
307370} ;
308371
309- const RE_WHITESPACE = / ^ \s + / ;
310- const RE_PROGRAM_COUNTER = / ^ ( [ 0 - 9 ] + ) : / ;
311- const RE_BPF_OPCODE = / ^ \( ( [ 0 - 9 a - f ] [ 0 - 9 a - f ] ) \) / ;
312- const RE_REGISTER = / ^ ( r 1 0 | r [ 0 - 9 ] | w [ 0 - 9 ] ) / ;
313- const RE_MEMORY_REF = / ^ \* \( ( u 8 | u 1 6 | u 3 2 | u 6 4 ) \* \) \( ( r 1 0 | r [ 0 - 9 ] ) ( [ + - ] [ 0 - 9 ] + ) \) / ;
314- const RE_IMM_VALUE = / ^ ( 0 x [ 0 - 9 a - f ] + | [ + - ] ? [ 0 - 9 ] + ) / ;
315- const RE_CALL_TARGET = / ^ c a l l ( [ 0 - 9 a - z _ # + - ] + ) / ;
316- const RE_GOTO_OP = / ^ ( (?: m a y _ ) ? g o t o | g o t o _ o r _ n o p ) ( p c [ + - ] [ 0 - 9 ] + ) / ;
317- const RE_FRAME_ID = / ^ f r a m e ( [ 0 - 9 ] + ) : / ;
318- const RE_ADDR_SPACE_CAST =
319- / ^ ( r [ 0 - 9 ] ) = a d d r _ s p a c e _ c a s t \( ( r [ 0 - 9 ] ) , ( [ 0 - 9 ] + , [ 0 - 9 ] + ) \) / ;
320- const RE_C_SOURCE_LINE = / ^ ; \s * ( .* ) @ ( [ a - z A - Z 0 - 9 _ \- . ] + ) : ( [ 0 - 9 ] + ) / ;
321-
322- const BPF_ALU_OPERATORS = [
323- "s>>=" ,
324- "s<<=" ,
325- "<<=" ,
326- ">>=" ,
327- "+=" ,
328- "-=" ,
329- "*=" ,
330- "/=" ,
331- "%=" ,
332- "&=" ,
333- "|=" ,
334- "^=" ,
335- "=" ,
336- ] ;
337- const BPF_COND_OPERATORS = [
338- "s>=" ,
339- "s<=" ,
340- "==" ,
341- "!=" ,
342- "<=" ,
343- ">=" ,
344- "s<" ,
345- "s>" ,
346- "<" ,
347- ">" ,
348- ] ;
349-
350372function consumeRegex (
351373 regex : RegExp ,
352374 str : string ,
@@ -820,18 +842,15 @@ function parseBpfInstruction(
820842 rawLine : string ,
821843 idx : number ,
822844) : InstructionLine | null {
823- let { match, rest } = consumeRegex (
824- RE_PROGRAM_COUNTER ,
825- consumeSpaces ( rawLine ) ,
826- ) ;
827- if ( ! match ) return null ;
845+ let { pc, rest } = parseProgramCounter ( consumeSpaces ( rawLine ) ) ;
846+ if ( pc === null ) return null ;
828847
829- const pc = parseInt ( match [ 1 ] , 10 ) ;
830848 const parsedIns = parseOpcodeIns ( consumeSpaces ( rest ) , pc ) ;
831849 if ( ! parsedIns . ins ) return null ;
832850
833851 const ins = parsedIns . ins ;
834852 rest = consumeSpaces ( parsedIns . rest ) ;
853+ rest = consumeString ( "; " , rest ) . rest ;
835854 const { exprs } = parseBpfStateExprs ( rest ) ;
836855 return {
837856 type : ParsedLineType . INSTRUCTION ,
@@ -845,21 +864,41 @@ function parseBpfInstruction(
845864const RE_MSG_GLOBAL_FUNC_VALID =
846865 / ^ F u n c # ( [ - 0 - 9 ] + ) \( ' ( .+ ) ' \) i s g l o b a l a n d a s s u m e d v a l i d \. / ;
847866
867+ function parseGlobalFuncValidMessage ( str : string ) : GlobalFuncValidInfo | null {
868+ let { match } = consumeRegex ( RE_MSG_GLOBAL_FUNC_VALID , str ) ;
869+ if ( ! match ) return null ;
870+ return {
871+ type : KnownMessageInfoType . GLOBAL_FUNC_VALID ,
872+ funcId : parseInt ( match [ 1 ] , 10 ) ,
873+ funcName : match [ 2 ] ,
874+ } ;
875+ }
876+
877+ function parseExprsOnly ( str : string ) : BpfStateExprsInfo | null {
878+ let { pc, rest } = parseProgramCounter ( consumeSpaces ( str ) ) ;
879+ if ( pc === null ) return null ;
880+ rest = consumeSpaces ( rest ) ;
881+ const { exprs } = parseBpfStateExprs ( rest ) ;
882+ return {
883+ type : KnownMessageInfoType . BPF_STATE_EXPRS ,
884+ bpfStateExprs : exprs ,
885+ pc,
886+ } ;
887+ }
888+
848889function parseKnownMessage (
849890 rawLine : string ,
850891 idx : number ,
851892) : KnownMessageLine | null {
852- let { match } = consumeRegex ( RE_MSG_GLOBAL_FUNC_VALID , rawLine ) ;
853- if ( ! match ) return null ;
893+ let info : KnownMessageInfo | null ;
894+ info = parseGlobalFuncValidMessage ( rawLine ) ;
895+ if ( ! info ) info = parseExprsOnly ( rawLine ) ;
896+ if ( ! info ) return null ;
854897 return {
855898 type : ParsedLineType . KNOWN_MESSAGE ,
856899 idx,
857900 raw : rawLine ,
858- info : {
859- type : KnownMessageInfoType . GLOBAL_FUNC_VALID ,
860- funcId : parseInt ( match [ 1 ] , 10 ) ,
861- funcName : match [ 2 ] ,
862- } ,
901+ info,
863902 } ;
864903}
865904
0 commit comments