@@ -2,15 +2,12 @@ package collector
22
33import (
44 "fmt"
5- "log/slog"
65 "os"
76 "os/exec"
87 "path"
98 "path/filepath"
10- "strings"
119
1210 "github.com/AlexsanderHamir/prof/config"
13- "github.com/AlexsanderHamir/prof/parser"
1411 "github.com/AlexsanderHamir/prof/shared"
1512)
1613
@@ -34,18 +31,17 @@ func RunCollector(files []string, tag string) error {
3431 }
3532
3633 var functionFilter config.FunctionFilter
37-
3834 globalFilter , hasGlobalFilter := cfg .FunctionFilter [globalSign ]
3935 if hasGlobalFilter {
4036 functionFilter = globalFilter
4137 }
4238
43- for _ , binaryFilePath := range files {
44- binaryDirName := filepath . Base ( binaryFilePath )
45- fileName := strings . TrimSuffix ( binaryDirName , filepath . Ext ( binaryDirName ) )
46- profileDirPath := path . Join (tagDir , fileName )
47- if err = ensureDirExists ( profileDirPath ); err != nil {
48- return err
39+ var profileDirPath string
40+ for _ , fullBinaryPath := range files {
41+ fileName := getFileName ( fullBinaryPath )
42+ profileDirPath , err = createProfileDirectory (tagDir , fileName )
43+ if err != nil {
44+ return fmt . Errorf ( "createProfileDirectory failed: %w" , err )
4945 }
5046
5147 if ! hasGlobalFilter {
@@ -56,23 +52,12 @@ func RunCollector(files []string, tag string) error {
5652 }
5753
5854 outputTextFilePath := path .Join (profileDirPath , fileName + "." + shared .TextExtension )
59- if err = GenerateProfileTextOutput (binaryFilePath , outputTextFilePath ); err != nil {
60- return err
61- }
62-
63- var functions []string
64- functions , err = parser .GetAllFunctionNames (outputTextFilePath , functionFilter )
65- if err != nil {
66- return fmt .Errorf ("failed to extract function names: %w" , err )
67- }
68-
69- functionDir := path .Join (profileDirPath , "functions" )
70- if err = ensureDirExists (functionDir ); err != nil {
55+ if err = GenerateProfileTextOutput (fullBinaryPath , outputTextFilePath ); err != nil {
7156 return err
7257 }
7358
74- if err = SaveAllFunctionsPprofContents ( functions , binaryFilePath , functionDir ); err != nil {
75- return fmt .Errorf ("getAllFunctionsPprofContents failed: %w" , err )
59+ if err = collectFunctions ( outputTextFilePath , profileDirPath , fullBinaryPath , functionFilter ); err != nil {
60+ return fmt .Errorf ("collectFunctions failed: %w" , err )
7661 }
7762 }
7863 return nil
@@ -106,31 +91,11 @@ func GeneratePNGVisualization(binaryFile, outputFile string) error {
10691 return os .WriteFile (outputFile , output , shared .PermFile )
10792}
10893
109- // GetFunctionPprofContent gets code line level mapping of specified function
110- // and writes the data to a file named after the function.
111- func GetFunctionPprofContent (function , binaryFile , outputFile string ) error {
112- cmd := []string {"go" , "tool" , "pprof" , fmt .Sprintf ("-list=%s" , function ), binaryFile }
113-
114- // #nosec ProfileTextDir04 -- cmd is constructed internally by getFunctionPprofContent(), not from user input
115- execCmd := exec .Command (cmd [0 ], cmd [1 :]... )
116- output , err := execCmd .Output ()
117- if err != nil {
118- return fmt .Errorf ("pprof list command failed: %w" , err )
119- }
120-
121- if err = os .WriteFile (outputFile , output , shared .PermFile ); err != nil {
122- return fmt .Errorf ("failed to write function content: %w" , err )
123- }
124-
125- slog .Info ("Collected function" , "function" , function )
126- return nil
127- }
128-
12994// SaveAllFunctionsPprofContents calls [GetFunctionPprofContent] sequentially.
13095func SaveAllFunctionsPprofContents (functions []string , binaryPath , basePath string ) error {
13196 for _ , functionName := range functions {
13297 outputFile := filepath .Join (basePath , functionName + "." + shared .TextExtension )
133- if err := GetFunctionPprofContent (functionName , binaryPath , outputFile ); err != nil {
98+ if err := getFunctionPprofContent (functionName , binaryPath , outputFile ); err != nil {
13499 return fmt .Errorf ("failed to extract function content for %s: %w" , functionName , err )
135100 }
136101 }
0 commit comments