Using the go trace tool with gocryptfs - go

I complied gocryptfs from source and I wanted to use the trace tool. So I followed https://making.pusher.com/go-tool-trace/ and added the following lines of code in the main function:
func main() {
f, er := os.Create("trace.out")
if er != nil {
panic(er)
}
defer f.Close()
er = trace.Start(f)
if er != nil {
panic(er)
}
defer trace.Stop()
mxp := runtime.GOMAXPROCS(0)
// rest of the function
}
I recompile via ./build-without-openssl.bash. When I run the trace tool, I get the following error:
2023/01/31 14:36:24 Parsing trace...
failed to parse trace: trace is empty
Any pointers to what's going wrong here, or if there's another blog I can follow?

Related

Creating and seeding a torrent in golang

I want to use the golang library https://github.com/anacrolix/torrent to create to torrent and get a magnet and seed the torrent. Below you can find the code I wrote. Yet, if I use the magnet the code generates I can not download anything not even the metainfo.
Am I missing something here?
package main
import (
"log"
"time"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/metainfo"
)
var builtinAnnounceList = [][]string{
{"http://p4p.arenabg.com:1337/announce"},
{"udp://tracker.opentrackr.org:1337/announce"},
{"udp://tracker.openbittorrent.com:6969/announce"},
}
func main() {
tmpComment:="Cool torrent description"
tmpCreatedBy:="coolboys"
tmpInfoName:="CoolInfoName"
mi := metainfo.MetaInfo{
AnnounceList: builtinAnnounceList,
}
mi.SetDefaults()
mi.Comment = tmpComment
mi.CreatedBy = tmpCreatedBy
//}
//mi.UrlList = args.Url//???????????
info := metainfo.Info{
PieceLength: 256 * 1024,
}
err := info.BuildFromFilePath("./TorrentFiles")//args.Root)
if err != nil {
log.Fatal(err)
}
info.Name =tmpInfoName
mi.InfoBytes, err = bencode.Marshal(info)
if err != nil {
log.Fatal(err)
}
tmpMagnet:=mi.Magnet(nil,nil)
log.Println("****",tmpMagnet)
//
cfg := torrent.NewDefaultClientConfig()
cfg.Seed = true
mainclient, ncerr := torrent.NewClient(cfg)
if ncerr != nil {
log.Println("new torrent client:", ncerr)
return
}
defer mainclient.Close()
t, _ := mainclient.AddMagnet(tmpMagnet.String())
for {
log.Println("******", t.Seeding())
time.Sleep(8 * time.Second)
}
}
I think you might need to take a closer look at this example. Among other things, I don't see any invocation of t.DownloadAll() to do the actual download or mainclient.WaitAll() to tell you when the downloads are complete.
I corrected the code I wrote and the correct code can be found here:
enter link description here
In the original code I should not have used addmagnet as it assumes that I don't have the info available, which is why it would fail to seed.

How can I debug my Golang API code to show me what is wrong?

I have this module that use Google Cloud API to retrieve a list of all running Virtual Machine instances for a particular project. I'm new to Go, and followed the intro tutorial to help me out. I'm still trying to debug my code but no luck.
The problem is I'm able to communicate to Google Cloud API and pass authentication but that is all I can get through
compute.go module:
compute.go is able to communicate to Google Cloud servers and pass authentication (I'm not getting an auth error)
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compute
// [START compute_instances_list_all]
import (
"context"
"fmt"
"io"
compute "cloud.google.com/go/compute/apiv1"
"google.golang.org/api/iterator"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/protobuf/proto"
)
// listAllInstances prints all instances present in a project, grouped by their zone.
func ListAllInstances(w io.Writer, projectID string) error {
// projectID := "your_project_id"
ctx := context.Background()
instancesClient, err := compute.NewInstancesRESTClient(ctx)
// instancesClient, err := compute.NewInstancesRESTClient(ctx, option.WithCredentialsFile(`C:\path\to\jsonkey.json`))
if err != nil {
return fmt.Errorf("NewInstancesRESTClient: %v", err)
}
defer instancesClient.Close()
// Use the `MaxResults` parameter to limit the number of results that the API returns per response page.
req := &computepb.AggregatedListInstancesRequest{
Project: projectID,
MaxResults: proto.Uint32(6),
}
it := instancesClient.AggregatedList(ctx, req)
fmt.Fprintf(w, "Instances found:\n")
// Despite using the `MaxResults` parameter, you don't need to handle the pagination
// yourself. The returned iterator object handles pagination
// automatically, returning separated pages as you iterate over the results.
for {
pair, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
instances := pair.Value.Instances
if len(instances) > 0 {
fmt.Fprintf(w, "%s\n", pair.Key)
for _, instance := range instances {
fmt.Fprintf(w, "- %s %s\n", instance.GetName(), instance.GetMachineType())
}
}
}
return nil
}
// [END compute_instances_list_all]
However the problem is when I run my main function that calls ListAllInstances, it returns a <nil>. Not allowing me to know what is wrong.
caller api.go module where I run go run .:
package main
import (
"fmt"
"example.com/compute"
"bytes"
)
func main() {
buf := new(bytes.Buffer)
// Get a message and print it.
respone := compute.ListAllInstances(buf, "project-unique-id")
fmt.Println(respone)
}
How else can I further debug this to figure out what is wrong with my code?
You're not printing buf. Your function returns an object of type error, which is nil (no error!), the actual output is written to buf.
Either print it out:
func main() {
buf := new(bytes.Buffer)
// Get a message and print it.
err := compute.ListAllInstances(buf, "project-unique-id")
if err != nil {
panic(err)
}
fmt.Println(buf.String()) // <======= Print buf contents!
}
Or just use os.Stdout:
func main() {
err := compute.ListAllInstances(os.Stdout, "project-unique-id")
if err != nil {
panic(err)
}
}
To answer your question about debugging, try using VSCode with the Go extension, in there you can run a debugger, set breakpoints and step through the code line-by-line, watching how variables change.
See also Debug Go programs in VS Code.

errors.Is() doesn't function propertly

I pasted a section of code that was supposed to catch an AllTopologyNodesDownError error which doesn't work and I have no idea why.
func (sc *ServerConfig) addNodesToCluster(store *ravendb.DocumentStore) error {
clusterTopology, err := sc.getClusterTopology(store)
if errors.Is(err, &ravendb.AllTopologyNodesDownError{}) {
for _, url := range sc.Url.List {
err = addNodeToCluster(store, url)
if err != nil {
return err
}
}
} else if err != nil {
return err
}
the structure of the ravendb.AllTopologyNodesDownError is
// AllTopologyNodesDownError represents "all topology nodes are down" error
type AllTopologyNodesDownError struct {
errorBase
}
type errorBase struct {
wrapped error
ErrorStr string
}
screen shot of the error when debugging the code
errors.Is() is used to tell if any error in the chain is the same instance as the provided error1, that can never be the case here because you provided a literal of your error type, no other code could hold that instance or a reference to it.
Your error looks like a type, to tell if any error in the chain is a given type you should use errors.As():
clusterTopology, err := sc.getClusterTopology(store)
var errAllDown *AllTopologyNodesDownError
if errors.As(err, &errAllDown) {
// err had an *AllTopologyNodesDownError in its
// chain and errAllDown now contains it.
}
Can be overridden by implementing the Unwrap() interface which your error type does not.

Things to be taken care Before Running Stellar Horizon Test Cases

I have cloned the Stellar Horizon Repo from GitHub written in Go.
I thought to run the test cases first.where the test cases have been written by using GINKGO Testing Framework. I have been running the test cases using ginkgo command
eg : ginkgo (package path name will be given here).
This is how I run test cases. Test cases for particular package has been executing properly.
But I am getting a panic in the first test file only. Which I debugged and found that the panic is occurring while running a command. Please find the below details for reference .
File name : action_accounts_test.go
func name : TestAccountAction_Show() // Has been defined below
func TestAccountActions_Show(t *testing.T) {
ht := StartHTTPTest(t, "allow_trust") **// This function is not returning anything. Its //getting panic inside this //**
defer ht.Finish()
// existing account
w := ht.Get(
"/accounts/GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU",
)
if ht.Assert.Equal(200, w.Code) {
var result horizon.Account
err := json.Unmarshal(w.Body.Bytes(), &result)
ht.Require.NoError(err)
ht.Assert.Equal("8589934593", result.Sequence)
ht.Assert.NotEqual(0, result.LastModifiedLedger)
for _, balance := range result.Balances {
if balance.Type == "native" {
ht.Assert.Equal(uint32(0), balance.LastModifiedLedger)
} else {
ht.Assert.NotEqual(uint32(0), balance.LastModifiedLedger)
}
}
}
// missing account
w = ht.Get("/accounts/GDBAPLDCAEJV6LSEDFEAUDAVFYSNFRUYZ4X75YYJJMMX5KFVUOHX46SQ")
ht.Assert.Equal(404, w.Code)
}
I did a Debugging and found that its getting panic in the below func call which happens inside the above func StartHTTPTest()
scenarios.Load(StellarCoreDatabaseURL(), stellarCorePath)
scenarios.Load(DatabaseURL(), horizonPath)
Definition of the particular func
func Load(url string, path string) {
log.Println("database url", url, " Path : ", path)
sql, err := Asset(path)
log.Println("Print err:", err, " sql :", sql)
if err != nil {
log.Panic(err)
}
reader := bytes.NewReader(sql)
cmd := exec.Command("psql", url)
cmd.Stdin = reader
err = cmd.Run() **// Exactly here it return some error**
log.Println("Print err", err)
if err != nil { **// Since err is not nil .the statement will get executed .**
**log.Panic(err)**
}
}
The Error returned by cmd.Run() is exit status 2
I just printed the error and found this is the error exit status 2
What is the reason for the particular error?

How to `catch` specific errors

For example, I am using one Go standard library function as:
func Dial(network, address string) (*Client, error)
This function may return errors, and I just care about errors which report "connection lost" or "connection refused", then do some code to fix these.
It seems like:
client, err := rpc.Dial("tcp", ":1234")
if err == KindOf(ConnectionRefused) {
// do something
}
What's more, how to get all the errors a specific standard library function may return?
There's no standard way to do this.
The most obvious way, which should only be used if no other method is available, is to compare the error string against what you expect:
if err.Error() == "connection lost" { ... }
Or perhaps more robust in some situations:
if strings.HasSuffix(err.Error(), ": connection lost") { ... }
But many libraries will return specific error types, which makes this much easier.
In your case, what's relevant are the various error types exported by the net package: AddrError, DNSConfigError, DNSError, Error, etc.
You probably care most about net.Error, which is used for network errors. So you could check thusly:
if _, ok := err.(net.Error); ok {
// You know it's a net.Error instance
if err.Error() == "connection lost" { ... }
}
What's more, how to get all the errors a specific standard library function may return?
The only fool-proof way to do this is to read the source for the library. Before going to that extreme, a first step is simply to read the godoc, as in the case of the net package, the errors are pretty well documented.
You can now use the errors.Is() function to compare some standard errors:
client, err := net.Dial("tcp", ":1234")
if errors.Is(err, net.ErrClosed) {
fmt.Println("connection has been closed.")
}
A common file opening scenario:
file, err := os.Open("non-existing");
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
fmt.Println("file does not exist")
} else {
fmt.Println(err)
}
}
UPDATE
You can also use errors.As() if you'd like to check the error type:
client, err := net.Dial("tcp", ":1234")
var errC = net.ErrClosed
if errors.As(err, &errC) {
fmt.Printf("connection has been closed: %s", errC)
}
client.Close()
A more detailed explanation can be found in the Go Blog.

Resources