MCPcopy
hub / github.com/crowdsecurity/crowdsec / RunWithDebug

Function RunWithDebug

pkg/exprhelpers/debugger.go:467–535  ·  view source on GitHub ↗

TBD: Based on the level of the logger (ie. trace vs debug) we could decide to add more low level instructions (pop, push, etc.)

(program *vm.Program, env any, logger *log.Entry)

Source from the content-addressed store, hash-verified

465
466// TBD: Based on the level of the logger (ie. trace vs debug) we could decide to add more low level instructions (pop, push, etc.)
467func RunWithDebug(program *vm.Program, env any, logger *log.Entry) ([]OpOutput, any, error) {
468 outputs := []OpOutput{}
469 erp := ExprRuntimeDebug{
470 Logger: logger,
471 }
472 vm := vm.Debug()
473 opcodes := program.Disassemble()
474 lines := strings.Split(opcodes, "\n")
475 erp.Lines = lines
476
477 go func() {
478 // We must never return until the execution of the program is done
479 erp.Logger.Tracef("[START] ip 0")
480
481 ops := erp.ipSeek(0)
482 if ops == nil {
483 log.Warningf("error while debugging expr: failed getting ops for ip 0")
484 }
485
486 outputs = erp.ipDebug(0, vm, program, ops, outputs)
487
488 vm.Step()
489
490 for ip := range vm.Position() {
491 ops := erp.ipSeek(ip)
492 if ops == nil {
493 erp.Logger.Tracef("[DONE] ip %d", ip)
494 break
495 }
496
497 outputs = erp.ipDebug(ip, vm, program, ops, outputs)
498
499 vm.Step()
500 }
501 }()
502
503 var return_error error
504
505 ret, err := vm.Run(program, env)
506 // if the expr runtime failed, we don't need to wait for the debug to finish
507 if err != nil {
508 return_error = err
509 }
510 // the overall result of expression is the result of last op ?
511 if len(outputs) > 0 {
512 lastOutIdx := len(outputs)
513 if lastOutIdx > 0 {
514 lastOutIdx -= 1
515 }
516
517 switch val := ret.(type) {
518 case bool:
519 log.Tracef("completing with bool %t", ret)
520 // if outputs[lastOutIdx].Comparison {
521 outputs[lastOutIdx].StrConditionResult = fmt.Sprintf("%v", ret)
522 outputs[lastOutIdx].ConditionResult = new(bool)
523 *outputs[lastOutIdx].ConditionResult = val
524 outputs[lastOutIdx].Finalized = true

Callers 2

TestBaseDbgFunction · 0.85
RunFunction · 0.85

Calls 5

ipSeekMethod · 0.95
ipDebugMethod · 0.95
TracefMethod · 0.80
RunMethod · 0.65
DebugMethod · 0.45

Tested by 1

TestBaseDbgFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…